Android BLE(蓝牙低能量)
这段代码有问题:Android BLE(蓝牙低能量),android,Android,这段代码有问题: BluetoothGattCharacteristic charc1 = Service.getCharacteristic(DLE_WRITE_CHAR); if (charc1 == null) { Log.d(TAG, "char not found!"); Log.d(TAG, "CHARAC_-TRST" + charc1.getUuid()); // return false; } //
BluetoothGattCharacteristic charc1 = Service.getCharacteristic(DLE_WRITE_CHAR);
if (charc1 == null) {
Log.d(TAG, "char not found!");
Log.d(TAG, "CHARAC_-TRST" + charc1.getUuid());
// return false;
}
// charc1.setValue(new byte[]{0x00, 0x05, 0x10, 0x01, 0x3E, 0x01, 0x23});
charc1.setValue(data);
boolean stat = mBluetoothGatt.writeCharacteristic(charc1);
Log.d(TAG, "FINISHED WRITTING CHAR 1 status write :(status)" + stat);
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for (byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
}
}
服务为null,因此services.getCharacteristics会引发NPE
请调试您的代码,并确保您具有BLUETOOTH_管理员权限。您可以自行决定添加必要的空保护。请提供更多有关正在发生的事情和不起作用的详细信息。我正在获取一个设备列表,并连接两个设备,但我将发送一些文本,然后它会给出错误。您可能需要添加更多信息,而不是代码墙。如果人们需要花太多时间试图理解代码的错误,他们不会帮助你。尝试用特定信息和错误消息编辑您的问题,清楚地指出问题所在。当我调用writeCharacteristics function->GATT server 133或->android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristicjava.util.UUID'时,我面临这两个错误对象引用我放了一些小代码,之前为了便于理解,我把整个代码放在这里,非常感谢,我正确地获取了特征,但是当我调用writeCharacteristics时,它会在一个空对象引用上给出错误,比如:android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristicjava.util.UUID',这个错误来自defaukt blluetooth班级
BluetoothGattCharacteristic charc1 = Service.getCharacteristic(DLE_WRITE_CHAR);
if (charc1 == null) {
Log.d(TAG, "char not found!");
Log.d(TAG, "CHARAC_-TRST" + charc1.getUuid());
// return false;
}
// charc1.setValue(new byte[]{0x00, 0x05, 0x10, 0x01, 0x3E, 0x01, 0x23});
charc1.setValue(data);
boolean stat = mBluetoothGatt.writeCharacteristic(charc1);
Log.d(TAG, "FINISHED WRITTING CHAR 1 status write :(status)" + stat);
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for (byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
}
}
public void displayBleService(List<BluetoothGattService> gattServices) {
Log.d(TAG, "display Services");
List<BluetoothGattCharacteristic> characteristics = new ArrayList<BluetoothGattCharacteristic>();
for (BluetoothGattService services : gattServices) {
Log.d(TAG, "SERVICES == ." + services.getUuid());
characteristics = services.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
Log.d(TAG, "CAHRACTERISTICS == ." + characteristic.getUuid() + "--" + characteristic.getProperties());
//enableNotification(characteristic, true);
}
}
}