Android BLE 蓝牙onCharacteristicChanged 方法不调用

xb12321 2016-01-06 12:31:36
最近接了一个新项目,但是对于蓝牙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 并不回调
麻烦大家帮我看看。。。。谢过了。。。
...全文
6260 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
AndroidDev2022 2020-01-10
  • 打赏
  • 举报
回复 2
我解决了这个问题,所以我想可以终结这个帖子了。 首先,请看我的这个总结文章: 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 开发板上面,只能发送数据,不能接收数据。所以我们不能用这种方式获取。可以用下面的方式: 这里的 serviceUUID、writeUUID、notifyUUID 由硬件工程师提供,一般都可以拿到的。使用这种方式设置的通知,一般会成功。如果设置成功,会回调这个方法: @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorWrite(gatt, descriptor, status); Log.d(TAG, "onDescriptorWrite: " + "设置成功"); } 好了,应该讲清楚了。具体的可以看上面连接中的源码。 希望帮助到大家解决这个问题,谢谢!
  • 打赏
  • 举报
回复
同求,麻烦已经解决的大神帮个忙
Yinlili2333 2017-03-27
  • 打赏
  • 举报
回复
我们遇到了相同的问题,最后显示是UUID的问题。
yupan_93 2017-01-06
  • 打赏
  • 举报
回复
楼主解决了吗??求分享,不吝赐教
botcott 2016-11-24
  • 打赏
  • 举报
回复
楼主解决了吗 求指导 我也不进回调 ,还有 你那儿应该是descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);不是notification
ZyCh1415 2016-06-14
  • 打赏
  • 举报
回复
同遇到相同的问题,关于onCharacteristicChanged回调需要注意几点:1、在创建某个特征值A时,一定记得设置该特征值为PROPERTY_NOTIFY;2、在中心设备上在发现A特征值时要设置:bluetoothGatt.setCharacteristicNotification(A,,true);表示该特征值可以接收通知;3、在外设中,当A的值改变时要设置bluetoothGattServer.notifyCharacteristicChanged(device,A,false)表示要发送通知;可以在onNotificationSend中查看通知是否发送成功;4.最后在中心设备的onCharacteristicChanged中查看是否有回调;
帝都-辉 2016-05-20
  • 打赏
  • 举报
回复
出现这样的问你就拿一个标准的测试的DEMO去测试,如果用DEMO去测试有数据返回那就是程序的问题了,正常操作是这样的,发现设备 ---连接 -发现服务--遍历服务下的特征 获取特的属性(读 写 notify without response)
住哪儿啊 2016-04-27
  • 打赏
  • 举报
回复
楼主,这个问题你解决了吧!说一下怎么解决的?我也遇到这问题了
Chase24 2016-04-14
  • 打赏
  • 举报
回复 1
这个问题你解决了吗?我也碰到这个问题了. 明明调用过setCharacteristicNotification方法了,写入指令时,onCharacteristicWrite回调成功了,但onCharacteristicChanged一点反应都没有
iegwwxf_sz 2016-01-14
  • 打赏
  • 举报
回复 1
引用 1 楼 iegwwxf_sz 的回复:
你的问题解决了吗? 我也遇到了一样的问题setCharacteristicNotification始終不回调? 网上的资料太少了 搞了一个星期了也不知道是哪里的问题?
onCharacteristicChanged 一直没有回调 调用writeCharacteristic 发了数据之后从onCharacteristicWrite可以正常回调上来 表示可以发送成功,但不知道 onCharacteristicChanged 不回调就收不到响应的数据
iegwwxf_sz 2016-01-14
  • 打赏
  • 举报
回复 3
你的问题解决了吗? 我也遇到了一样的问题setCharacteristicNotification始終不回调? 网上的资料太少了 搞了一个星期了也不知道是哪里的问题?

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧