his is just my code. Saw this example somewhere. But it didn't work.
private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");
private static final UUID Battery_Level_UUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothGattService mBluetoothGatt;
...
public void readCustomCharacteristic() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w("Beacons", "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mBatteryService = mBluetoothGatt;
if(mBatteryService == null){
Log.w("Beacons", "Battery BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mBatteryService.getCharacteristic(Battery_Level_UUID);
if(mReadCharacteristic.getStringValue(0) == null){
Log.w("Beacons", "Failed to read characteristic");
} else {
Log.i("Beacons", "Battery Level: " + mReadCharacteristic.getValue());
}
}
To concrete, I get a NullPointerException:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.bluetooth.BluetoothGattCharacteristic.getStringValue(int)' on a null object reference
Maybe I have to implement a complete server, service and broadcastreceiver? Thank you for your help!