Android低功耗蓝牙发送消息到设备进行响应

时间:2019-02-18 15:26:04

标签: android bluetooth-lowenergy

我想与蓝牙低能耗设备进行通信。我应该能够向它发送“特殊”消息,并且它应该发送回一个数字(该设备是测距仪)。

在服务发现部分之前,一切似乎都不错。但是,我无法读取响应,因为在编写特征时仅会触发onCharacteristicWrite方法。

我知道我想念一些东西,因为我可以通过演示阅读器应用程序获得响应(没有可用的源代码)。

这是代码的相关部分:

private fun startScan() {
    val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
    device = pairedDevices?.first { it.name.startsWith("RN487") } ?: return
    bluetoothAdapter.bluetoothLeScanner.startScan(object : ScanCallback() {
      override fun onScanResult(callbackType: Int, result: ScanResult?) {
        super.onScanResult(callbackType, result)
        if (result?.device?.address?.equals(device.address) == true) {
          bluetoothAdapter.bluetoothLeScanner.stopScan(this)
          device.connectGatt(applicationContext, true, object : BluetoothGattCallback() {
            override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
              super.onConnectionStateChange(gatt, status, newState)
              val intentAction: String
              when (newState) {
                BluetoothProfile.STATE_CONNECTED -> {
                  connectionState = hu.horvathady.sarang.camera.STATE_CONNECTED
                  intentAction = ACTION_GATT_CONNECTED
                  broadcastUpdate(intentAction)
                  gatt?.discoverServices()
                  Log.e(LOG, "Connected.")
                }
                else -> {
                  connectionState = hu.horvathady.sarang.camera.STATE_DISCONNECTED
                  intentAction = ACTION_GATT_DISCONNECTED
                  broadcastUpdate(intentAction)
                  Log.e(LOG, "Disconnected.")
                }
              }
            }

            // New services discovered
            override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
              Log.e(LOG, "service discovered.")
              for (gattService in gatt.services) {
                if (gattService.uuid.toString().equals("49535343-fe7d-4ae5-8fa9-9fafd205e455")) {
                  val characteristics = gattService.characteristics
                  val measure = byteArrayOf(0x40, 0x64, 0x0D, 0x0A)
                  for (characteristic in characteristics) {
                    Log.e(LOG, characteristic.uuid.toString())
                    characteristic.value = measure
                    characteristic.writeType = 2
                    gatt.writeCharacteristic(characteristic)
                    Log.e(LOG, "x" + measure.contentToString())
                  }
                }
              }
              when (status) {
                BluetoothGatt.GATT_SUCCESS -> {
                  broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED)
                }
                else -> Log.e(LOG, "onServicesDiscovered received: $status")
              }
            }

            override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) {
              super.onCharacteristicChanged(gatt, characteristic)
              Log.e(LOG + " dasfasfasf", characteristic?.value.toString())
            }

            override fun onCharacteristicRead(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) {
              super.onCharacteristicRead(gatt, characteristic, status)
              Log.e(LOG + " werqw", characteristic?.value.toString())
            }

            override fun onCharacteristicWrite(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) {
              super.onCharacteristicWrite(gatt, characteristic, status)
              if (characteristic != null) for (bajt in characteristic.value) {
                Log.e(LOG + " qqqqerqw", bajt.toString())
              }
            }
          })
        }
      }
    })
  }

这里是broadcastUpdate函数。但是,只有第一个触发。

private fun broadcastUpdate(action: String) {
    val intent = Intent(action)
    Log.e(LOG, action)
    sendBroadcast(intent)
  }

  private fun broadcastUpdate(action: String, characteristic: BluetoothGattCharacteristic) {
    val intent = Intent(action)
    Log.e(LOG, action)
  }

1 个答案:

答案 0 :(得分:0)

尝试对每个特征使用自己的自定义方法。您需要知道服务的UUID和特征的UUID:

school_state  project_is_approved
AK            0                        0.16
              1                        0.84
AL            0                        0.14
              1                        0.86