B4A Library BleExtended

Hello,

I made some additions into Ble Library (write, notifications) and Erel asked me to post the extended library here. I made those additions to get my prototype working, so it is not necessarily of production quality but anayway works well for me just now.

I use this library exactly as the original one (+ additons of course) and I compile it with SimpleLibraryCompiler. It has same name and version number as the original.

The library source code is in the zip file.
 

Attachments

  • BleManager.zip
    3.5 KB · Views: 1,278

mkh42

Member
Licensed User
Longtime User
eventually i have find the solution to solve the ugly hack which remains in bleSendData.

Here you can find where the problem comes from.

http://stackoverflow.com/questions/...tooth-low-energy-service-discovery-on-android

There is obviously a cache in the gatt implementation which prevents to make a scan on the same devices.

My hack clears this cache but there seems to be also a gatt.refresh() method which can be called (but its seems not so easy because you have to do it by java reflection)

I recognized that the app 'nRF Master Control Panel' also calls gatt.refresh(). so that seems the way to go.

I will give this a try.

If I'm successful i will post it here.

greetings
mkh42
 

Denny Hopp

Member
Licensed User
Longtime User
Hi mkh -- I should have seen that mistake previously! I write the string to the characteristic variable then perform the actual write. I can now write to one characteristic and read from another (in addition to writing/reading to both). I am still using the original library since it works fine with that minor change.
 

mkh42

Member
Licensed User
Longtime User
Hi Denny,
its absolute fine to use the original BleExtended lib. As i stated i didn't find an error in the lib until now. The error was only in the example.
My extension to the library are mostly useful to get more information about the characteristics.

My remaining problem is to disconnect reliable from my device.
If i press disconnect button I execute manager.disconnect. I also see that the callback ble_disconnected is called.
I also checked back in the lib that during that callback also a manager.close ist execute so the device should be disconnected.
But if I press connect again my scan doesn't find my device.

A method to solve this problem is to disable bluetooth for a moment and start it again. Then the next scan is successful.

I also made some tests with the above mentioned gatt.refresh() method. I added it to my lib.
I can now more reliable connect ,disconnect, connect, disconnect .. but still sometimes the scan fails to detect my device.

I can bring this to work in the form that i only use Bluetooth.disable/enable in the case i don't find something but it is still not a really clean solution.

I would like to get that reliable.

greetings
mkh42
 

gelay

Member
Licensed User
Longtime User
Hi Denny,
its absolute fine to use the original BleExtended lib. As i stated i didn't find an error in the lib until now. The error was only in the example.
My extension to the library are mostly useful to get more information about the characteristics.

My remaining problem is to disconnect reliable from my device.
If i press disconnect button I execute manager.disconnect. I also see that the callback ble_disconnected is called.
I also checked back in the lib that during that callback also a manager.close ist execute so the device should be disconnected.
But if I press connect again my scan doesn't find my device.

A method to solve this problem is to disable bluetooth for a moment and start it again. Then the next scan is successful.

I also made some tests with the above mentioned gatt.refresh() method. I added it to my lib.
I can now more reliable connect ,disconnect, connect, disconnect .. but still sometimes the scan fails to detect my device.

I can bring this to work in the form that i only use Bluetooth.disable/enable in the case i don't find something but it is still not a really clean solution.

I would like to get that reliable.

greetings
mkh42

I'm using your extextended lib and really like the new features.
What do you think can you manage to put the RSSI measurement in it? That would be cool!
 

sdujolo

Member
Licensed User
Longtime User
Hi Denny,
its absolute fine to use the original BleExtended lib. As i stated i didn't find an error in the lib until now. The error was only in the example.
My extension to the library are mostly useful to get more information about the characteristics.

My remaining problem is to disconnect reliable from my device.
If i press disconnect button I execute manager.disconnect. I also see that the callback ble_disconnected is called.
I also checked back in the lib that during that callback also a manager.close ist execute so the device should be disconnected.
But if I press connect again my scan doesn't find my device.

A method to solve this problem is to disable bluetooth for a moment and start it again. Then the next scan is successful.

I also made some tests with the above mentioned gatt.refresh() method. I added it to my lib.
I can now more reliable connect ,disconnect, connect, disconnect .. but still sometimes the scan fails to detect my device.

I can bring this to work in the form that i only use Bluetooth.disable/enable in the case i don't find something but it is still not a really clean solution.

I would like to get that reliable.

greetings
mkh42
Is the lib updated with gatt.refresh()?

Regards /Jörgen
 

mkh42

Member
Licensed User
Longtime User
@gelay
I will take a look on it

@sdujolo
I'm still working on my remaining problem. I built in the refresh function in the library but until now i do not post the updated library here.
I'm not sure if refresh solves the problem I made progress with and without calling refresh() but could not really come to the point that I can Connect/disconnect as often as I wish without problems.
During my problem solving tries one of the things i see is that after a disconnect i got more and more onConnectionStateChange (instead of only one if i disconnect the first time) if i often connect/disconnect to the device. So my impression is there is still some kind of problem concerning connect/disconnect cycle. I will try to eliminate this before i post an updated library.


greetings
Mkh42
 

sdujolo

Member
Licensed User
Longtime User
I have no problems with connect/disconnect but I am running android 5.01 so maybe it is fixed. There also new functionality in android 5 can you add this function also? I can make donation for that.

Best regards /Jörgen
 

Denny Hopp

Member
Licensed User
Longtime User
I have my App running using the Microchip RN4020 BTLE module. It is writing and reading two different characteristics and plotting real-time data (along with controlling LED's on the Microchip Explorer board). Looks like the BTLE Extended library is working fine.
 

mkh42

Member
Licensed User
Longtime User
i fixed the problem down. It was again an error it in the BleSendData example and not in the library. The state handling in the example was not sufficient.
It is very important if you use BleManager that you track your state properly.
Example given: after you call Manger.Connect(MacAddress) you have to take care that you never call Manger.Connect again before you call Manger.disconnect and Manager.close.


That's what could happen in the BleSendData example and made the problems. I now implement a proper state tracking and the problems are gone.

I also extend the BleExtEx lib (now Version 1.10) with Rssi Tracking if you are interested.
To use it take a look in the BleSendData_V2 example. You can only get rssi from a device you are connected to. You can choose the interval Rssi is updated. Default is 2000ms but you can change this value with a call to the new SetRssiTrackingInterval method.


I made a change in BleExtEx which is very important to know about if you now call Manager.disconnect there is no automatic call to Manager.Close anymore.
Therfore you should normally place a call to Manager.Close in the disconnect callback.
This is for future use because then it is possible to implement a quick connect/disconnect and one is a bit more flexible in this point perhaps to call the mentioned refresh method to solve some fancy problems. (see some posts above)

The manager.refresh method as I just stated is also implemented in the BleExtEx v1.10 but was not necessary to solve my problem.


@Sdulolo: unfortunately I do not own a Android 5 phone until now so testing would be a little bit difficult. Can you tell me the features you are interested in ?

greetings
mkh42
 

Attachments

  • BleExtEx_1_10.zip
    16.2 KB · Views: 573
  • blueSenddata_V2.zip
    5.2 KB · Views: 463
Last edited:

sdujolo

Member
Licensed User
Longtime User
This is new for 5.0 https://developer.android.com/reference/android/bluetooth/le/package-summary.html

The ScanFilter and ScanSettings looks interesting.

Android 4.3 introduced platform support for Bluetooth Low Energy (Bluetooth LE) in the central role. In Android 5.0, an Android device can now act as a Bluetooth LE peripheral device. Apps can use this capability to make their presence known to nearby devices. For instance, you can build apps that allow a device to function as a pedometer or health monitor and communicate its data with another Bluetooth LE device.

The new android.bluetooth.le APIs enable your apps to broadcast advertisements, scan for responses, and form connections with nearby Bluetooth LE devices. To use the new advertising and scanning features, add the BLUETOOTH_ADMIN permission in your manifest. When users update or download your app from the Play Store, they are asked to grant the following permission to your app: "Bluetooth connection information: Allows the app to control Bluetooth, including broadcasting to or getting information about nearby Bluetooth devices."

To begin Bluetooth LE advertising so that other devices can discover your app, call startAdvertising() and pass in an implementation of the AdvertiseCallback class. The callback object receives a report of the success or failure of the advertising operation.

Android 5.0 introduces the ScanFilter class so that your app can scan for only the specific types of devices it is interested in. To begin scanning for Bluetooth LE devices, call startScan() and pass in a list of filters. In the method call, you must also provide an implementation of ScanCallback to report when a Bluetooth LE advertisement is found.
 

mkh42

Member
Licensed User
Longtime User
@sdujolo sounds interesting. But i'm sorry without and Android 5.0 device to test and considering my spare freetime it is not possible for me to implement this at moment.
 

mdehrnsb

Member
Licensed User
Longtime User
I am using the bleextended library with some success but I am getting a null pointer exception whenever I try to use the manager.SetCharacteristicNotification() function. I have tried this with the BLE send example and with my own code. Any ideas?
 

gelay

Member
Licensed User
Longtime User
i fixed the problem down. It was again an error it in the BleSendData example and not in the library. The state handling in the example was not sufficient.
It is very important if you use BleManager that you track your state properly.
Example given: after you call Manger.Connect(MacAddress) you have to take care that you never call Manger.Connect again before you call Manger.disconnect and Manager.close.


That's what could happen in the BleSendData example and made the problems. I now implement a proper state tracking and the problems are gone.

I also extend the BleExtEx lib (now Version 1.10) with Rssi Tracking if you are interested.
To use it take a look in the BleSendData_V2 example. You can only get rssi from a device you are connected to. You can choose the interval Rssi is updated. Default is 2000ms but you can change this value with a call to the new SetRssiTrackingInterval method.


I made a change in BleExtEx which is very important to know about if you now call Manager.disconnect there is no automatic call to Manager.Close anymore.
Therfore you should normally place a call to Manager.Close in the disconnect callback.
This is for future use because then it is possible to implement a quick connect/disconnect and one is a bit more flexible in this point perhaps to call the mentioned refresh method to solve some fancy problems. (see some posts above)

The manager.refresh method as I just stated is also implemented in the BleExtEx v1.10 but was not necessary to solve my problem.


@Sdulolo: unfortunately I do not own a Android 5 phone until now so testing would be a little bit difficult. Can you tell me the features you are interested in ?

greetings
mkh42

@mkh42

Great! You made my day with the Rssi tracking!! Thanks :)
 

mkh42

Member
Licensed User
Longtime User
@gelay
I'm happy you like it.

@mdehrnsb
do you use bluesendData_V2 example?
what device do you use ?

Are you sure your device has a "Read Characteristic" with "Notify" property?
You can only call SetCharacteristicNotification with a Characteristic which has the "Notify" property.
you can check this in the dump bluesendData_V2 makes during connect or you can use the "nrf master Control panel" app from playstore a very useful tool for playing with BLE devices
 
Last edited:

gmh

New Member
Licensed User
Longtime User
@mkh42
Luv'n your work.

Have been able to connect to and read characteristics from a Casio GBA-400 and an old GB-6900.
Thanks :)
 

robertyoungs

Member
Licensed User
Longtime User
@mkh42
Thank you so much for this. Working through your examples and spinning off into specs etc was a great way to understand the nitty-gritty of BLE

For ref, this all works with the RedBearLabs Blend Micro Arduino simply (eventually!) by swapping read/write characteristics, forgetting the ble_CharacteristicRead, enabling Button3 and adding cwrite.SetStringValue("test") to Button3_Click. And all works with the Blend Micro SimpleChat sketch

This is a massive contribution bearing in mind the importance of BLE now - still can't find anything B4A can't do yet!:)
 

Denny Hopp

Member
Licensed User
Longtime User
I now have the BLE library running perfectly with the Microchip RN4020 BTLE module. It implements a read characteristic, write characteristic, and notify characteristic. Each of the characteristics are private characteristics. Everything is working great! The App performs real time graphing from the data received and also control LED's on the target board. I will be putting together a presentation to describe the entire project, including the C code that is on a Microchip PIC24 that communicates with the RN4020. I am using a Microchip Explorer16 board with a RN4020 PicTail for the target.
 

KY Leng

Active Member
Licensed User
Longtime User
i fixed the problem down. It was again an error it in the BleSendData example and not in the library. The state handling in the example was not sufficient.
It is very important if you use BleManager that you track your state properly.
Example given: after you call Manger.Connect(MacAddress) you have to take care that you never call Manger.Connect again before you call Manger.disconnect and Manager.close.


That's what could happen in the BleSendData example and made the problems. I now implement a proper state tracking and the problems are gone.

I also extend the BleExtEx lib (now Version 1.10) with Rssi Tracking if you are interested.
To use it take a look in the BleSendData_V2 example. You can only get rssi from a device you are connected to. You can choose the interval Rssi is updated. Default is 2000ms but you can change this value with a call to the new SetRssiTrackingInterval method.


I made a change in BleExtEx which is very important to know about if you now call Manager.disconnect there is no automatic call to Manager.Close anymore.
Therfore you should normally place a call to Manager.Close in the disconnect callback.
This is for future use because then it is possible to implement a quick connect/disconnect and one is a bit more flexible in this point perhaps to call the mentioned refresh method to solve some fancy problems. (see some posts above)

The manager.refresh method as I just stated is also implemented in the BleExtEx v1.10 but was not necessary to solve my problem.


@Sdulolo: unfortunately I do not own a Android 5 phone until now so testing would be a little bit difficult. Can you tell me the features you are interested in ?

greetings
mkh42

Good morning mh42,

Thank for updating the library. Since the first release of BLE library, I could not event find my HM-10 (My phone is LG-E975 updated to Android 4.4.2 and it work with some app from Google Play Store).
Now, with your new library, I could not successfully compile the code to test. I got the error as shown below:

Parsing code. 0.00
Compiling code. 0.03
Compiling layouts code. 0.00
Generating R file. 0.02
Compiling generated Java code. Error
B4A line: 93
s = Services.GetValueAt(i)
javac 1.7.0_71
src\b4a\example\main.java:532: error: cannot find symbol
_s.setObject((android.bluetooth.BluetoothGattService)(_services.GetValueAt(_i)));
^
symbol: class BluetoothGattService
location: package android.bluetooth
1 error


I test it with both last extended library of your.
Could you please tell me what is the problem ? I do not know Java.

Looking forward to hearing from you soon,

Best regards.
 
Top