在发送命令之后,onCharacteristicChanged这个回调里面接收不到数据
打开通知
/*打开广播通知*/
private void notifyData() {
BluetoothGattCharacteristic characteristic = bluetoothGatt.getService(N_Notify).getCharacteristic(uNotify);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
bluetoothGatt.setCharacteristicNotification(characteristic, true);
}
发送数据
public void writeData() {
if (bluetoothGatt == null) {
return;
}
BluetoothGattCharacteristic characteristic = bluetoothGatt.getService(N_Write).getCharacteristic(uWrite);
characteristic.setValue("EF52FFFAFFFF");
bluetoothGatt.writeCharacteristic(characteristic);
}
接收通知
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
bluetoothGatt = gatt;
byte[] value = characteristic.getValue();
try {
String s=new String(value,"UTF-8");
Log.e(TAG, "onCharacteristicChanged: "+s);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
printNotify(value);
}