我正在尝试与Laird BL652 BLE模块建立通信。到目前为止,我已经获得了用于vSP(虚拟串行协议)的RX和TX的UUID,并且可以成功将数据发送到模块,但是,我希望模块在发送特定字符后将数据发送回我。 / p>
我的主要活动是这样:
mBluetoothLeService.setCharacteristicNotification(characteristicRX, true);
这是Google的BluetoothLeService库中的代码,用于设置特征通知
/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
问题是,当我设置特征通知时,我的应用程序崩溃了,并发出了此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.a.a.BluetoothLeService.setCharacteristicNotification(android.bluetooth.BluetoothGattCharacteristic.boolean)' on a null object reference
此外,我尝试在主活动中设置描述符,但是它说即使导入了android.bluetooth.BluetoothGattDescriptor,也无法识别mBluetoothLeService.writeDescriptor(descriptor)。