Android Question BLE Descriptors discover

elpic76

Member
Licensed User
Longtime User
In my system with a BLE112 configured as Cable Raplacement Service I've to enable the indications.
From bluegiga documentation:

"Data can be received from DevA with the attribute protocol’s indication operation. Indication is a reliable way of transmitting data from the ATT Server to the ATT client and they are generated whenever the ATT server has some new data to send to the client. Indications can carry up to 20 bytes of application data.
Indications are not sent by default, but the ATT client needs to enable them first. In order to enable the indications:
1. Select the Cable Replacement Service in the BLEGUI’s GATT view
2. Press Descriptors discover in order to see all characteristics and descriptors in the CR service.
3. Once the descriptors discovery is complete, select the Client Characteristics Configuration (UUID: 2902) value that relates to the Cable Replacement Data (UUID: e7add780-b042-4876-aae1-112855353cc1) and select it
4. In order to enable indications for the Cable Replacement Data, use Write button to write value 2 to the Client Characteristics Configuration.
5. Finally make sure the write operation is executed properly (see Log)"

How can I write 2 to Client Characteristics Configuration (UUID: 2902) to enable indications?

Thanks in advance.

Mosè
 

elpic76

Member
Licensed User
Longtime User
Yes Erel. I tried also to set the notification to "true" but without success.
From my documentation I've to enable the Client Characteristics Configuration to enable indications that we can for simplicity sake also consider notification.
In the list of characteristics I don't see the 0x2902 and 0x2901 descriptor that I can see in the PC Software.
How can I solve this empasse?
Regards Mosè.
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
Dear Erel,
I tried the code you posted in the last post and the outuput on the log is the following:

** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Found: Bluegiga CR Demo, 00:07:80:A7:44:6E, RSSI = -77, (MyMap) {1=[B@4273aea8, 7=[B@4273af10, 9=[B@4273af60}
Discovering services.
Connected
e7add780-b042-4876-aae1-112855353cc1
00002a00-0000-1000-8000-00805f9b34fb
00002a01-0000-1000-8000-00805f9b34fb

The characteristic I'm interested is "e7add780-b042-4876-aae1-112855353cc1" it's configured as write indicate in properties (0x28), below I'll find the gatt.xml used to configure the device, a Bluegiga BLE112, as "Cable replacement service":

B4X:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

    <service uuid="1800">
      <description>Generic Access Profile</description>

      <characteristic uuid="2a00">
        <properties read="true" const="true" />
        <value>Bluegiga CR Demo</value>
      </characteristic>

      <characteristic uuid="2a01">
        <properties read="true" const="true" />
        <value type="hex">4142</value>
      </characteristic>
    </service>
   
    <service uuid="0bd51666-e7cb-469b-8e4d-2742f1ba77cc" advertise="true">
        <description>Cable replacement service</description>
       
        <characteristic uuid="e7add780-b042-4876-aae1-112855353cc1" id="xgatt_data">
            <description>Data</description>
            <properties write="true" indicate="true" />
            <value variable_length="true" length="20" type="user" />
        </characteristic>
    </service>
   
</configuration>

It seems I need to enable the indications but I wasn't able to do so with SetNotify.
You can find additional information in the following post and youtube video:

https://bluegiga.zendesk.com/entrie...nfiguration-descriptor-from-iOS8-Objective-C-


Best Regards.
Mosè
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
Hi Erel,
I used SetNotify and the descriptor is set successfully but then DataAvailable event isn't raised; you can see the log below:

Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Found: Bluegiga CR Demo, 00:07:80:A7:44:6E, RSSI = -81, (MyMap) {1=[B@4273e660, 7=[B@4273e6c8, 9=[B@4273e718}
Discovering services.
Connected
Setting descriptor. Success = true
writing descriptor: true
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
e7add780-b042-4876-aae1-112855353cc1
00002a00-0000-1000-8000-00805f9b34fb
00002a01-0000-1000-8000-00805f9b34fb

The manufacturer recommends to write 0x02, 0x00 in little endian format to the Client Characteristics Configuration (UUID: 2902) but I don't that characteristic.
Thanks in advance.
Mosè.
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
Hi Erel,
I use your BLEexample and adding some log messages as you can see in the following code:

B4X:
Sub DataAvailable (Service As String, Characteristics As Map)
    pbReadData.Visible = False
    Log("Log in DataAvailable")
    Log("Service: "&Service)
    clv.Add(CreateServiceItem(Service), 30dip, "")
    For Each id As String In Characteristics.Keys
        Log("Log in DataAvailable")
        Log("Characteristic Key: "&id&" Characteristic Item: "& Characteristics.Get(id))
        clv.Add(CreateCharacteristicItem(id, Characteristics.Get(id)), 40dip, "")
    Next
End Sub

....

Sub CreateServiceItem (service As String) As Panel
    Dim pnl As Panel
    pnl.Initialize("")
    pnl.Color = 0xFF808080
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = service
    Log("Log in CreateServiceItem")
    Log("service: "&service)
    lbl.Gravity = Gravity.CENTER
    lbl.Typeface = Typeface.DEFAULT_BOLD
    pnl.AddView(lbl, 0, 0, clv.AsView.Width, 30dip)
    Return pnl
End Sub

Sub CreateCharacteristicItem(Id As String, Data() As Byte) As Panel
    Dim pnl As Panel
    pnl.Initialize("")
    pnl.Color = Colors.White
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = Id
    pnl.AddView(lbl, 0, 0, clv.AsView.Width, 20dip)
    Log("Log in CharacteristicItem")
    Log("Id: "&Id)
    Dim lbl2 As Label
    lbl2.Initialize("")
    Try
        lbl2.Text = BytesToString(Data, 0, Data.Length, "UTF8")
        Log("Log in CharacteristicItem")
        Log("Data Length: "&Data.Length&" Data: "&lbl2.Text)
    Catch
        Log(LastException)
        lbl2.Text = "Error reading data as string"
    End Try
    lbl2.TextColor = 0xFF909090
    lbl2.TextSize = 14
    pnl.AddView(lbl2, 0, 20dip, clv.AsView.Width, 20dip)
    Return pnl
End Sub

I obtain this log:

** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Found: Bluegiga CR Demo, 00:07:80:A7:44:6E, RSSI = -74, (MyMap) {1=[B@427b8848, 7=[B@427b88b0, 9=[B@427b8900}
Discovering services.
Connected
Log in DataAvailable
Service: 0bd51666-e7cb-469b-8e4d-2742f1ba77cc
Log in CreateServiceItem
service: 0bd51666-e7cb-469b-8e4d-2742f1ba77cc
Log in DataAvailable
Characteristic Key: e7add780-b042-4876-aae1-112855353cc1 Characteristic Item: [B@427c1f08
Log in CharacteristicItem
Id: e7add780-b042-4876-aae1-112855353cc1
Log in CharacteristicItem
Data Length: 0 Data:
Log in DataAvailable
Service: 00001800-0000-1000-8000-00805f9b34fb
Log in CreateServiceItem
service: 00001800-0000-1000-8000-00805f9b34fb
Log in DataAvailable
Characteristic Key: 00002a00-0000-1000-8000-00805f9b34fb Characteristic Item: [B@4266b140
Log in CharacteristicItem
Id: 00002a00-0000-1000-8000-00805f9b34fb
Log in CharacteristicItem
Data Length: 16 Data: Bluegiga CR Demo
Log in DataAvailable
Characteristic Key: 00002a01-0000-1000-8000-00805f9b34fb Characteristic Item: [B@427bf788
Log in CharacteristicItem
Id: 00002a01-0000-1000-8000-00805f9b34fb
Log in CharacteristicItem
Data Length: 2 Data: AB
** Activity (main) Pause, UserClosed = false **

I hope this can be usefull for you to solve my problem.
Best Regard.
Thank in advance.
Mosè
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The correct way to write 2 to this characteristic is with:
B4X:
Manager.WriteData("0bd51666-e7cb-469b-8e4d-2742f1ba77cc", "e7add780-b042-4876-aae1-112855353cc1", Array As Byte(2))
Also try:
B4X:
Manager.SetNotify("0bd51666-e7cb-469b-8e4d-2742f1ba77cc", "e7add780-b042-4876-aae1-112855353cc1", True)
Don't run them at the same time. Use CallSubPlus or a timer to run the make the first call and then make the second one after a second.
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
Hi Erel,
I tried the CallSubPlus without success.
Infact I'm not sure how to implement the CallSubPlus or the timer so can you please can give me an example?
Moreover it seems the bluegiga has the descriptor 2902 predefined as Client Characteristics Configuration but I don't see it in the list of the services or characteristics.
Thanks in advance.
Mosè
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add CallSubUtils module: https://www.b4x.com/android/forum/threads/callsubplus-callsub-with-explicit-delay.60877/#content and add csu to the Starter service.
B4X:
Sub WriteData
 Manager.WriteData("0bd51666-e7cb-469b-8e4d-2742f1ba77cc", "e7add780-b042-4876-aae1-112855353cc1", ArrayAs Byte(2))
End Sub

Sub SetNotify
Manager.SetNotify("0bd51666-e7cb-469b-8e4d-2742f1ba77cc", "e7add780-b042-4876-aae1-112855353cc1", True)
End Sub

'after connection
Starter.csu.CallSubPlus(Me, "WriteData", 1000)
Starter.csu.CallSubPlus(Me, "SetNotify", 2000)

All the discovered characteristics are listed. There are no other characteristics that you can access.
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
I tried your code all is ok but I don't receive response from the device :(
I continue to debug the app and probably I'llcontact bluegiga support for more information.
Thank you Erel.
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
Dear all,
it seems you need to enable the Client Characteristics Configuration (UUID: 2902) on the android device as stated in the following post of stackoverflow:

"I think is a litte bit late for give an answer but today I had the same doubt and I found a clear answer. Using setCharacteristicNotification() you enable notification localy (on android device) and setting CCC descriptor to ENABLE_NOTIFICATION_VALUE you enable notification on ble peripheral. In fact for enabling CCC notification you have to use setValue() and writeDescriptor() that are methods used for writing characteristics (in this case characteristics descriptors) to remote device. I found this on: http://processors.wiki.ti.com/index.php/SensorTag_User_Guide"

link: http://stackoverflow.com/questions/...otification-not-actually-enable-notifications

So is it possible to enable the characteristic on the android device using the ble2 library?

Regards,
Mosè
 
Upvote 0

elpic76

Member
Licensed User
Longtime User
Thank you Erel but probably I'm misunderstanding the task of SetNotify.
To communicate with the server (BLE112) I've to enable the indications on client (android device and UUID: 2902) writing 2.
If the SetNotify write 1 the notifications are enabled and not the indications.
Is it correct?
Regard Mosè
 
Upvote 0

ShaiS

Member
Licensed User
Longtime User
Please help me, I have the same problem how can I write to Client Characteristics Configuration (UUID: 0x2902) to enable indications or notification? My code is here
B4X:
Sub ble_DeviceFound(Name As String, DeviceId As String, AdvertisingData As Map, RSSI As Double)       
    Diag.Write(1,"Device found")
    manager.StopScan
    manager.Connect(DeviceId)
End Sub
Sub ble_Connected(Services As List)   
    manager.SetNotify("00001810-0000-1000-8000-00805f9b34fb","00002a35-0000-1000-8000-00805f9b34fb",True)
   
'    For i=0 To Services.Size-1
'    Diag.Write(1,Services.Get(i))
'    Next
   
End Sub
log file

upload_2016-4-28_15-43-54.png

But readings not coming from device

Thanks in advance
 
Upvote 0
Top