Android Question BLEManager2.Connect delay in getting service results

mdehrnsb

Member
Licensed User
Longtime User
I have been developing an app to connect to my BLE embedded system based on a TI 2640 SoC. I have had some success but have found that once a Scan() is completed and the Connect() command is issued with the appropriate ID, the amount of time required prior to the "Connected" event being raised can vary from a second or two to 45 seconds or more. I have noted that the library generates a log message "Discovering Services". This message is also delayed until just prior to the Connected event occurring.

I have tried several scanner apps and the TI development app with my embedded system and in all cases, the services are discovered in a fairly brief period, about 2 - 4 seconds consistently.

A second observation that seems to be connect to the timeliness of the connection is Activity Control. I maintain all of the BLE related events in the Starter file. After a successful Connected event has occurred, I call the ReadData() command with my custom service as the parameter. This generates a DataAvailable event. At the end of and within the ReadData event, I call a function that is a member of the Main activity file which stops a spinner icon and starts a different activity. If the service discovery is very fast (within a second or two), the second activity starts briefly but then I get the following in the log:

** Activity (auxconfiguration) Pause, UserClosed = false **
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

The peculiar issue is that if the amount of time required to discover the services is greater than about 5 seconds, this activity control issue does not occur. My configuration activity is not paused and Main is not killed.

Any suggestions would be appreciated.

Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The connection is made of two steps. First it connects to the peripheral device and then it discovers the services. Based on your description it is the first step that is slow.
There is nothing that can be done to make it faster. It is a simple call to the native API.

However the time it takes to connect shouldn't affect the activities.

Please post the relevant code and I'm sure that we will be able to solve this issue.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
I have been developing an app to connect to my BLE embedded system based on a TI 2640 SoC. I have had some success but have found that once a Scan() is completed and the Connect() command is issued with the appropriate ID, the amount of time required prior to the "Connected" event being raised can vary from a second or two to 45 seconds or more. I have noted that the library generates a log message "Discovering Services". This message is also delayed until just prior to the Connected event occurring.

I have tried several scanner apps and the TI development app with my embedded system and in all cases, the services are discovered in a fairly brief period, about 2 - 4 seconds consistently.

A second observation that seems to be connect to the timeliness of the connection is Activity Control. I maintain all of the BLE related events in the Starter file. After a successful Connected event has occurred, I call the ReadData() command with my custom service as the parameter. This generates a DataAvailable event. At the end of and within the ReadData event, I call a function that is a member of the Main activity file which stops a spinner icon and starts a different activity. If the service discovery is very fast (within a second or two), the second activity starts briefly but then I get the following in the log:

** Activity (auxconfiguration) Pause, UserClosed = false **
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

The peculiar issue is that if the amount of time required to discover the services is greater than about 5 seconds, this activity control issue does not occur. My configuration activity is not paused and Main is not killed.

Any suggestions would be appreciated.

Thank you.


I find this as well, i think it is just the way it is! It does seem quite reliable though.
Steve
 
Upvote 0

mdehrnsb

Member
Licensed User
Longtime User
Erel,

I would agree that the timing is device dependent but I have tested different scanner apps and the TI development app on the same phone with the same embedded system and they all connect consistently faster. I am assuming that these other apps are written in native Java but that is only a guess. Based on that information, it seems that there is some other reason that the B4A version is slower. I am currently running library version 1.15. Is there a newer version available?

Regarding an additional request, can you add an event call that is raised after a characteristic is written? My embedded system returns error codes if parameter data is invalid and I currently have no access to this data. Having an event raised after each transmission would also allow write commands to be queued and sent after each transmission event occurred. Currently, I have to used delayed sub calls to do this when sending multiple characteristic updates and this is not very ideal.

Thank you
Mark
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

mdehrnsb

Member
Licensed User
Longtime User
Erel,

Thank you for your efforts. I appreciate that you are responsive to the posts in a timely manner. I will try this new library this evening, local time and let you know the results.

Mark.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
It doesn't matter whether the app is written in Java or B4A. What matters is which API it uses to connect.

I've released a new version (1.16): https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/page-3#post-397828
Try to use Connect2 instead of Connect with AutoConnect parameter set to False. Probably related to the slow connection: http://stackoverflow.com/questions/22214254/android-ble-connect-slowly

There is also a new WriteComplete event in this version.

Just an observation:

With Library 1.16 and Connect i have seen the time between the connect call and the Manager_Connected call back vary between 1,2,3 and up to 30 seconds.
The time between connect2 and Manager_connected has been around 1.5 to 2 seconds so far (about 5 tries).

Steve

Just noticed the earlier number of services fix seems to be broken - getting 3 in the list but there should be 5.
 
Last edited:
Upvote 0

mdehrnsb

Member
Licensed User
Longtime User
Erel,

I have tried the new connect2 function and it seems to work very well. Connection time typically 2 - 5 seconds. Thank you for the help.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Hi,

I have noticed the log, after connecting, contains..

B4X:
Discovering services.
Setting descriptor. Success = true
writing descriptor: true

This is not a problem but i just wondered if it should be there.

Steve
 
Upvote 0
Top