Reputation: 2724
I've implemented the Android LE bluetooth example that find a heart rate monitor and connects to it. However, this example has a class that defines the GATT profile like this:
private static HashMap<String, String> attributes = new HashMap();
public static String HEART_RATE_MEASUREMENT = "00002a37-0000-1000-8000-00805f9b34fb";
public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
static {
// Sample Services.
attributes.put("0000180d-0000-1000-8000-00805f9b34fb", "Heart Rate Service");
attributes.put("0000180a-0000-1000-8000-00805f9b34fb", "Device Information Service");
// Sample Characteristics.
attributes.put(HEART_RATE_MEASUREMENT, "Heart Rate Measurement");
attributes.put("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name String");
}
public static String lookup(String uuid, String defaultName) {
String name = attributes.get(uuid);
return name == null ? defaultName : name;
}
Now, what I'm wanting to do is change it so that this program finds any device with bluetooth le but I have no idea how Google have gotten the information for the heart rate measurement of the client characteristic config.
Upvotes: 19
Views: 38937
Reputation: 1545
The Bluetooth SIG maintains a list of "Assigned Numbers" that includes those UUIDs found in the sample app: https://www.bluetooth.com/specifications/assigned-numbers/
Although UUIDs are 128 bits in length, the assigned numbers for Bluetooth LE are listed as 16 bit hex values because the lower 96 bits are consistent across a class of attributes.
For example, all BLE characteristic UUIDs are of the form:
0000XXXX-0000-1000-8000-00805f9b34fb
The assigned number for the Heart Rate Measurement characteristic UUID is listed as 0x2A37
, which is how the developer of the sample code could arrive at:
00002a37-0000-1000-8000-00805f9b34fb
Upvotes: 89
Reputation: 11035
In addition to @Godfrey Duke's answer, here's a method I use to extract the significant bits of the UUID:
private static int getAssignedNumber(UUID uuid) {
// Keep only the significant bits of the UUID
return (int) ((uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >> 32);
}
Example of usage:
// See https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml
private static final int GATT_SERVICE_HEART_RATE = 0x180D;
(...)
for (BluetoothGattService service : services) {
if (getAssignedNumber(service.getUuid()) == GATT_SERVICE_HEART_RATE) {
// Found heart rate service
onHeartRateServiceFound(service);
found = true;
break;
}
}
Upvotes: 7
Reputation: 217
Here's the page on the gatt callback: https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html
You need to use the BluetoothGatt.discoverServices();
and then in the callback onServicesDiscovered(...) i think you need to useBluetoothGatt.getServices();
Upvotes: -3