Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#getValue()
The following examples show how to use
android.bluetooth.BluetoothGattCharacteristic#getValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SensorTagMovementProfile.java From SensorTag-CC2650 with Apache License 2.0 | 6 votes |
@Override public void didUpdateValueForCharacteristic(BluetoothGattCharacteristic c) { byte[] value = c.getValue(); if (c.equals(this.dataC)){ Point3D v; v = Sensor.MOVEMENT_ACC.convert(value); if (this.tRow.config == false) this.tRow.value.setText(Html.fromHtml(String.format("<font color=#FF0000>X:%.2fG</font>, <font color=#00967D>Y:%.2fG</font>, <font color=#00000>Z:%.2fG</font>", v.x, v.y, v.z))); this.tRow.sl1.addValue((float)v.x); this.tRow.sl2.addValue((float)v.y); this.tRow.sl3.addValue((float)v.z); v = Sensor.MOVEMENT_GYRO.convert(value); SensorTagMovementTableRow row = (SensorTagMovementTableRow)this.tRow; row.gyroValue.setText(Html.fromHtml(String.format("<font color=#FF0000>X:%.2f°/s</font>, <font color=#00967D>Y:%.2f°/s</font>, <font color=#00000>Z:%.2f°/s</font>", v.x, v.y, v.z))); row.sl4.addValue((float)v.x); row.sl5.addValue((float)v.y); row.sl6.addValue((float)v.z); v = Sensor.MOVEMENT_MAG.convert(value); row.magValue.setText(Html.fromHtml(String.format("<font color=#FF0000>X:%.2fuT</font>, <font color=#00967D>Y:%.2fuT</font>, <font color=#00000>Z:%.2fuT</font>", v.x, v.y, v.z))); row.sl7.addValue((float)v.x); row.sl8.addValue((float)v.y); row.sl9.addValue((float)v.z); } }
Example 2
Source File: CharacteristicDetailActivity.java From AndroidBleManager with Apache License 2.0 | 6 votes |
@Subscribe(threadMode = ThreadMode.MAIN) public void onEventReceiveNotify(UpdateEvent event){ if (event != null && event.getType() == UpdateEvent.Type.BLE_DATA){ BluetoothGattCharacteristic characteristic = (BluetoothGattCharacteristic) event.getObj(); final byte[] dataArr = characteristic.getValue(); final String flag = event.getMsg(); if (dataArr != null && dataArr.length > 0){ if (flag.equals("read")){ mTvReadValue.setText("byte:"+ ByteUtils.byteArrayToHexString(dataArr)+" ,string:"+ByteUtils.byteArrayToHexString(dataArr)); }else if(flag.equals("write")){ mTvWriteValue.setText("byte:"+ByteUtils.byteArrayToHexString(dataArr)+" ,string:"+ ByteUtils.byteArrayToHexString(dataArr)); }else if(flag.equals("notify")){ notifyListAdapter.addHead(ByteUtils.byteArrayToHexString(dataArr)); }else{ Toast.makeText(CharacteristicDetailActivity.this, "Fail to operator info", Toast.LENGTH_LONG).show(); } }else { Toast.makeText(CharacteristicDetailActivity.this, "Chara data is null", Toast.LENGTH_LONG).show(); } } }
Example 3
Source File: MetawearSensor.java From BleSensorTag with MIT License | 6 votes |
@Override protected boolean apply(BluetoothGattCharacteristic c, Metawear metawear) { final String uuid = c.getUuid().toString(); switch (uuid) { case UUID_DATA: final byte[] data = c.getValue(); final ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); final byte id = buffer.get(); if (id != getServiceId()) { return false; } final byte op = buffer.get(); return processData(op, buffer, metawear); default: return false; } }
Example 4
Source File: PinIOActivity.java From Bluefruit_LE_Connect_Android with MIT License | 6 votes |
@Override public void onDataAvailable(BluetoothGattCharacteristic characteristic) { byte[] data = characteristic.getValue(); Log.d(TAG, "received: " + BleUtils.bytesToHexWithSpaces(data)); switch (mUartStatus) { case kUartStatus_QueryCapabilities: receivedQueryCapabilities(data); break; case kUartStatus_QueryAnalogMapping: receivedAnalogMapping(data); break; default: receivedPinState(data); break; } }
Example 5
Source File: BleGattImpl.java From EasyBle with Apache License 2.0 | 6 votes |
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { String address = gatt.getDevice().getAddress(); String serviceUuid = characteristic.getService().getUuid().toString(); String characteristicUuid = characteristic.getUuid().toString(); OperationIdentify identify = getOperationIdentifyFromMap(mReadCallbackMap, address, serviceUuid, characteristicUuid); if (identify == null) { return; } final BleReadCallback callback = mReadCallbackMap.get(identify); final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); final byte[] data = characteristic.getValue(); mHandler.post(new Runnable() { @Override public void run() { if (callback != null) { callback.onReadSuccess(data, device); } } }); } }
Example 6
Source File: TIMagnetometerTranslator.java From ibm-wearables-android-sdk with Apache License 2.0 | 6 votes |
@Override public MagnetometerData translate(BluetoothGattCharacteristic c) { MagnetometerData data; byte[] value = c.getValue(); final float SCALE = (float) (32768 / 4912); if (value.length >= 18) { float x = ((value[13] << 8) + value[12]) / SCALE; float y = ((value[15] << 8) + value[14]) / SCALE; float z = ((value[17] << 8) + value[16]) / SCALE; data = new MagnetometerData(x, y, z); } else{ data = new MagnetometerData(0,0,0); } return data; }
Example 7
Source File: BleManager.java From BLEChat with GNU General Public License v3.0 | 6 votes |
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { // We've received data from remote Logs.d(TAG, "# onCharacteristicChanged: "+characteristic.toString()); final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { final StringBuilder stringBuilder = new StringBuilder(data.length); //for(byte byteChar : data) // stringBuilder.append(String.format("%02X ", byteChar)); stringBuilder.append(data); Logs.d(TAG, stringBuilder.toString()); mHandler.obtainMessage(MESSAGE_READ, new String(data)).sendToTarget(); } if(mDefaultChar == null && isWritableCharacteristic(characteristic)) { mDefaultChar = characteristic; } }
Example 8
Source File: BluetoothLeService.java From IoT-Firstep with GNU General Public License v3.0 | 5 votes |
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); // This is special handling for the Heart Rate Measurement profile. Data parsing is // carried out as per profile specifications: // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; Log.d(TAG, "Heart rate format UINT16."); } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; Log.d(TAG, "Heart rate format UINT8."); } final int heartRate = characteristic.getIntValue(format, 1); Log.d(TAG, String.format("Received heart rate: %d", heartRate)); intent.putExtra(EXTRA_DATA, String.valueOf(heartRate)); } else { // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); //System.out.println("LeService ---->" + new String(data)); if (data != null && data.length > 0) { intent.putExtra(EXTRA_DATA, data); } } sendBroadcast(intent); }
Example 9
Source File: SerialInterface_BLE.java From PodEmu with GNU General Public License v3.0 | 5 votes |
@Override @TargetApi(21) // Result of a characteristic read operation public synchronized void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { final UUID uuid = characteristic.getUuid(); // final int j=i; // i++; if(CHARACTERISTIC_SERIAL_UUID.equals(uuid)) { final byte[] bytes = characteristic.getValue(); Runnable runnable = new Runnable() { @Override public void run() { //PodEmuLog.debug("SIBLE: GATT onCharacteristicChanged for " + uuid); //PodEmuLog.debug("SIBLE: received: " + (new String(bytes))); try { mInBuffer.add(bytes, bytes.length); // PodEmuLog.debug("SIBLE: GATT received " + bytes.duration + " bytes: " + OAPMessenger.oap_hex_to_str(bytes, bytes.duration) + " - " + j); } catch (InterruptedException e) { PodEmuLog.error("SIBLE: something wrong happen. Adding bytes to mInBuffer interrupted."); } } }; //PodEmuLog.debug("SIBLE: GATT received " + bytes.duration + " bytes: " + OAPMessenger.oap_hex_to_str(bytes, bytes.duration) + " - " + j + " - PRE!!!"); readExecutorService.execute(runnable); } }
Example 10
Source File: UpdateService.java From Android-nRF-Beacon with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Obtains the cached value of the Manufacturer ID characteristic. If the value has not been obtained yet using {@link #read()}, or the characteristic has not been found on the beacon, * <code>null</code> is returned. * * @return the Manufacturer ID or <code>null</code> */ public Integer getManufacturerId() { final BluetoothGattCharacteristic characteristic = mManufacturerIdCharacteristic; if (characteristic != null) { final byte[] data = characteristic.getValue(); if (data == null || data.length < 2) return null; return characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0); } return null; }
Example 11
Source File: GattManager.java From thunderboard-android with Apache License 2.0 | 5 votes |
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); Timber.d("characteristic: %s status: %d", characteristic.getUuid().toString(), status); byte[] ba = characteristic.getValue(); if (ba == null || ba.length == 0) { Timber.d("characteristic: %s is not initialized", characteristic.getUuid().toString()); } else { ThunderBoardDevice device = bleManager.getDeviceFromCache(gatt.getDevice().getAddress()); if (ThunderBoardUuids.UUID_CHARACTERISTIC_DIGITAL.equals(characteristic.getUuid())) { device.getSensorIo().setLed(ba[0]); bleManager.selectedDeviceMonitor.onNext(device); } else if (ThunderBoardUuids.UUID_CHARACTERISTIC_CALIBRATE.equals(characteristic.getUuid())) { if (ba[0] == 0x01) { Timber.d("onCharacteristicWrite startCalibration value: %02x, length: %d," + " ACTION: ACTION_CALIBRATE", ba[0], ba .length); bleManager.motionDetector.onNext(new MotionEvent(device, characteristic .getUuid(), MotionEvent.ACTION_CALIBRATE)); return; } else if (ba[0] == 0x02) { Timber.d("onCharacteristicWrite startCalibration value: %02x, length: %d," + " ACTION: ACTION_CLEAR_ORIENTATION", ba[0], ba .length); bleManager.motionDetector.onNext(new MotionEvent(device, characteristic .getUuid(), MotionEvent.ACTION_CLEAR_ORIENTATION)); return; } Timber.d("onCharacteristicWrite startCalibration value: %02x, length: %d," + " ACTION: UNKOWN", ba[0], ba .length); } else if (ThunderBoardUuids.UUID_CHARACTERISTIC_HALL_CONTROL_POINT.equals(characteristic.getUuid())) { boolean read = BleUtils.readCharacteristic(gatt, ThunderBoardUuids.UUID_SERVICE_HALL_EFFECT, ThunderBoardUuids.UUID_CHARACTERISTIC_HALL_STATE); Timber.d("Characteristic read successful: %s", read); } } }
Example 12
Source File: BluetoothLeService.java From IoT-Firstep with GNU General Public License v3.0 | 5 votes |
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); // This is special handling for the Heart Rate Measurement profile. Data parsing is // carried out as per profile specifications: // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; Log.d(TAG, "Heart rate format UINT16."); } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; Log.d(TAG, "Heart rate format UINT8."); } final int heartRate = characteristic.getIntValue(format, 1); Log.d(TAG, String.format("Received heart rate: %d", heartRate)); intent.putExtra(EXTRA_DATA, String.valueOf(heartRate)); } else { // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); //System.out.println("LeService ---->" + new String(data)); if (data != null && data.length > 0) { intent.putExtra(EXTRA_DATA, data); } } sendBroadcast(intent); }
Example 13
Source File: DeviceInformationPeripheralService.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 5 votes |
@Override public void saveValues(@NonNull Context context) { super.saveValues(context); SharedPreferences.Editor preferencesEditor = context.getSharedPreferences(kPreferences, MODE_PRIVATE).edit(); List<BluetoothGattCharacteristic> characteristics = mService.getCharacteristics(); for (BluetoothGattCharacteristic characteristic : characteristics) { byte[] data = characteristic.getValue(); if (data != null) { String encodedData = Base64.encodeToString(data, Base64.NO_WRAP); preferencesEditor.putString(characteristic.getUuid().toString(), encodedData); } } preferencesEditor.apply(); }
Example 14
Source File: UpdateService.java From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Obtains the cached value of the Public ECDH Key characteristic. If the value has not been obtained yet using {@link #read()}, or the characteristic has not been found on the beacon, * <code>null</code> is returned. * * @return the Public ECDH Key or <code>null</code> */ public byte[] getBeaconPublicEcdhKey() { final BluetoothGattCharacteristic characteristic = mPublicEcdhKeyCharacteristic; if (characteristic != null) { final byte[] data = characteristic.getValue(); if (data == null || data.length == 0) return null; return characteristic.getValue(); } return null; }
Example 15
Source File: BluetoothLeService.java From BLEConnect with GNU General Public License v2.0 | 5 votes |
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); // This is special handling for the Heart Rate Measurement profile. Data // parsing is // carried out as per profile specifications: // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; Log.d(TAG, "Heart rate format UINT16."); } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; Log.d(TAG, "Heart rate format UINT8."); } final int heartRate = characteristic.getIntValue(format, 1); Log.d(TAG, String.format("Received heart rate: %d", heartRate)); intent.putExtra(EXTRA_DATA, String.valueOf(heartRate)); } else { // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { final StringBuilder stringBuilder = new StringBuilder( data.length); for (byte byteChar : data) stringBuilder.append(String.format("%02X ", byteChar)); intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); } } sendBroadcast(intent); }
Example 16
Source File: ShareTest.java From xDrip with GNU General Public License v3.0 | 5 votes |
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Log.i(TAG, "Characteristic Read"); byte[] value = characteristic.getValue(); if(value != null) { Log.i(TAG, "VALUE" + value); } else { Log.w(TAG, "Characteristic was null"); } nextGattStep(); } else { Log.w(TAG, "Characteristic failed to read"); } }
Example 17
Source File: P_BleDevice_Listeners.java From SweetBlue with GNU General Public License v3.0 | 5 votes |
@Override public final void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int gattStatus) { final byte[] data = characteristic.getValue(); m_device.getManager().getPostManager().runOrPostToUpdateThread(new Runnable() { @Override public void run() { onCharacteristicWrite_updateThread(gatt, characteristic, data, gattStatus); } }); }
Example 18
Source File: G5CollectionService.java From xDrip with GNU General Public License v3.0 | 4 votes |
private synchronized void processRxCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.i(TAG, "onCharacteristicChanged On Main Thread? " + isOnMainThread()); Log.e(TAG, "CharBytes-nfy" + Arrays.toString(characteristic.getValue())); Log.i(TAG, "CharHex-nfy" + Extensions.bytesToHex(characteristic.getValue())); byte[] buffer = characteristic.getValue(); byte firstByte = buffer[0]; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && gatt != null) { gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); } Log.d(TAG, "Received opcode reply: " + JoH.bytesToHex(new byte[] { firstByte })); if (firstByte == 0x2f) { SensorRxMessage sensorRx = new SensorRxMessage(characteristic.getValue()); ByteBuffer sensorData = ByteBuffer.allocate(buffer.length); sensorData.order(ByteOrder.LITTLE_ENDIAN); sensorData.put(buffer, 0, buffer.length); int sensor_battery_level = 0; if (sensorRx.status == TransmitterStatus.BRICKED) { //TODO Handle this in UI/Notification sensor_battery_level = 206; //will give message "EMPTY" } else if (sensorRx.status == TransmitterStatus.LOW) { sensor_battery_level = 209; //will give message "LOW" } else { sensor_battery_level = 216; //no message, just system status "OK" } //Log.e(TAG, "filtered: " + sensorRx.filtered); disconnected133 = 0; // reset as we got a reading disconnected59 = 0; lastState = "Got data OK: " + JoH.hourMinuteString(); successes++; failures=0; Log.e(TAG, "SUCCESS!! unfiltered: " + sensorRx.unfiltered + " timestamp: " + sensorRx.timestamp + " " + JoH.qs((double)sensorRx.timestamp / 86400, 1) + " days"); if (sensorRx.unfiltered == 0) { lastState = "Transmitter sent raw sensor value of 0 !! This isn't good. " + JoH.hourMinuteString(); } last_transmitter_timestamp = sensorRx.timestamp; if ((getVersionDetails) && (!haveFirmwareDetails())) { doVersionRequestMessage(gatt, characteristic); } else if ((getBatteryDetails) && (getBatteryStatusNow || !haveCurrentBatteryStatus())) { doBatteryInfoRequestMessage(gatt, characteristic); } else { doDisconnectMessage(gatt, characteristic); } // TODO beware that wear G5CollectionService is now getting rather out of sync with app version final boolean g6 = usingG6(); processNewTransmitterData(g6 ? sensorRx.unfiltered * G6_SCALING : sensorRx.unfiltered, g6 ? sensorRx.filtered * G6_SCALING : sensorRx.filtered, sensor_battery_level, new Date().getTime()); // was this the first success after we force enabled always_authenticate? if (force_always_authenticate && (successes == 1)) { Log.wtf(TAG, "We apparently only got a reading after forcing the Always Authenticate option"); Home.toaststaticnext("Please Enable G5 Always Authenticate debug option!"); // TODO should we actually change the settings here? } } else if (firstByte == GlucoseRxMessage.opcode) { disconnected133 = 0; // reset as we got a reading disconnected59 = 0; GlucoseRxMessage glucoseRx = new GlucoseRxMessage(characteristic.getValue()); Log.e(TAG, "SUCCESS!! glucose unfiltered: " + glucoseRx.unfiltered); successes++; failures=0; doDisconnectMessage(gatt, characteristic); processNewTransmitterData(glucoseRx.unfiltered, glucoseRx.filtered, 216, new Date().getTime()); } else if (firstByte == VersionRequestRxMessage.opcode) { if (!setStoredFirmwareBytes(defaultTransmitter.transmitterId, characteristic.getValue(), true)) { Log.wtf(TAG, "Could not save out firmware version!"); } doDisconnectMessage(gatt, characteristic); } else if (firstByte == BatteryInfoRxMessage.opcode) { if (!setStoredBatteryBytes(defaultTransmitter.transmitterId, characteristic.getValue())) { Log.wtf(TAG, "Could not save out battery data!"); } getBatteryStatusNow = false; doDisconnectMessage(gatt, characteristic); } else { Log.e(TAG, "onCharacteristic CHANGED unexpected opcode: " + firstByte + " (have not disconnected!)"); } Log.e(TAG, "OnCharacteristic CHANGED finished: "); }
Example 19
Source File: DexCollectionService.java From xDrip with GNU General Public License v3.0 | 4 votes |
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { setSerialDataToTransmitterRawData(data, data.length); } }
Example 20
Source File: MutableData.java From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static MutableData from(@NonNull final BluetoothGattCharacteristic characteristic) { return new MutableData(characteristic.getValue()); }