|
|
|
@ -144,23 +144,26 @@ public class BluetoothLeService extends Service { |
|
|
|
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) { |
|
|
|
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid()) |
|
|
|
&& data.length >= 1) { |
|
|
|
int value = (data[0]<<8)&0x0000ff00 | (data[1]<<0)&0x000000ff; |
|
|
|
intent.putExtra(EXTRA_DATA, String.format("décimal: %d", value)); |
|
|
|
} else { |
|
|
|
final StringBuilder stringBuilder = new StringBuilder(data.length); |
|
|
|
//stringBuilder.append(String.format("%d", data));/
|
|
|
|
//stringBuilder.append(" --- ");
|
|
|
|
for (byte byteChar : data) |
|
|
|
stringBuilder.append(String.format("%02X ", byteChar)); |
|
|
|
Log.d(TAG, String.format(stringBuilder.toString())); |
|
|
|
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); |
|
|
|
} |
|
|
|
// For all other profiles, writes the data formatted in HEX.
|
|
|
|
final byte[] data = characteristic.getValue(); |
|
|
|
if (data != null && data.length > 0) { |
|
|
|
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { |
|
|
|
long value = |
|
|
|
(data.length == 2) ? (data[0] << 8) & 0x0000ff00 | (data[1] << 0) & 0x000000ff |
|
|
|
: (data[0] << 0) & 0x000000ff; |
|
|
|
long max = 65535; // 2^16 - 1
|
|
|
|
double percent = ((double) (100 * value)) / ((double) max); |
|
|
|
intent.putExtra(EXTRA_DATA, String.format("%.3f %%", percent)); |
|
|
|
} else { |
|
|
|
final StringBuilder stringBuilder = new StringBuilder(data.length); |
|
|
|
//stringBuilder.append(String.format("%d", data));/
|
|
|
|
//stringBuilder.append(" --- ");
|
|
|
|
for (byte byteChar : data) |
|
|
|
stringBuilder.append(String.format("%02X ", byteChar)); |
|
|
|
Log.d(TAG, String.format(stringBuilder.toString())); |
|
|
|
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
/* TODO |
|
|
|
} |
|
|
|
*/ |
|
|
|
@ -218,11 +221,10 @@ public class BluetoothLeService extends Service { |
|
|
|
* Connects to the GATT server hosted on the Bluetooth LE device. |
|
|
|
* |
|
|
|
* @param address The device address of the destination device. |
|
|
|
* |
|
|
|
* @return Return true if the connection is initiated successfully. The connection result |
|
|
|
* is reported asynchronously through the |
|
|
|
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} |
|
|
|
* callback. |
|
|
|
* is reported asynchronously through the |
|
|
|
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} |
|
|
|
* callback. |
|
|
|
*/ |
|
|
|
public boolean connect(final String address) { |
|
|
|
if (mBluetoothAdapter == null || address == null) { |
|
|
|
@ -301,7 +303,7 @@ public class BluetoothLeService extends Service { |
|
|
|
* Enables or disables notification on a give characteristic. |
|
|
|
* |
|
|
|
* @param characteristic Characteristic to act on. |
|
|
|
* @param enabled If true, enable notification. False otherwise. |
|
|
|
* @param enabled If true, enable notification. False otherwise. |
|
|
|
*/ |
|
|
|
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, |
|
|
|
boolean enabled) { |
|
|
|
@ -310,17 +312,15 @@ public class BluetoothLeService extends Service { |
|
|
|
return; |
|
|
|
} |
|
|
|
Log.d(TAG, "setChar.Notification() appelé"); |
|
|
|
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); |
|
|
|
|
|
|
|
/* TODO |
|
|
|
// This is specific to Heart Rate Measurement.
|
|
|
|
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { |
|
|
|
BluetoothGattDescriptor descriptor = characteristic.getDescriptor( |
|
|
|
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); |
|
|
|
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); |
|
|
|
mBluetoothGatt.writeDescriptor(descriptor); |
|
|
|
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { |
|
|
|
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(SampleGattAttributes.CHARACTERISTIC_CONFIG_UUID); |
|
|
|
descriptor.setValue( |
|
|
|
enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE |
|
|
|
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE |
|
|
|
); |
|
|
|
while (!mBluetoothGatt.writeDescriptor(descriptor)) ; |
|
|
|
} |
|
|
|
*/ |
|
|
|
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|