@Override public String toString() { if (size() == 0) return ""; final char[] out = new char[mValue.length * 3 - 1]; for (int j = 0; j < mValue.length; j++) { int v = mValue[j] & 0xFF; out[j * 3] = HEX_ARRAY[v >>> 4]; out[j * 3 + 1] = HEX_ARRAY[v & 0x0F]; if (j != mValue.length - 1) out[j * 3 + 2] = '-'; } return "(0x) " + new String(out); }
/** * Returns a byte at the given offset from the byte array. * * @param offset Offset at which the byte value can be found. * @return Cached value or null of offset exceeds value size. */ @Nullable public Byte getByte(@IntRange(from = 0) final int offset) { if (offset + 1 > size()) return null; return mValue[offset]; }
@Override public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { if (data.size() < 2) { onInvalidDataReceived(device, data); return; } // Read flags int offset = 0; final int flags = data.getIntValue(Data.FORMAT_UINT8, offset); final int hearRateType = (flags & 0x01) == 0 ? Data.FORMAT_UINT8 : Data.FORMAT_UINT16; offset += 1; // Validate packet length. The type's lower nibble is its length. if (data.size() < 1 + (hearRateType & 0x0F)) { onInvalidDataReceived(device, data); return; } final int value = data.getIntValue(hearRateType, offset); // offset += hearRateType & 0xF; // ... // Report the parsed value(s) onSampleValueReceived(device, value); } }
public Float getFloatValue(@FloatFormat final int formatType, @IntRange(from = 0) final int offset) { if ((offset + getTypeLen(formatType)) > size()) return null;
/** * Returns a long value from the byte array. * <p>Only {@link #FORMAT_UINT32} and {@link #FORMAT_SINT32} are supported. * <p>The formatType parameter determines how the value * is to be interpreted. For example, setting formatType to * {@link #FORMAT_UINT32} specifies that the first four bytes of the * value at the given offset are interpreted to generate the * return value. * * @param formatType The format type used to interpret the value. * @param offset Offset at which the integer value can be found. * @return Cached value or null of offset exceeds value size. */ @Nullable public Long getLongValue(@LongFormat final int formatType, @IntRange(from = 0) final int offset) { if ((offset + getTypeLen(formatType)) > size()) return null; switch (formatType) { case FORMAT_SINT32: return unsignedToSigned(unsignedBytesToLong(mValue[offset], mValue[offset + 1], mValue[offset + 2], mValue[offset + 3]), 32); case FORMAT_UINT32: return unsignedBytesToLong(mValue[offset], mValue[offset + 1], mValue[offset + 2], mValue[offset + 3]); } return null; }
/** * Reads the battery level from the device. * * @deprecated Use {@link #readCharacteristic(BluetoothGattCharacteristic)} instead. */ @SuppressWarnings("ConstantConditions") @Deprecated protected void readBatteryLevel() { Request.newReadBatteryLevelRequest().setManager(this) .with((device, data) -> { if (data.size() == 1) { final int batteryLevel = data.getIntValue(Data.FORMAT_UINT8, 0); log(Log.INFO, "Battery Level received: " + batteryLevel + "%"); mBatteryValue = batteryLevel; final BleManagerGattCallback callback = mGattCallback; if (callback != null) { callback.onBatteryValueReceived(mBluetoothGatt, batteryLevel); } mCallbacks.onBatteryValueReceived(device, batteryLevel); } }) .enqueue(); }
@Override protected void onDeviceReady() { super.onDeviceReady(); // Initialization is now ready. // The service or activity has been notified with TemplateManagerCallbacks#onDeviceReady(). // TODO Do some extra logic here, of remove onDeviceReady(). // Device is ready, let's read something here. Usually there is nothing else to be done // here, as all had been done during initialization. readCharacteristic(mOptionalCharacteristic) .with((device, data) -> { // Characteristic value has been read // Let's do some magic with it. if (data.size() > 0) { final Integer value = data.getIntValue(Data.FORMAT_UINT8, 0); log(LogContract.Log.Level.APPLICATION, "Value '" + value + "' has been read!"); } else { log(Log.WARN, "Value is empty!"); } }) .enqueue(); } };
/** * This method enables notifications on the Battery Level characteristic. * * @deprecated Use {@link #setNotificationCallback(BluetoothGattCharacteristic)} and * {@link #enableNotifications(BluetoothGattCharacteristic)} instead. */ @SuppressWarnings("ConstantConditions") @Deprecated protected void enableBatteryLevelNotifications() { if (mBatteryLevelNotificationCallback == null) { mBatteryLevelNotificationCallback = new ValueChangedCallback() .with((device, data) -> { if (data.size() == 1) { final int batteryLevel = data.getIntValue(Data.FORMAT_UINT8, 0); mBatteryValue = batteryLevel; final BleManagerGattCallback callback = mGattCallback; if (callback != null) { callback.onBatteryValueReceived(mBluetoothGatt, batteryLevel); } mCallbacks.onBatteryValueReceived(device, batteryLevel); } }); } Request.newEnableBatteryLevelNotificationsRequest().setManager(this) .done(device -> log(Log.INFO, "Battery Level notifications enabled")) .enqueue(); }
public Integer getIntValue(@IntFormat final int formatType, @IntRange(from = 0) final int offset) { if ((offset + getTypeLen(formatType)) > size()) return null;
/** * This method will write important data to the device. * * @param parameter parameter to be written. */ void performAction(final String parameter) { log(Log.VERBOSE, "Changing device name to \"" + parameter + "\""); // Write some data to the characteristic. writeCharacteristic(mDeviceNameCharacteristic, Data.from(parameter)) // If data are longer than MTU-3, they will be chunked into multiple packets. // Check out other split options, with .split(...). .split() // Callback called when data were sent, or added to outgoing queue in case // Write Without Request type was used. .with((device, data) -> log(Log.DEBUG, data.size() + " bytes were sent")) // Callback called when data were sent, or added to outgoing queue in case // Write Without Request type was used. This is called after .with(...) callback. .done(device -> log(LogContract.Log.Level.APPLICATION, "Device name set to \"" + parameter + "\"")) // Callback called when write has failed. .fail((device, status) -> log(Log.WARN, "Failed to change device name")) .enqueue(); } }