微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使为 ble 设备启用通知,也未收到通知

如何解决即使为 ble 设备启用通知,也未收到通知

我使用以下代码在使用 kotlin 的 android 设备上启用通知: 使用此代码通知已启用,但即使设备上的数据发生更改,我也没有收到通知。我没有收到任何错误,因为我正在开发我的第一个 ble 应用程序,所以我很困惑。我不知道我可以包含哪些更多细节

    is EnableNotifications -> with(operation) {
                    gatt.findCharacteristic(characteristicUuid)?.let { characteristic ->
                        val cccdUuid = UUID.fromString(CCC_DESCRIPTOR_UUID)
                        val payload = when {
                            characteristic.isIndicatable() ->
                                BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
                            characteristic.isNotifiable() ->
                                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                            else ->
                                error("${characteristic.uuid} doesn't support notifications/indications")
                        }
    
                        characteristic.getDescriptor(cccdUuid)?.let { cccDescriptor ->
                            if (!gatt.setCharacteristicNotification(characteristic,true)) {
                                Timber.e("setCharacteristicNotification Failed for ${characteristic.uuid}")
                                signalEndOfOperation()
                                return
                            }
                            cccDescriptor.value = payload
                            gatt.writeDescriptor(cccDescriptor)
    
                        }}
    
    
    //which is writing descriptor as follows:
    
    
      override fun onDescriptorWrite(
                gatt: BluetoothGatt,descriptor: BluetoothGattDescriptor,status: Int
            ) {
                with(descriptor) {
                    when (status) {
                        BluetoothGatt.GATT_SUCCESS -> {
                            Timber.i("Wrote to descriptor $uuid | value: ${value.toHexString()}")
                            if (isCccd()) {
                                onCccdWrite(gatt,value,characteristic)
                            } else {
                                listeners.forEach {
                                    it.get()?.onDescriptorWrite?.invoke(
                                        gatt.device,this
                                    )
                                }
                            }
                        }
                        BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> {
                            Timber.e("Write not permitted for $uuid!")
                        }
                        else -> {
                            Timber.e("Descriptor write Failed for $uuid,error: $status")
                        }

然后在特性上改变了以下代码得到使用:

      override fun onCharacteristicChanged(
                gatt: BluetoothGatt,characteristic: BluetoothGattCharacteristic
            ) {
                with(characteristic) {
                    super.onCharacteristicChanged(gatt,characteristic)
    
                    Timber.i("Characteristic $uuid changed | value: ${value.toHexString()}")
                    listeners.forEach {
                        it.get()?.onCharacteristicChanged?.invoke(gatt.device,this)
                    }
                }
    
            }

由于上面的代码是在管理器类中编写的,我使用来自需要接收通知的活动的以下代码调用它:

      val characteristicNotify = BluetoothGattCharacteristic(
            char_uuid,BluetoothGattCharacteristic.PROPERTY_NOTIFY,1
        )
    Manager.enableNotifications(device,characteristicNotify)

解决方法

订阅通知的代码看起来不错,因为它从BluetoothGatt找到了可用的特性,检查它是否支持订阅,调用gatt.setCharacteristicNotification然后调用gatt.writeDescriptor

从你的代码部分很难说,但我猜这些行不符合你的期望,因为这是你应该如何在外设方面创建特征,而不是中央:>

// Central should get the existing characteristic from BluetoothGatt
// but this code creates a new characteristic
val characteristicNotify = BluetoothGattCharacteristic(
        char_uuid,BluetoothGattCharacteristic.PROPERTY_NOTIFY,1
    )
Manager.enableNotifications(device,characteristicNotify)

Central 应使用已发现的 BluetoothGattService 提供的特性(在 BluetoothGattCallback.onServicesDiscovered 之后可用),但不创建新特性。

这个订阅指示的例子可能有用:BLEProof: MainActivity.kt:351 on github

此外,我建议确保您连接的外设正确发送通知 - 使用以下 Android 应用程序之一:LightBlue、BLE Scanner,您可以手动扫描您的外设、发现服务并订阅您的特征,只是为了确保外围设备按预期工作d.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。