为什么Android BLE 蓝牙onCharacteristicChanged 方法不调用?

最近接了一个新项目,但是对于蓝牙BLE并不大了解。。。这个问题卡我我很久。。一直不知道是哪里错了。onCharacteristicChanged这个方法一直不回调
我的项目需要连接一个蓝牙4.0的设备,用Android BLE 我这边设置了扫描到蓝牙有一个服务,三个特征,分别是 indicate,write,read。我在链接设备后。然后订阅indicate的 characteristic
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);

mBluetoothLeService.java
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}

BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
}
最后写入在write写入
                 byte[] data = { (byte) 0xfe, 0x01, 0x00, 0x12, (byte) 0x9c};

writegattCharacteristic.setValue(data);

mBluetoothLeService.writeCharacteristic(writegattCharacteristic);
displayData("send data:" + writegattCharacteristic.getUuid().toString() + " \n data: "
+ Utils.bytesToHexString(data));
Log.w("trySendCmd ", "send data  " + Utils.bytesToHexString(data));

但是最后在BluetoothGattCallback 的方法内onCharacteristicChanged 并不回调
麻烦大家帮我看看。。。。谢过了。。。

你的问题解决了吗?
我也遇到了一样的问题setCharacteristicNotification始終不回调?
网上的资料太少了 搞了一个星期了也不知道是哪里的问题?

onCharacteristicChanged   一直没有回调  

调用writeCharacteristic
 发了数据之后从onCharacteristicWrite可以正常回调上来  表示可以发送成功,但不知道 onCharacteristicChanged   不回调就收不到响应的数据

这个问题你解决了吗?我也碰到这个问题了.
明明调用过setCharacteristicNotification方法了,写入指令时,onCharacteristicWrite回调成功了,但onCharacteristicChanged一点反应都没有

楼主,这个问题你解决了吧!说一下怎么解决的?我也遇到这问题了

出现这样的问你就拿一个标准的测试的DEMO去测试,如果用DEMO去测试有数据返回那就是程序的问题了,正常操作是这样的,发现设备 ---连接  -发现服务--遍历服务下的特征   获取特的属性(读 写 notify without response)

同遇到相同的问题,关于onCharacteristicChanged回调需要注意几点:1、在创建某个特征值A时,一定记得设置该特征值为 PROPERTY_NOTIFY;2、在中心设备上在发现A特征值时要设 置:bluetoothGatt.setCharacteristicNotification(A,,true);表示该特征值可以接收通知;3、在外 设中,当A的值改变时要设置 bluetoothGattServer.notifyCharacteristicChanged(device,A,false)表示要发送通知;可 以在onNotificationSend中查看通知是否发送成功;4.最后在中心设备的onCharacteristicChanged中查看是否有回 调;

楼主解决了吗 求指导  我也不进回调    ,还有 你那儿应该是descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);不是notification

楼主解决了吗??求分享,不吝赐教

我们遇到了相同的问题,最后显示是UUID的问题。

同求,麻烦已经解决的大神帮个忙

我解决了这个问题,所以我想可以终结这个帖子了。
首先,请看我的这个总结文章: https://blog.csdn.net/jdfkldjlkjdl/article/details/103925128
重点关注发现服务后的处理逻辑,网上很多文章中获取 BluetoothGattDescriptor 对象是通过这个方法的:

传入的 uuid 形如  00002901-0000-1000-8000-00805f9b34fb 或者  00002902-0000-1000-8000-00805f9b34fb,
然后,也按照网上的方式:
 boolean b1 = descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            if (b1) {
                                mBluetoothGatt.writeDescriptor(descriptor);
                                Log.d(TAG, "描述 UUID :" + descriptor.getUuid().toString());
                            }
这种方式获取的 descriptor, 在某些 BLE 开发板上面,只能发送数据,不能接收数据。所以我们不能用这种方式获取。可以用下面的
lrc下载方式:

这里的 serviceUUID、writeUUID、notifyUUID 由硬件工程师提供,一般都可以拿到的。使用这种方式设置的通知,一般会成功。如果设置成功,会回调这个方法:
  @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            Log.d(TAG, "onDescriptorWrite: " + "设置成功");
        }

好了,应该讲清楚了。具体的可以看上面连接中的源码。
希望帮助到大家解决这个问题,谢谢!

发布了122 篇原创文章 · 获赞 2 · 访问量 5217

猜你喜欢

转载自blog.csdn.net/liuji0517/article/details/105141826