这个问题卡了我两天了,一直找不到原因。 我的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方法,始终不执行。 麻烦大家帮我看看怎么回事? 这是公司的项目,已经卡在这里两天了,十万火急啊
下面是我的代码: @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方法,始终不执行。 麻烦大家帮我看看怎么回事? 这是公司的项目,已经卡在这里两天了,十万火急啊
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货