Failing to establish a connection with a custom service - android

I currently own a LilyPad Simblee BLE Board - RFD77101, with which I'm trying to establish a connection with a custom service that I am defining in arduino 1.6.5 IDE with the Simblee.customUUID command.
I later tried to get the service and the characteristic in Android Studio with the BluetoothleGatt sample code using the UUID I established previously.
The problem is when I connect to the Simblee the app fails to recognize the service and logs me the following error.
Custom BLE Service not found
The code is kinda long hence I am not posting all of it directly. If anyone has an idea as to a solution to my problem and need and part of code I am obviously more than glad to post it.
Thanks to anyone in advance.
This is the public void where I attempt to obtain the characteristic:
public void readCustomCharacteristic() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("fe84-0000-1000-8000-00805f9b34fb"));
if(mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("2d30c083-f39f-4ce6-923f-3484ea480596"));
if(!mBluetoothGatt.readCharacteristic(mReadCharacteristic)) {
Log.w(TAG, "Failed to read characteristic");
}
}

BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("fe84-0000-1000-8000-00805f9b34fb"));
The UUID you provided is not in correct format. According to the documentation, the part before first hyphen should consist of 4 hex octets (See the one you provided for characteristics) but you only got 2 here. You should add padding 0 in front like
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("0000fe84-0000-1000-8000-00805f9b34fb"));
Edit : Missing a point here. Have you called mBluetoothGatt.discoverServices()?

Related

Decode BLE GattServices data

I am implementing BLE in Android Studio. I have connected with the peripheral device ok. In my onServicesDiscovered method I want to analyze the services (and characteristics) and I get something like the following when I print out:
android.bluetooth.BluetoothGattService#41b6dd18
There is 4 services in the list and they all look similar except for the numbers at the end. How can I convert this to useful information. I have seen no reference to this format.
Thanks.
Try to read the uuid from the BluetoothGattService object.
You can find uuid of standard services on Bluetooth SIG website. If the uuid is not there (i.e. custom services), you should read the manual of the peripheral or reach out the peripheral maker.
That depends on what you consider "useful" information.
BLE works mostly like dictionary where you look up long numbers (characteristics) and get binary data, so without prior information about the device you're working on, there is not much you can see when you discover services.
That said, in the BLE docs, there is a method displayGattServices() which puts the discovered services info in an ExpandableListView, and here I changed it to print the UUIDs of services and characteristics to logcat instead.
Besides the UUIDs, you can use getProperties() to find out other characteristic properties such as the format of the characteristic data, or getPermissions() to see whether you can read or write the characteristic.
// Demonstrates how to iterate through the supported GATT
// Services/Characteristics.
private void displayGattServices(List<BluetoothGattService> gattServices) {
final String TAG = "BleServiceInfo";
if (gattServices == null) return;
String uuid;
String unknownServiceString = "Unknown service"
String unknownCharaString = "Unknown characteristic"
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
Log.d(TAG, "Service: " + gattService.getUuid().toString());
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
Log.d(TAG, "\tCharacteristic: " + gattCharacteristic.getUuid().toString());
}
}
}
Call this method from onServicesDisccovered() like this:
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
....
displayGattServices(gatt.getServices());
}

Android Ble status code 14 in onCharacteristicwrite

I am developing an app using BLE where i have to send some commands to peripheral device to get response. Write characteristics code is written below:
public void writeCustomCharacteristic(String value) {
this.value = "";
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(SERVICE_UUID);
if (mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(CHARACTERSTICS_UUID);
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
mWriteCharacteristic.setValue(value.getBytes());
if (!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)) {
Log.w(TAG, "Failed to write characteristic");
}
}
And after write characteristics i got response in following method here it returns status code 14, which is not mentioned in official document as well:
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
System.out.println("BluetoothLeService.onCharacteristicWrite");
System.out.println(BluetoothGatt.GATT_SUCCESS + " :status: " + status);
}
Status code 14 probably means ATT error code 14, which is defined in Bluetooth Core specification v5.0, Vol 3, Part F, section 3.4.1.1 Table 3.3.
This error code is sent from the remote device so it's most likely no error on the Android side. Error code 14 in ATT means "Unlikely Error". You need to investigate why the remote device sends this error code.

BLE readCharacteristics always return false, or getvalue is null

I'm trying to use BLE in Android with a deivice that has fields : battery (RO), status (RO), intensity (R/W).
I followed some tutorials about setting up the Gatt with the device.
I Use the following code :
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.w(LOGGER + mOwner.get().getName(), "onServicesDiscovered received: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
// Save all services
for (BluetoothGattService gattService : mBluetoothGatt.getServices()) {
for (BluetoothGattCharacteristic charac : gattService
.getCharacteristics()) {
if (isContained(charac) {
mCharacteristics.set(mCharacteristicsUuid.indexOf(charac.getUuid()
.toString()), charac);
mBluetoothGatt.setCharacteristicNotification(charac, true);
// UUID for notification
BluetoothGattDescriptor descriptor = charac
.getDescriptor(UUID.fromString("00002902-0000-1000-" +
"8000-00805f9b34fb"));
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor
.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
}
}
So i try to discover services, if done iterate over them, and their characteristics. If their UUIDs are correct, i add them to mCharacteristics.
If this a value readable (here, everyone of them), I enable notification.
When it's done i try to do a first read :
Log.e(LOGGER + mOwner.get().getName(), "Reading first charac(index=0) : " + mBluetoothGatt
.readCharacteristic(mCharacteristics.get(0)));
}
I have to precise that all of this is inside a dedicated thread, kept up in an Android Service.
I'm sure that the device is connected, and characteristics too.
For each one i verified the value of (Property & REEADABLE), that always > 0...
But for the Read only characteristics i ALWAYS get false on read...
And for the intensity (read/write), read returns true, but onCharacteristicRead() is never called, and the getValue() method returns null.
I'm pretty newer using BLE in Android.
Someone has an idea about the problem ?
Thanks in advance !
EDIT : I finally found the solution but not as I expected...
In fact, if I set the notification enabled, ii can't read the characteristic after...
Can someone tell me how i can perform this way :
> 1) on discover
> a) get all characts + set in list characs I want
> b) enable notification for ALL of these charac (if possible, I supposed, because of the descriptor that can be null ? )
> c) first read to know starting values
You have the answer here: BLE Android, can't enable more than 1 notify on read characteristics.
You have to wait for a GATT operation to complete before you can do another one.

keyfob cc2541 bluetooth connection drops when trying to enable acclerometer on android

I am developing an Android app that needs to read the Keyfob's Accelerometer data. Until now I've followed this tutorial: https://thenewcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial
With it I was able to connect with the Keyfob, search for services and read some characteristics. The problem is when I try to enable the keyfob's Accelerometer, the bluetooth connection simply drops.
This is the code I use to try to enable the accelerometer:
private void enableAccelerometer(BluetoothGatt gatt){
BluetoothGattCharacteristic characteristic;
BluetoothGattService service;
Log.d(TAG, "ligando acelerometro");
service = gatt.getService(ACCELEROMETER_SERVICE);
if(service == null){
Log.d(TAG, "Not able to find the service");
}
else{
Log.d(TAG, "Service found");
characteristic = service.getCharacteristic(ENABLE_ACCELEROMETER);
if(characteristic == null){
Log.d(TAG, "Characteristic not found");
}
else{
characteristic.setValue(new byte[] {0x01});
if (!gatt.writeCharacteristic(characteristic)){
Log.d(TAG, "writing failed ");
}
else {
Log.d(TAG, "writing successful: ");
}
}
}
This method is called in the "onServicesDiscovered" callback function.
The Texas Instrument CC2540/41 Mini Development Kit User’s Guide states that to enable the accelerometer it is necessary to write "01" in the enable acceleromenter characteristic in the accelerometer serivce, that's what I am doing with this code.
The connection between the phone (LG G2 mini running Android 4.4.2) drops when I write:
characteristic.setValue(new byte[] {0x01});
I am sure it is this line that is making the connection drop, if I comment it out or simply try to write a string instead of a byte, the connection doesn't drop.
Does anyone have any idea what am I doing wrong?
Turns out that after a week I found a way to turn the accelerometer on. I still don't know why it is working only that way, but I just changed:
characteristic.setValue(new byte[] {0x01});
to
characteristic.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
I don't know what value is exactly inside the "BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE" constant, but it worked.

BLE Device disconnect with Android device automatically. Android BLE

I'm using Android Nexus 7 to connect a device via Bluetooth Low Energy link. I'm able to connect the device, and stay connected if I don't do any communication with the device.
However, if I enable the notification of one specific characteristic by clicking a button, then the device would disconnect with the tablet after a few seconds' data transmission.
Does anyone know what might be the problem? Thank you very much!
Here's my code:
public boolean setCharacteristicNotification(boolean enabled){
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return false;
}
BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
if (Service == null) {
Log.e(TAG, "service not found!");
return false;
}
BluetoothGattCharacteristic characteristic = Service.getCharacteristic(UUID_MY_CHARACTERISTIC);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
return true;
}
return false;
}
(Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )
The OP wrote:
I solved this problem today.
Just change descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
to descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
Follow Up:
After I did some research and testing, I found that the automatically disconnection problem has something to do with the interference between Bluetooth and WIFI on Nexus 7. If I turned off the WIFI, then the disconnection problem of Bluetooth has gone. And this problem did not occur on Galaxy 3,4,5.
Problem: I was having same problem on Tesco Hudl 2, if i transmit some
data soon as Bluetooth is connected, it will disconnect.
Solution: Wait for few
seconds after connection, it seems to work okay.

Categories

Resources