You have to first discover all services for the given device, otherwise when you run BluetoothGattService service = gatt.getService(getServiceUUID());
it will return null.
I would recommend you add the onServicesDiscovered
function and use gatt.discoverServices();
instead.
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
for (BluetoothGattService gattService : gattServices) {
Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
}
}
}