Android Question Problem using Ble2

EApreda

Member
Licensed User
Longtime User
Hi Erel
I need to receive data from a ble by UART. Using old bleexended library it works, but using new ble2 library when calling ReadData, i receive the error :No matching characteristic found.

i post my code:

Sub Process_Globals
Dim manager As BleManager2
Dim mappa As Map

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim bc As ByteConverter
Private Button1 As Button
Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("ble2")
If FirstTime Then
manager.Initialize("ble")
End If
End Sub

Sub ble_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Dim i As Int
Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
Dim data1() As Byte = AdvertisingData.Get(75)

Label1.Visible = True
Label1.Text = "Name: " & Name & CRLF & "Mac: " & Id & CRLF & "Data: " & bc.HexFromBytes(data1)

End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ble_Connected(services As List)
Log("Connected")
'manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e","6e400003-b5a3-f393-e0a9-e50e24dcca9e",True)
'manager.ReadData2("6e400001-b5a3-f393-e0a9-e50e24dcca9e","6e400003-b5a3-f393-e0a9-e50e24dcca9e")

For Each s As String In services
Log(s)
manager.ReadData(s)
Next


End Sub
Sub ble_DataAvailable(ServiceId As String, Characteristics As Map)
Log("receiving from " & ServiceId)

For Each id As String In Characteristics.Keys
Dim data3() As Byte = Characteristics.Get(id)
Log(id)
Log(bc.StringFromBytes(data3,"UTF8"))
Next
End Sub

Sub Button1_Click
Log("searching")
manager.Scan2(Null,True)

End Sub

Sub Label1_Click

manager.StopScan
manager.Connect("F8:61:A6:3D:39:AD")


End Sub


my Log is:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
searching
Found: Adafruit Bluefruit LE, F8:61:A6:3D:39:AD, RSSI = -77, (MyMap) {1=[B@423ed250, 75=[B@423ed2b8, 9=[B@423ed2f8}
Adafruit Bluefruit LE
Discovering services.
Connected
00001800-0000-1000-8000-00805f9b34fb
00001801-0000-1000-8000-00805f9b34fb
00001530-1212-efde-1523-785feabcd123
0000180a-0000-1000-8000-00805f9b34fb
6e400001-b5a3-f393-e0a9-e50e24dcca9e
ee0c2080-8786-40ba-ab96-99b91ac981d8
receiving from 00001800-0000-1000-8000-00805f9b34fb
00002a00-0000-1000-8000-00805f9b34fb
Adafruit Bluefruit LE
00002a01-0000-1000-8000-00805f9b34fb
����
00002a04-0000-1000-8000-00805f9b34fb
��P�������
receiving from 00001530-1212-efde-1523-785feabcd123
00001534-1212-efde-1523-785feabcd123
��
receiving from 0000180a-0000-1000-8000-00805f9b34fb
00002a29-0000-1000-8000-00805f9b34fb
Adafruit Industries
00002a24-0000-1000-8000-00805f9b34fb
BLEFRIEND32
00002a28-0000-1000-8000-00805f9b34fb
0.6.7 - Sep 17 2015
00002a26-0000-1000-8000-00805f9b34fb
S110 8.0.0, 0.2
00002a27-0000-1000-8000-00805f9b34fb
QFACA10
receiving from ee0c2080-8786-40ba-ab96-99b91ac981d8
ee0c2081-8786-40ba-ab96-99b91ac981d8
��
ee0c2084-8786-40ba-ab96-99b91ac981d8
��adafruit
ee0c2085-8786-40ba-ab96-99b91ac981d8
ee0c2087-8786-40ba-ab96-99b91ac981d8
ee0c2088-8786-40ba-ab96-99b91ac981d8
d��
** Activity (main) Pause, UserClosed = true **
** Activity (main) Resume **

as you can see, it works for all characteristic, but not for 6e400001-b5a3-f393-e0a9-e50e24dcca9e that is the UART characteristic i need.

using

manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e","6e400003-b5a3-f393-e0a9-e50e24dcca9e",True)

and

manager.ReadData2("6e400001-b5a3-f393-e0a9-e50e24dcca9e","6e400003-b5a3-f393-e0a9-e50e24dcca9e")

i have: No matching characteristic found

P.s. i need to use ble2 because i have to read some data that i send in GAP

can you help me Pease? thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
What is the output of this code:
B4X:
Dim jo As JavaObject = manager
Dim service1 As JavaObject = jo.runMethod("getService", Array("6e400001-b5a3-f393-e0a9-e50e24dcca9e"))
Dim chrs As List = service1.RunMethod("getCharacteristics", Null)
For Each c As JavaObject In chrs
 Log(c.RunMethod("getUuid", null)
Next
Call it from the connected sub.
 
Upvote 0

EApreda

Member
Licensed User
Longtime User
What is the output of this code:
B4X:
Dim jo As JavaObject = manager
Dim service1 As JavaObject = jo.runMethod("getService", Array("6e400001-b5a3-f393-e0a9-e50e24dcca9e"))
Dim chrs As List = service1.RunMethod("getCharacteristics", Null)
For Each c As JavaObject In chrs
Log(c.RunMethod("getUuid", null)
Next
Call it from the connected sub.

It give me a compiling error

Parsing code. 0.00
Compiling code. Error
Error compiling program.
Error description: 'as' expected.
Occurred on line: 92
Dim service1 As JavaObject = jo.runMethod("getService", Array("6e400001-b5a3-f393-e0a9-e50e24dcca9e"))
Word: (
 
Upvote 0

EApreda

Member
Licensed User
Longtime User
I used this code:

B4X:
Sub ble_Connected(services As List)
Log("Connected")
   
Dim jo As JavaObject = manager
Dim service1 As JavaObject = jo.runMethod("getService", Array As Object("6e400001-b5a3-f393-e0a9-e50e24dcca9e"))
Dim chrs As List = service1.RunMethod("getCharacteristics", Null)
For Each c As JavaObject In chrs
Log(c.RunMethod("getUuid", Null))
Next
   
End Sub

and i have this log

Connected
6e400003-b5a3-f393-e0a9-e50e24dcca9e
6e400002-b5a3-f393-e0a9-e50e24dcca9e

one is the TX characteristic, and the other is RX characteristic
 
Upvote 0

EApreda

Member
Licensed User
Longtime User
16 means that you cannot directly read this char. You need to use SetNotify.

What happens when you call SetNotify and wait for the data to change?

Nothing..the event ble_DataAvailable is never raised..
Using the old ble library, with SetCharacteristicNotification works.

p.s. it's normal that i need to invoke StopScan before Connect? If no,the program crash!
 
Upvote 0

EApreda

Member
Licensed User
Longtime User
1. No error is logged. The program on the phone crash, and the bridge disconnect

2.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("ble")
   
If FirstTime Then
    manager1.Initialize("manager")
End If
End Sub
Sub Button1_Click
    Log("searching")
   
    manager1.Scan(10000,Null)
End Sub
Sub manager_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ", " & MacAddress)
   manager.Connect(MacAddress, True)
  
End Sub

Sub manager_Connected(Services As Map)
mappa = Services
   
Dim s As BleService = mappa.Get("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
Dim c As BleCharacteristic = s.GetCharacteristics.Get("6e400003-b5a3-f393-e0a9-e50e24dcca9e")
manager1.SetCharacteristicNotification(c,True)
End Sub
Sub manager_CharacteristicChanged (Characteristic As BleCharacteristic)
  
   Log("Change: " & Characteristic.GetStringValue(0))    
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No error is logged. The program on the phone crash, and the bridge disconnect
You need to connect to the device in USB debug mode and monitor the logs. There should be an error message in the logs.

Try the attached jar file (replace the existing BLE2 library). Please run your code that calls SetNotify and post the logs.
 
Upvote 0

Heli Pihlajamaa

New Member
Licensed User
Longtime User
Hi,

Is there any progress with this? I'm also using BLE module from Nordic Semiconductor and I need to get data notifications from nRFUart TX characteristic.

When I'm trying to set notifications with

B4X:
 Manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e", True)

it shows in log:

Setting descriptor. Success = true
writing descriptor: false
 
Upvote 0

Heli Pihlajamaa

New Member
Licensed User
Longtime User
I got the SetNotify working for me now.
My solution was to call the Manager.SetNotify twice in the Connected event with some delay in between, like so:

B4X:
Manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e", True)
Delay(500, True) '500ms, allow events
Manager.SetNotify("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e", True)
 
Upvote 0
Top