在做Android蓝牙BLE4.0开发时,onCharacteristicChanged方法一直不调用,是怎么回事呢? 30C

鹏仔学java 2016-05-28 02:07:27
这个问题卡了我两天了,一直找不到原因。 我的BLE设备已经连接成功,数据写入也是成功的,设备通知功能也已经打开,可是onCharacteristicChanged这个方法一直无法调用。 各位大神帮我看看啊,有做BLE相关开发的可以加我QQ:879981004,我们一起探讨下。


下面是我的代码:

@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
super.onServicesDiscovered(gatt, status);
Log.i("inform", "连接已经成功,正在写入数据。。。");
// readData();
btnNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status == mBluetoothGatt.GATT_SUCCESS) {
final List<BluetoothGattService> services = gatt.getServices();
new Thread(new Runnable() {
@Override
public void run() {
for (BluetoothGattService bluetoothGattService : services) {
if (bluetoothGattService.getUuid().toString().equals("0000fff0-0000-1000-8000-00805f9b34fb")) {
for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattService.getCharacteristics()) {
if (bluetoothGattCharacteristic.getUuid().toString().equals("0000fff1-0000-1000-8000-00805f9b34fb")) {
try {
Thread.sleep(500);
enableNotification(true, bluetoothGattCharacteristic);
Thread.sleep(500);
writeWDData();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}).start();
}
}
});
}

我在onServicesDiscovered中设置通知打开,然后进行数据写入。在onCharacteristicWrite中,是可以正常调用的,说明数据写入是成功的啊。这里的enableNotification(true, bluetoothGattCharacteristic); 就是设置打开设备通知功能
/**
* 通知
*/
private void enableNotification(boolean enable, BluetoothGattCharacteristic characteristic) {
if (mBluetoothGatt == null || characteristic == null) {
return;
}
if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable)) {
return;
}
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (clientConfig == null) {
return;
}
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(clientConfig);
// }
}
下面是我写入数据的代码
/**
* 向蓝牙设备写入温湿度数据
*/
public void writeWDData() {
BluetoothGattService sendService = mBluetoothGatt.getService(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"));
if (sendService != null) {
BluetoothGattCharacteristic sendCharacteristic = sendService.getCharacteristic(UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb"));
if (sendCharacteristic != null) {
byte[] bytes = new byte[5];
bytes[0] = 0x30;
bytes[1] = 'b';
bytes[2] = 'b';
bytes[3] = 'b';
bytes[4] = 'b';
sendCharacteristic.setValue(bytes);
// sendCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
mBluetoothGatt.writeCharacteristic(sendCharacteristic);//写命令到设备,
Log.i("inform", "数据已写入");
}
} else {
Log.i("inform", "UUID错误。。。");
// String name= BluetoothDevice.EXTRA_UUID;
// Log.i("inform",name+">>>>>>>>name");
}
}

就是这个返回通知的方法,一直不执行:
/**
* 蓝牙通知回调
* @param gatt
* @param characteristic
*/
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i("inform","已通知成功");

}

不是说在数据写入成功后,蓝牙设备会给手机发送通知嘛?可是我已经写入成功了,但是就是在最后这里的用来返回通知的onCharacteristicChanged方法,始终不执行。 麻烦大家帮我看看怎么回事? 这是公司的项目,已经卡在这里两天了,十万火急啊。。。
...全文
1639 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hl763785813 2018-03-30
  • 打赏
  • 举报
回复
楼主刚接触蓝牙,和你出现了同样的问题。请问最后您是怎么解决的。现在卡在这里好久了
铁掌码上飘 2017-09-14
  • 打赏
  • 举报
回复
在进行写入数据之前应该调用gatt.setCharacteristicNotification(characteristic,true) 才能接收到返回来的数据吧
  • 打赏
  • 举报
回复
楼主有没有解决,我也出现这种情况了,求指教 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
小小Q_Forler 2017-01-22
  • 打赏
  • 举报
回复
楼主有没有解决,我也出现这种情况了,求指教
lirui319 2017-01-07
  • 打赏
  • 举报
回复
请问楼主这个问题搞清楚了没有,我也是出现了这种问题,感觉都是坑,如果解决了请指点一下,谢谢!!

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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