我扫描了蓝牙,链接并发送了启动蓝牙设备测量功能的指令,发现了以下错误:
03-15 11:12:44.903 22685-22685/com.homehealth.bluetooth_one E/BluetoothGatt: android.os.DeadObjectException
at android.os.BinderProxy.transact(Native Method)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.writeCharacteristic(IBluetoothGatt.java:933)
at android.bluetooth.BluetoothGatt.writeCharacteristic(BluetoothGatt.java:952)
at com.homehealth.bluetooth_one.BluetoothLeService.wirteCharacteristic(BluetoothLeService.java:283)
at 当我将蓝牙连接到我的手机时,我想要发送CMD到蓝牙设备并获取特征。我使用的方法是mBluetoothGatt.writeCharacteristic(characteristic),mBluetoothGatt.readCharacteristic(characteristic),mBluetoothGatt.setCharacteristicNotification(characteristic, enabled),It is error。myCode:
活动:
if (uuid.contains("fff3")){
mBluetoothLeService.wirteCharacteristic(gattCharacteristic);
mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);
mBluetoothLeService.readCharacteristic(gattCharacteristic);
}服务类别为:
public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean success = mBluetoothGatt.writeCharacteristic(characteristic);
if (success) Log.i(TAG,"wirteCharacteristic successful");
}
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoohAdapter not initialized");
return;
}
Log.i(TAG,"readCharacteristic UUID="+characteristic.getUuid().toString());
boolean success = mBluetoothGatt.readCharacteristic(characteristic);
if (success) Log.i(TAG,"readCharacteristic successful");
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean successful = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
mBluetoothGatt.writeCharacteristic(characteristic);
if (successful) {
Log.i(TAG,"setCharacteristicNotification successful");
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(characteristic.getUuid());
if (null == descriptor) {
Log.e(TAG, "descriptor is null,or UUID is invalid uuid=" + characteristic.getUuid());
return;
}
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}发布于 2017-03-17 21:37:43
这就是mBluetoothGatt.writeCharacteristic(characteristic).的问题特征不是设置值。
正确的代码是:
characteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(characteristic);https://stackoverflow.com/questions/42800652
复制相似问题