Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #76 from beaufortfrancois/addDescriptors
Browse files Browse the repository at this point in the history
Add Characteristic User Description GATT descriptor
  • Loading branch information
beaufortfrancois committed Feb 9, 2017
2 parents e6e1af9 + 67d4752 commit 72d83ce
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android:
- tools

# The BuildTools version
- build-tools-23.0.2
- build-tools-24.0.1

# The SDK version used to compile
- android-22
Expand Down
9 changes: 4 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "23.0.2"

buildToolsVersion '24.0.1'
defaultConfig {
applicationId "io.github.webbluetoothcg.bletestperipheral"
minSdkVersion 21
targetSdkVersion 22
versionCode 8
versionName "8.0"
versionCode 9
versionName '9.0'
}
buildTypes {
release {
Expand All @@ -26,5 +25,5 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class BatteryServiceFragment extends ServiceFragment {
.fromString("00002A19-0000-1000-8000-00805f9b34fb");
private static final int INITIAL_BATTERY_LEVEL = 50;
private static final int BATTERY_LEVEL_MAX = 100;
private static final String BATTERY_LEVEL_DESCRIPTION = "The current charge level of a " +
"battery. 100% represents fully charged while 0% represents fully discharged.";

private ServiceFragmentDelegate mDelegate;
// UI
Expand Down Expand Up @@ -116,6 +118,9 @@ public BatteryServiceFragment() {
mBatteryLevelCharacteristic.addDescriptor(
Peripheral.getClientCharacteristicConfigurationDescriptor());

mBatteryLevelCharacteristic.addDescriptor(
Peripheral.getCharacteristicUserDescriptionDescriptor(BATTERY_LEVEL_DESCRIPTION));

mBatteryService = new BluetoothGattService(BATTERY_SERVICE_UUID,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
mBatteryService.addCharacteristic(mBatteryLevelCharacteristic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class HeartRateServiceFragment extends ServiceFragment {
private static final int INITIAL_HEART_RATE_MEASUREMENT_VALUE = 60;
private static final int EXPENDED_ENERGY_FORMAT = BluetoothGattCharacteristic.FORMAT_UINT16;
private static final int INITIAL_EXPENDED_ENERGY = 0;
private static final String HEART_RATE_MEASUREMENT_DESCRIPTION = "Used to send a heart rate " +
"measurement";

/**
* See <a href="https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml">
Expand Down Expand Up @@ -158,6 +160,9 @@ public HeartRateServiceFragment() {
mHeartRateMeasurementCharacteristic.addDescriptor(
Peripheral.getClientCharacteristicConfigurationDescriptor());

mHeartRateMeasurementCharacteristic.addDescriptor(
Peripheral.getCharacteristicUserDescriptionDescriptor(HEART_RATE_MEASUREMENT_DESCRIPTION));

mBodySensorLocationCharacteristic =
new BluetoothGattCharacteristic(BODY_SENSOR_LOCATION_UUID,
BluetoothGattCharacteristic.PROPERTY_READ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class Peripheral extends Activity implements ServiceFragmentDelegate {
private static final String TAG = Peripheral.class.getCanonicalName();
private static final String CURRENT_FRAGMENT_TAG = "CURRENT_FRAGMENT";

private static final UUID CHARACTERISTIC_USER_DESCRIPTION_UUID = UUID
.fromString("00002901-0000-1000-8000-00805f9b34fb");
private static final UUID CLIENT_CHARACTERISTIC_CONFIGURATION_UUID = UUID
.fromString("00002902-0000-1000-8000-00805f9b34fb");

Expand Down Expand Up @@ -172,6 +174,21 @@ public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
}
}

@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId,
int offset, BluetoothGattDescriptor descriptor) {
Log.d(TAG, "Device tried to read descriptor: " + descriptor.getUuid());
Log.d(TAG, "Value: " + Arrays.toString(descriptor.getValue()));
super.onDescriptorReadRequest(device, requestId, offset, descriptor);
if (offset != 0) {
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset,
/* value (optional) */ null);
return;
}
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset,
descriptor.getValue());
}

@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded,
Expand All @@ -180,7 +197,8 @@ public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
Log.v(TAG, "Descriptor Write Request " + descriptor.getUuid() + " " + Arrays.toString(value));
super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded,
offset, value);
if(responseNeeded) {
descriptor.setValue(value);
if (responseNeeded) {
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,
/* No need to respond with offset */ 0,
/* No need to respond with a value */ null);
Expand Down Expand Up @@ -346,8 +364,21 @@ public void run() {
////// Bluetooth //////
///////////////////////
public static BluetoothGattDescriptor getClientCharacteristicConfigurationDescriptor() {
return new BluetoothGattDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID,
(BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
CLIENT_CHARACTERISTIC_CONFIGURATION_UUID,
(BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
descriptor.setValue(new byte[]{0, 0});
return descriptor;
}

public static BluetoothGattDescriptor getCharacteristicUserDescriptionDescriptor(String defaultValue) {
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
CHARACTERISTIC_USER_DESCRIPTION_UUID, (BluetoothGattDescriptor.PERMISSION_READ));
try {
descriptor.setValue(defaultValue.getBytes("UTF-8"));
} finally {
return descriptor;
}
}

private void ensureBleFeaturesAvailable() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 72d83ce

Please sign in to comment.