Difference between Reading a characteristic and setting up notifications - android

Hi I am working on a Bluetooth low energy in my android app
I am able to read the characteristics whenever I call my readCharacter() in app
but I want to enable a notification such that when ever there is data available I can read.
Currently I am using the method below.
private byte[] readCharacteristic(){
if(!isReadEnabled){
enableTXNotification();
isReadEnabled = true;
}
byte[] arr = new byte[CHUCK_SIZE];
BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
if(RxService == null){
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return arr;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
if(TxChar == null){
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return arr;
}
arr = TxChar.getValue();
Log.d(TAG,Arrays.toString(arr));
return arr;
}
And this is my enableTXNotification():
private void enableTXNotification(){
if (mBluetoothGatt == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
if (RxService == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
if (TxChar == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
mBluetoothGatt.setCharacteristicNotification(TxChar,true);
BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
So my question is what is the difference between Enabling notification and Reading characteristic?
How can I get notification whenever data is available

Related

Android BLE device disconnects after short time, error code 8

I have an Android app which can connect to a BLE device (using BMD-350), receive data from it via notifications and transmit data on a characteristic. I don't know why, but after a while the BLE device disconnects on its own, reporting error code 8 in the BondStateChanged callback. I can see data coming in to the phone right up until it disconnects. The BLE device sends data to the phone around 550 bytes/sec. The phone does not send any data to the BLE device in this time. Why is it disconnecting?
*note: This code is written in C# (Xamarin), but it's basically the same as its Java counterpart.
Connection Code:
_bleGatt = device.ConnectGatt(context, false, this);
Connection Callback:
if (newState == ProfileState.Connected)
{
if (status == GattStatus.Success)
{
gatt.DiscoverServices();
}
}
Service Discovery:
public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
{
base.OnServicesDiscovered(gatt, status);
BluetoothGattService mService;
try
{
mService = gatt.GetService(UART_UUID);
}
catch
{
BluetoothConnectionError("UART service not found", gatt.Device);
return;
}
_tx = mService.GetCharacteristic(TX_UUID);
_rx = mService.GetCharacteristic(RX_UUID);
if (_tx == null)
{
BluetoothConnectionError("Tx characteristic not found", gatt.Device);
return;
}
if ((_tx.Properties | GattProperty.WriteNoResponse) == 0 && (_tx.Properties | GattProperty.Write) == 0)
{
BluetoothConnectionError("Tx characteristic not not in 'write' or 'write - no response' mode", gatt.Device);
return;
}
if (_rx == null)
{
BluetoothConnectionError("Rx characteristic not found", gatt.Device);
return;
}
if ((_rx.Properties | GattProperty.Notify) == 0)
{
BluetoothConnectionError("Rx characteristic not in 'notify' mode", gatt.Device);
return;
}
BluetoothGattDescriptor mDescriptor = _rx.GetDescriptor(CLIENT_UUID);
if (mDescriptor == null)
{
BluetoothConnectionError("Could not get descriptor", gatt.Device);
return;
}
mDescriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
try
{
gatt.WriteDescriptor(mDescriptor);
}
catch
{
BluetoothConnectionError("Could not write descriptor", gatt.Device);
return;
}
ConnectionStatusChanged(ProfileState.Connected, gatt.Device);
}
Enabling Receive Notifications:
_bleGatt.SetCharacteristicNotification(_rx, true);
Reading Characteristic:
public override void OnCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic)
{
base.OnCharacteristicChanged(gatt, characteristic);
PublishNewData(characteristic.GetValue());
}
UUID's/CCCD:
private readonly UUID UART_UUID = UUID.FromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
private readonly UUID RX_UUID = UUID.FromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
private readonly UUID TX_UUID = UUID.FromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
private readonly UUID CLIENT_UUID = UUID.FromString("00002902-0000-1000-8000-00805f9b34fb");

Unable to read/write some characateristics from BLE device using the BluetoothLeGatt app

I have been working with the BluetoothLeGatt application. I am trying to read characteristics from a BLE device- TI CC2541 Keyfob. I am able to read and write into some characteristics, but failing to do so to some other characteristics.
All these services and characteristics are listed in the expandableListView. But on selecting some of them, their values and not getting displayed.
Can anybody help me out with this problem.
Is there a way to read value using Characteristic Value Handle
**For Write characterstics Steps :
1. First you have to do indicate
2. After executing indication onDecriptor write sucessfull is executed.Here you have to start write characterstics.**
// for indicate the follwing code is in BluetoothLEService
public void indicateCharacteristic(UUID serviceUUID, UUID characteristicUuid, boolean isIndicate) {
try {
UUID serviceuid = serviceUUID;
if (serviceuid != null && mBluetoothGatt != null) {
BluetoothGattService service = mBluetoothGatt.getService(serviceuid);
UUID characteristicuid = characteristicUuid;
BluetoothGattCharacteristic characteristic = null;
if (service != null) {
characteristic = service.getCharacteristic(characteristicuid);
//Enable local notifications
if (mBluetoothGatt != null) {
mBluetoothGatt.setCharacteristicNotification(characteristic, isIndicate);
ArrayList<BluetoothGattDescriptor> gattdescriptor = (ArrayList<BluetoothGattDescriptor>) characteristic
.getDescriptors();
for (int k = 0; k < gattdescriptor.size(); k++) {
BluetoothGattDescriptor descriptor = gattdescriptor.get(k);
if (descriptor.getUuid().toString().equalsIgnoreCase(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG))
writeGattDescriptorForIndication(descriptor, isIndicate);
}
}
}
}
} catch (Exception e) {
Log.d("device", "not found");
}
}
// Write gatt descriptor
public void writeGattDescriptorForIndication(BluetoothGattDescriptor d, boolean isIndicate) {
boolean test;
if (isIndicate) {
d.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
d.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
} else {
d.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
// test = mBluetoothGatt.readDescriptor(d); // return value = true
// Log.d("test",""+test);
test = mBluetoothGatt.writeDescriptor(d);
Log.d("test", "" + test);
}
#Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (characteristic.getUuid().toString().equals(GattAttributes.BATTERY_LEVEL_CHARACTERSTIC_UUID))
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
#Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
writeStartCommand();
Log.d(TAG, "volume Descriptor writing successful");
} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
// this is where the tricky part comes
if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
Log.e(TAG, "Bonding required!!!");
} else {
// this situation happens when you try to connect for the
// second time to already bonded device
// it should never happen, in my opinion
Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
// I don't know what to do here
// This error was found on Nexus 7 with KRT16S build of
// Andorid 4.4. It does not appear on Samsung S4 with
// Andorid 4.3.
}
} else {
Log.e(TAG, "Error writing descriptor, status: " + status);
}
}
};
public void writeStartCommand() {
int val = 0x55;
doWrite(UUID.fromString(GattAttributes.BATTERY_LEVEL_SERVICE_UUID), UUID.fromString(GattAttributes.VOLUME_START_SERVICE_UUID), val);
waitSometime(100);
}
public synchronized void doWrite(UUID gatservice_uuid, UUID char_uuid, int value) {
try {
byte[] value1 = new byte[1];
value1[0] = (byte) (Integer.valueOf(value) & 0xFF);
BluetoothGattService mSVC = mBluetoothGatt.getService(gatservice_uuid);
BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(char_uuid);
mCH.setValue(value1);
Log.d("write val", "" + value1);
// mCH.setValue(value, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
// mCH.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);
if (mBluetoothGatt.writeCharacteristic(mCH)) {
waitSometime(100);
Log.d("sucess", "write characteristics successfully stored-" + char_uuid);
} else {
Log.d("fail", "write characteristics failed-" + char_uuid);
/* if(char_uuid.toString().equalsIgnoreCase(GattAttributes.CALIBRATION_START_COMMAND_UUID)){
Thread.sleep(100);
isWriting
writeStopCommand();
}else {
Toast.makeText(CalibrationLeService.this, "Write Characteristics failed"+char_uuid.toString(), Toast.LENGTH_SHORT).show();
}*/
//sendBroadcast(new Intent(BluetoothLeForegroundService.ACTION_FINISH));
}
} catch (Exception e) {
Log.d("characteristic id is", "discoverd services not available");
}
}
public synchronized void waitSometime(int seconds) {
try {
Thread.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// And then finally in your activity just call
public static String BATTERY_LEVEL_SERVICE_UUID = "5956efdd-7272-4bfe-937a-f17c70e86b55"; // Volume level service UUID
public static String BATTERY_LEVEL_CHARACTERSTIC_UUID = "a5bd1e6a-db71-4da5-9b42-a59800e4538b";
mBluetoothLeForegroundService.indicateCharacteristic(UUID.fromString(GattAttributes.BATTERY_LEVEL_SERVICE_UUID), UUID.fromString(GattAttributes.BATTERY_LEVEL_CHARACTERSTIC_UUID), true);

Is the procedure correct to write Characteristic in Bluetooth Low Energy?

I have a button in MainActivity which is used to write a byte[] to BLE device:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Write Char"
android:id="#+id/button2"
android:onClick="onClickWrite" />
And the function onClickWrite is
public void onClickWrite(View v){
if(mBluetoothLeService != null) {
byte [] data= new byte[3];
data[0] = (byte)(0 & 0xFF);
data[1] = (byte)(255 & 0xFF);
data[2] = (byte)(0 & 0xFF);
mBluetoothLeService.sendData(data);
}
}
where sendData is a modified function in class [BluetoothLeService][1]. It worked well when I press the button. However, Let is careful to look at the sendData function. It searches service and Characteristic again when I press the button. Is it correct procedure for write a Characteristic in BLE?
public void sendData(byte[] data){
String lService = "00001c00-d102-11e1-9b23-00025b00a5A5";
String lCharacteristic = "00001c03-d102-11e1-9b23-00025b00a5a5";
BluetoothGattService mBluetoothLeService = null;
BluetoothGattCharacteristic mBluetoothGattCharacteristic = null;
for (BluetoothGattService service : mBluetoothGatt.getServices()) {
if ((service == null) || (service.getUuid() == null)) {
Log.d("TAG","Something is null");
continue;
}
if (lService.equalsIgnoreCase(service.getUuid().toString())) {
Log.d("TAG","service.getUuid().toString()="+service.getUuid().toString());
mBluetoothLeService = service;
}
}
if(mBluetoothLeService!=null) {
mBluetoothGattCharacteristic =
mBluetoothLeService.getCharacteristic(UUID.fromString(lRgbCharacteristic));
}
else{
Log.d("TAG","mBluetoothLeService is null");
}
if(mBluetoothGattCharacteristic!=null) {
mBluetoothGattCharacteristic.setValue(data);
boolean write = mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic);
Log.d("TAG","writeCharacteristic:"+write);
}
else{
Log.d("TAG", "mBluetoothGattCharacteristic is null");
}
}
Yes.
(filling unnecessarily required length)

onCharacteristicChanged not called with BLE

I've connected a blood pressure device and set notification in onServiceDicovered.
I set notify with every Characteristic.
But onCharacteristicChanged is still not called.
public final static UUID CLIENT_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
gatt.setCharacteristicNotification(characteristic, enabled);
for (BluetoothGattDescriptor dp : characteristic.getDescriptors()) {
dp = characteristic.getDescriptor(CLIENT_UUID);
if (dp != null) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
}
gatt.writeDescriptor(dp);
}
}
#Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
I want to receive blood pressure, but onCharacteristicChanged never called.
But I can receive in ios or other sample code.
Thank you!
First, the property of Blood Pressure Measurement is Indicate, not Notify
https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.blood_pressure.xml
iOS the CBPeripheral setNotifyValue is automatic to set Indicate or Notify, but android not implement.
Give you some code for reference
if (gatt.setCharacteristicNotification(characteristic, true)) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (descriptor != null) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
} else {
// The characteristic does not have NOTIFY or INDICATE property set;
}
if (gatt.writeDescriptor(descriptor)) {
// Success
} else {
// Failed to set client characteristic notification;
}
} else {
// Failed to set client characteristic notification;
}
} else {
// Failed to register notification;
}

Android Bluetooth: Get UUIDs of discovered devices

As I'm currently working on a little bluetooth library for Android, I'm trying to get all the service uuids of the devices I discovered in my surrounding.
When my broadcast receiver gets the BluetoothDevice.ACTION_FOUND intent, I'm extracting the device and call:
device.fetchUuidsWithSdp();
This will result BluetoothDevice.ACTION_UUID intents for each device found and I'm handling them with the same receiver:
BluetoothDevice d = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
if(uuidExtra == null) {
Log.e(TAG, "UUID = null");
}
if(d != null && uuidExtra != null)
Log.d(TAG, d.getName() + ": " + uuidExtra.toString());
The thing is, that uuidExtra is always null.
How can i get all the UUIDs of the surrounding devices?
EDIT:
Im working on a Nexus 7. I tried code i found on the internet and this also gives me a NullPointerException: http://digitalhacksblog.blogspot.de/2012/05/android-example-bluetooth-discover-and.html
Thank you.
The documentation on this states...
Always contains the extra field BluetoothDevice.EXTRA_UUID
However, just like you, I have found this not to be true.
If you call fetchUuidsWithSdp() while device discovery is still taking place BluetoothDevice.EXTRA_UUID can be null.
You should wait until you receive BluetoothAdapter.ACTION_DISCOVERY_FINISHED before you make any calls to fetchUuidsWithSdp().
NOTE: This solution applies to CLASSIC bluetooth and not BLE. For BLE check how to send
manufacturer specific Data in advertiser on the peripheral side
The problem with fetching Uuids is that you have only one bluetooth adapter, and we cannot have parallel api calls which uses adapter for its purpose.
As Eddie pointed out, wait for BluetoothAdapter.ACTION_DISCOVERY_FINISHED and then call fetchUuidsWithSdp().
Still this cannot guarantee uuids to be fetched for all devices. In addition to this one must wait for each subsequent call to fetchuuidsWithSdp() to complete, and then give a call to this method for another device.
See the code below --
ArrayList<BluetoothDevice> mDeviceList = new ArrayList<BluetoothDevice>();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// discovery has finished, give a call to fetchUuidsWithSdp on first device in list.
if (!mDeviceList.isEmpty()) {
BluetoothDevice device = mDeviceList.remove(0);
boolean result = device.fetchUuidsWithSdp();
}
} else if (BluetoothDevice.ACTION_UUID.equals(action)) {
// This is when we can be assured that fetchUuidsWithSdp has completed.
// So get the uuids and call fetchUuidsWithSdp on another device in list
BluetoothDevice deviceExtra = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
System.out.println("DeviceExtra address - " + deviceExtra.getAddress());
if (uuidExtra != null) {
for (Parcelable p : uuidExtra) {
System.out.println("uuidExtra - " + p);
}
} else {
System.out.println("uuidExtra is still null");
}
if (!mDeviceList.isEmpty()) {
BluetoothDevice device = mDeviceList.remove(0);
boolean result = device.fetchUuidsWithSdp();
}
}
}
}
UPDATE: Latest android versions (mm & above) would result in triggering a pairing process with each device
Here is a good example of how to get UUIDs of service characteristics from a service that I did for getting heart rate devices:
private class HeartRateBluetoothGattCallback extends BluetoothGattCallback {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
logMessage("CONNECTED TO " + gatt.getDevice().getName(), false, false);
gatt.discoverServices();
} else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
logMessage("DISCONNECTED FROM " + gatt.getDevice().getName(), false, false);
if(mIsTrackingHeartRate)
handleHeartRateDeviceDisconnection(gatt);
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
logMessage("DISCOVERING SERVICES FOR " + gatt.getDevice().getName(), false, false);
if(mDesiredHeartRateDevice != null &&
gatt.getDevice().getAddress().equals(mDesiredHeartRateDevice.getBLEDeviceAddress())) {
if(subscribeToHeartRateGattServices(gatt)) {
mIsTrackingHeartRate = true;
setDeviceScanned(getDiscoveredBLEDevice(gatt.getDevice().getAddress()), DiscoveredBLEDevice.CONNECTED);
broadcastHeartRateDeviceConnected(gatt.getDevice());
} else
broadcastHeartRateDeviceFailedConnection(gatt.getDevice());
} else {
parseGattServices(gatt);
disconnectGatt(getDiscoveredBLEDevice(gatt.getDevice().getAddress()));
}
}
}
#Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
if(characteristic.getUuid().equals(UUID.fromString(HEART_RATE_VALUE_CHAR_READ_ID))) {
int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0)
format = BluetoothGattCharacteristic.FORMAT_UINT16;
else
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Integer heartRateValue = characteristic.getIntValue(format, 1);
if(heartRateValue != null)
broadcastHeartRateValue(heartRateValue);
else
Log.w(SERVICE_NAME, "UNABLE TO FORMAT HEART RATE DATA");
}
};
};
private void parseGattServices(BluetoothGatt gatt) {
boolean isHeartRate = false;
for(BluetoothGattService blueToothGattService : gatt.getServices()) {
logMessage("GATT SERVICE: " + blueToothGattService.getUuid().toString(), false, false);
if(blueToothGattService.getUuid().toString().contains(HEART_RATE_DEVICE_SERVICE_CHARACTERISTIC_PREFIX))
isHeartRate = true;
}
if(isHeartRate) {
setDeviceScanned(getDiscoveredBLEDevice(gatt.getDevice().getAddress()), DiscoveredBLEDevice.IS_HEART_RATE);
broadcastHeartRateDeviceFound(getDiscoveredBLEDevice(gatt.getDevice().getAddress()));
} else
setDeviceScanned(getDiscoveredBLEDevice(gatt.getDevice().getAddress()), DiscoveredBLEDevice.NOT_HEART_RATE);
}
private void handleHeartRateDeviceDisconnection(BluetoothGatt gatt) {
broadcastHeartRateDeviceDisconnected(gatt.getDevice());
gatt.close();
clearoutHeartRateData();
scanForHeartRateDevices();
}
private void disconnectGatt(DiscoveredBLEDevice device) {
logMessage("CLOSING GATT FOR " + device.getBLEDeviceName(), false, false);
device.getBlueToothGatt().close();
device.setBlueToothGatt(null);
mInDiscoveryMode = false;
}
private boolean subscribeToHeartRateGattServices(BluetoothGatt gatt) {
for(BluetoothGattService blueToothGattService : gatt.getServices()) {
if(blueToothGattService.getUuid().toString().contains(HEART_RATE_DEVICE_SERVICE_CHARACTERISTIC_PREFIX)) {
mHeartRateGattService = blueToothGattService;
for(BluetoothGattCharacteristic characteristic : mHeartRateGattService.getCharacteristics()) {
logMessage("CHARACTERISTIC UUID = " + characteristic.getUuid().toString(), false, false);
for(BluetoothGattDescriptor descriptor :characteristic.getDescriptors()) {
logMessage("DESCRIPTOR UUID = " + descriptor.getUuid().toString(), false, false);
}
if(characteristic.getUuid().equals(UUID.fromString(HEART_RATE_VALUE_CHAR_READ_ID))) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HEART_RATE_VALUE_CHAR_DESC_ID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
return gatt.writeDescriptor(descriptor);
}
}
break; //break out of master for-loop
}
}
return false;
}
I suppose you need to be paired with the device in order to receive the uuids.
At least, this is what happened to me.
device.getUuids() use this to get all the uuid of that paired device in the form of ParcelUuid; Example code below:-
private void get_uuid_from_paired_devices(){
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device: pairedDevices){
for (ParcelUuid uuid: device.getUuids()){
String uuid_string = uuid.toString();
Log.d(TAG, "uuid : "+uuid_string);
}
}
}
Below worked for me to fetch the records from the remote device
-0-
registerReceiver(..,
new IntentFilter(BluetoothDevice.ACTION_UUID));
-1-
device.fetchUuidsWithSdp();
-2-from within the broadcase receiver
if (BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (Parcelable ep : uuids) {
Utilities.print("UUID records : "+ ep.toString());
}
}
You can also fetch the offline cached UUID records with
BluetoothDevice.getUuids();

Categories

Resources