我有两个Android应用程序,一个是我的蓝牙服务器,另一个是客户端。在我
发现的服务
我使用特征来发送消息。类似这样:
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHAR_UUID);
characteristic.setValue("START SENDING...".getBytes());
gatt.writeCharacteristic(characteristic);
然后在我的服务器上使用回调方法
OnCharacteristicWriterRequest
被调用。
在这里,我只需记录消息(工作正常),然后在特征中设置一个值并调用
notifyCharacteristicChanged
.类似这样:
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
BluetoothGattCharacteristic characteristic, boolean preparedWrite,
boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic,
preparedWrite, responseNeeded, offset, value);
byte[] bytes = value;
String message = new String(bytes);
Log.d(TAG, message);
String someText = "Some Value";
characteristic.setValue(someText.getBytes());
bluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);
if (responseNeeded) {
bluetoothGattServer.sendResponse(device, requestId,
BluetoothGatt.GATT_SUCCESS, 0,null);
}
}
不幸的是,当上述方法返回时,我出现以下错误:
A/libc:tid 10460(Binder\U 3)中的致命信号11(SIGSEGV),代码1,故障地址0x10
根据一些stackoverflow问题,如果尝试取消引用空指针,但我的变量都不是空的,则会发生此错误。是否有人知道问题出在哪里,或者至少有人知道如何调试它?
编辑:
当我注释掉下面一行代码时,我没有发现这个错误,所以很明显这与此有关,但我仍然不知道到底是什么:
bluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);
编辑2:
这是我在服务器上配置我的特征的方式:
BluetoothGattCharacteristic characteristic = new
BluetoothGattCharacteristic(CHAR_UUID,
BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_BROADCAST,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
BluetoothGattDescriptor descriptor = new
BluetoothGattDescriptor(DESCRIPTOR_UUID,
BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
characteristic.addDescriptor(descriptor);
bluetoothGattService.addCharacteristic(characteristic);