List of usage examples for android.bluetooth BluetoothGattCharacteristic getValue
public byte[] getValue()
From source file:Main.java
public static int getBatteryLevel(BluetoothGattCharacteristic characteristic) { return characteristic.getValue()[0] & 0xff; }
From source file:Main.java
/** * Returns the SystemID from the given characteristic * //from w ww. j a v a2 s . c o m * @param characteristic * @return {@link String} */ @SuppressLint("NewApi") public static String getSYSID(BluetoothGattCharacteristic characteristic) { final byte[] data = characteristic.getValue(); final StringBuilder stringBuilder = new StringBuilder(data.length); if (data != null && data.length > 0) { for (byte byteChar : data) stringBuilder.append(String.format("%02X ", byteChar)); } return String.valueOf(stringBuilder); }
From source file:Main.java
/** * Returns the PNP ID from the given characteristic * //from w ww . j a v a2 s.c o m * @param characteristic * @return {@link String} */ @SuppressLint("NewApi") public static String getPNPID(BluetoothGattCharacteristic characteristic) { final byte[] data = characteristic.getValue(); final StringBuilder stringBuilder = new StringBuilder(data.length); if (data != null && data.length > 0) { for (byte byteChar : data) stringBuilder.append(String.format("%02X ", byteChar)); } return String.valueOf(stringBuilder); }
From source file:Main.java
/** * Run actions given that the read operation * of part 1 of the beacon's advertising packet * has completed./* ww w . ja v a 2 s.c om*/ * * @param bluetoothGattCharacteristic */ private static void handleGattCharacteristicRead_beaconDataPart1( BluetoothGattCharacteristic bluetoothGattCharacteristic) { // Store the read value byte array mAdvertisingPacketData_read = bluetoothGattCharacteristic.getValue(); // Read the beacon's advertising packet length readCharacteristic_beaconDataLength(); }
From source file:com.orange.beaconme_sdk.ble.control.BeaconTagDeviceUpdater.java
private byte[] getEnablingValueForChar(BluetoothGattCharacteristic characteristic, boolean enable) { byte[] newValue = Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length); if (characteristic.getUuid().equals(BeaconTagDevice.SLEEP_CHARACTERISTIC_UUID)) { newValue[0] = 0;// w w w . j av a2s .c o m newValue[1] = 0; } else { newValue[0] = (byte) (enable ? 1 : 0); } return newValue; }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.InitOperation.java
@Override public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { UUID characteristicUUID = characteristic.getUuid(); if (MiBand2Service.UUID_CHARACTERISTIC_AUTH.equals(characteristicUUID)) { try {/*from ww w. j ava2 s . c o m*/ byte[] value = characteristic.getValue(); getSupport().logMessageContent(value); if (value[0] == MiBand2Service.AUTH_RESPONSE && value[1] == MiBand2Service.AUTH_SEND_KEY && value[2] == MiBand2Service.AUTH_SUCCESS) { TransactionBuilder builder = createTransactionBuilder("Sending the secret key to the band"); builder.write(characteristic, requestAuthNumber()); getSupport().performImmediately(builder); } else if (value[0] == MiBand2Service.AUTH_RESPONSE && value[1] == MiBand2Service.AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == MiBand2Service.AUTH_SUCCESS) { // md5?? byte[] eValue = handleAESAuth(value, getSecretKey()); byte[] responseValue = org.apache.commons.lang3.ArrayUtils.addAll( new byte[] { MiBand2Service.AUTH_SEND_ENCRYPTED_AUTH_NUMBER, MiBand2Service.AUTH_BYTE }, eValue); TransactionBuilder builder = createTransactionBuilder( "Sending the encrypted random key to the band"); builder.write(characteristic, responseValue); getSupport().setCurrentTimeWithService(builder); getSupport().performImmediately(builder); } else if (value[0] == MiBand2Service.AUTH_RESPONSE && value[1] == MiBand2Service.AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == MiBand2Service.AUTH_SUCCESS) { TransactionBuilder builder = createTransactionBuilder("Authenticated, now initialize phase 2"); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext())); getSupport().requestDeviceInfo(builder); getSupport().phase2Initialize(builder); getSupport().setInitialized(builder); getSupport().performImmediately(builder); } else { return super.onCharacteristicChanged(gatt, characteristic); } } catch (Exception e) { GB.toast(getContext(), "Error authenticating Mi Band 2", Toast.LENGTH_LONG, GB.ERROR, e); } return true; } else { LOG.info("Unhandled characteristic changed: " + characteristicUUID); return super.onCharacteristicChanged(gatt, characteristic); } }
From source file:com.megster.cordova.rfduino.Peripheral.java
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); if (characteristic.getUuid().equals(RECEIVE_CHARACTERISTIC_UUID)) { PluginResult result = new PluginResult(PluginResult.Status.OK, characteristic.getValue()); result.setKeepCallback(true);/*from w w w .j a v a 2 s .c om*/ onDataCallback.sendPluginResult(result); } }
From source file:com.github.w666.ezonwatch.BluetoothLeService.java
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { intent.putExtra(EXTRA_DATA, data); }// www . j av a 2 s. c o m sendBroadcast(intent); }
From source file:com.orange.beaconme_sdk.ble.control.BeaconTagDeviceUpdater.java
private void onReadCharacteristic(BluetoothGattCharacteristic characteristic, BluetoothGatt gatt, int status) { Log.i("Updater", "onReadCharacteristic"); UUID charUUID = characteristic.getUuid(); for (WriteCharacteristicCommand command : commands) { if (command.getCharacteristicUUID().equals(charUUID)) { if (characteristic.getValue() == null) { Log.e(TAG, String.format("read characteristic %s value=%s, status=%d", characteristic.getUuid(), Arrays.toString(characteristic.getValue()), status)); } else { byte[] newValue; if (command.getSwitchState() != WriteCharacteristicCommand.SwitchState.NONE) { newValue = getEnablingValueForChar(characteristic, command.getSwitchState() == WriteCharacteristicCommand.SwitchState.ENABLE); } else { newValue = command.getBytesToUpload(); }/*from w ww.j a v a2s . c om*/ if (!Arrays.equals(characteristic.getValue(), newValue)) { characteristic.setValue(newValue); queue(getWriteCharacteristicOperation(characteristic)); } else { doneUploadingUuid(characteristic.getUuid()); } } } } }
From source file:com.lef.ibeacon.service.UpdateService.java
public static int decodeUInt16(final BluetoothGattCharacteristic characteristic, final int offset) { final byte[] data = characteristic.getValue(); return (unsignedByteToInt(data[offset]) << 8) | unsignedByteToInt(data[offset + 1]); }