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,277

avalle

Active Member
Licensed User
Longtime User
Hi Denny, happy to see it helped.
The behavior you get is exactly the same that I described in msg #11.

Not sure why it happened, maybe @Erel has an idea on that?
 

Denny Hopp

Member
Licensed User
Longtime User
Hi Avalle -- I did some further testing and found that if i do a write then a read, the read value is not the same as the write value. I have to do two writes, then the read picks up the first write.
 

naveenpn

Member
Licensed User
Longtime User
Hi,
I'm trying to communicate (both read & write) with a BLE device that we have created using the Nordic Semiconductor nrf51822 BLE chip. It works perfectly fine using the Nordic's sample java app. I can read as well as write to the BLE device. However, I'm not a java person so I'm trying to use the B4A to develop an app.

I saw that there is a BLE library which doesn't support write. So I tried using the BLEextended library. I'm using the library from post#17 and example from post#13. I'm able to read the data from BLE device as expected. However, I'm unable to write any data back to the device. The BLE device has multiple characteristics & one is used for reading and another for writing. The Log(s.GetCharacteristics) returns the following info:
(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=android.bluetooth.BluetoothGattCharacteristic@42beee90, 6e400002-b5a3-f393-e0a9-e50e24dcca9e=android.bluetooth.BluetoothGattCharacteristic@42bef460}

The 6e400002-b5a3-f393-e0a9-e50e24dcca9e is the UUID for the read characteristics (which works fine) & 6e400003-b5a3-f393-e0a9-e50e24dcca9e is the UUID for write characteristic which I'm not able to get it working still.

I've modified the ble_Connected & Button3_Click subs in post#13 as follows:

B4X:
Sub ble_Connected (Services As Map)
   Dim n As Int
 
   Log("Connected")
   Button1.Enabled = True
   Button2.Enabled = False
   n = 2                        'Service 0000ffe0-0000-1000-8000-00805f9b34fb (serial data transfer on HM-10 module)
   s = Services.GetValueAt(n)                            ' Change index to access other Services
   Log("Service[" & n & "]: " & Services.GetKeyAt(n))    ' Log the UUID of the selected Service
   ' Read the manufacturer characteristic
   c = s.GetCharacteristics.GetValueAt(0)    'this returns the UUID for read characteristic
   d = s.GetCharacteristics.GetValueAt(1)    '<----- added - this returns the UUID for write characteristic
   Log(s.GetCharacteristics)
   Log("Charact[0]: " & c.Uuid)
   Log("Charact[1]: " & d.Uuid)           '<----- added
   manager.ReadCharacteristic(c)
   manager.SetCharacteristicNotification(c, True)    'Define a Notification to get data from HM-10 via Changed event
End Sub

B4X:
Sub Button3_Click                    'Send data to HM-10  
   Log("Message " & x)
   manager.WriteCharacteristic(d) '<--- changed argument to d (from c)
 
End Sub

When writing data, the app crashes with a message "Unfortunately Bluetooth Share has stopped".

Can someone more knowledgeable than me please help?
 

naveenpn

Member
Licensed User
Longtime User
Thanks for the response.

s.GetCharacteristics.Get("6e400002-b5a3-f393-e0a9-e50e24dcca9e") instead of GetValueAt.
This didn't help, the app is still crashing.

Check the logs when the app crashes. There must be an error message there.
Surprisingly there are no error messages in the log.

Here is the log from the launch of program till it crashes:
B4X:
** Activity (main) Create, isFirst = true **
BLE Supported: true
** Activity (main) Resume **
Nordic_UART, FB:2A:A7:85:C3:28
Connecting...
Discovering services.
Connected
Service[2]: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=android.bluetooth.BluetoothGattCharacteristic@42bf4cf8, 6e400002-b5a3-f393-e0a9-e50e24dcca9e=android.bluetooth.BluetoothGattCharacteristic@42bf52c8}
Charact[0]: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
Charact[1]: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
Message 0
 

naveenpn

Member
Licensed User
Longtime User
Ok, after using USB debug mode, I'm seeing this log:

B4X:
LogCat connected to: 4d003c4d499280d1
--------- beginning of /dev/log/main
running waiting messages (1)
** Activity (main) Resume **
** Service (service1) Destroy **
** Activity (main) Pause, UserClosed = true **
--------- beginning of /dev/log/system
** Activity (main) Create, isFirst = true **
BLE Supported: true
** Activity (main) Resume **
Nordic_UART, FB:2A:A7:85:C3:28
Connecting...
Discovering services.
Connected
Service[2]: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
(MyMap) {6e400003-b5a3-f393-e0a9-e50e24dcca9e=android.bluetooth.BluetoothGattCharacteristic@42bf3098, 6e400002-b5a3-f393-e0a9-e50e24dcca9e=android.bluetooth.BluetoothGattCharacteristic@42bf3668}
Charact[0]: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
Charact[1]: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
Message 0
Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 746 (Binder_8)

If it helps, I can provide Nordic Semiconductor's java source code which works fine.
 

naveenpn

Member
Licensed User
Longtime User
Hi Denny Hopp,
I'm using the hardware which is one of the development kits from Nordic Semi and it works perfectly fine with their own Java written app. So I'm suspecting something wrong either with my usage of the BLE/BLEextended library or the library itself.

The major difference of the Nordic firmware from other BLE modules is that they use two characteristics one for read and another for write and this is what seems to break the B4A BLE examples. I cannot change the firmware now since its more or less finalized & too little time left.

So I'm hoping B4A can help me out asap. This will be useful not just for me, but anyone else who wants to use Nordic + B4A.
 

Denny Hopp

Member
Licensed User
Longtime User
Hi naveenpn -- I am finding the same thing as you. I set up one private service and two characteristics for that service. I can only access the first characteristic and cannot even see the second one. I know that the second characteristic exists because I can see it on a BTLE browser ad can read/write it from that browser.
 

Denny Hopp

Member
Licensed User
Longtime User
Hi Erel -- The problem I am seeing is that if I create multiple private characteristics, I can only access one of them (both reads and writes).
 

Denny Hopp

Member
Licensed User
Longtime User
I can see two private characteristics now but cannot write to the second one. I can write to the one at the 0 index. I get the same fatal error.
 

naveenpn

Member
Licensed User
Longtime User
I don't think anybody else will be able to help. As pointed out in post#50, we are expected to solve this ourselves by going through the Java source of the library and fixing the problem! I find it easier to hire a Android-Java developer to get my app developed. I cannot justify having to use java in order to use B4A. Somehow beats the purpose. :(

I think this is that 1% of the applications where B4A cannot be used :(
 
Last edited:

merlin2049er

Well-Known Member
Licensed User
Longtime User
I'm not sure if this is an Android OS problem or not. I used an iphone and ran a ble scanning app on my hardware, and it returned more characteristics than the ble scanning app (which I downloaded) for the Android.
 

naveenpn

Member
Licensed User
Longtime User
I got everything working perfectly using Android Java including read/write & multiple characteristics.
 

Denny Hopp

Member
Licensed User
Longtime User
Hi naveenpn. I also had to resort back to Android Developer Studio and Java to get the BLE to work properly. It looks like there are no shortcuts if BLE is a requirement for the project.
 

mkh42

Member
Licensed User
Longtime User
Hello,
since a couple of days i tried to bring my RFdunio Led Button example to work with BleExtended library.
Reading was not a problem but writing has similar behavior then reported from some other a couple of posts above.

The trouble are:
Crash during writing a characteristics
One have two write two times to get the write value to the device

Fortunately I found the main issue of trouble writing to a characteristics people are experiencing.

There is one fatal error concerning the writing of a characteristics in the example 'Ble send data.zip' posted in this thread.

In this example if one presses the 'Send Data' button a call to

manager.WriteCharacteristic(c) 'c is the characteristics I want two write to

is done before a value is set. Setting the value is wrongly placed in the callback routine ble_CharacteristicWrite.

This is definitely the wrong place. In the callback the write to the device is already done.

You have to call SetStringValue before the first call of manager.WriteCharacteristic !!!!

so it should look like this

c.SetStringValue('Hallo') ' set the desired value this is only locally
manager.WriteCharacteristic(c) ' send it to the device

That is the main issue of the troubles writing a charateristics.

The setStringvalue routine only changes the value of the characteristics locally and the WriteCharacteristic send it to the device.

In the callback you can check if the write was successful and eventually read the characteristics.

The bleextended library itsself seems not to have an issue at least my Rfdunino example works fine with it.

I extend the BleExtended library a little bit to be able to get more infos about the characteristics but my example works also with the original library.

If someone is interested in the extended bleExtended library and my example I will post it here.

greetings

mkh42
 
Last edited:

gelay

Member
Licensed User
Longtime User
Hello,
since a couple of days i tried to bring my RFdunio Led Button example to work with BleExtended library.
Reading was not a problem but writing has similar behavior then reported from some other a couple of posts above.

The trouble are:
Crash during writing a characteristics
One have two write two times to get the write value to the device

Fortunately I found the main issue of trouble writing to a characteristics people are experiencing.

There is one fatal error concerning the writing of a characteristics in the example 'Ble send data.zip' posted in this thread.

In this example if one presses the 'Send Data' button a call to

manager.WriteCharacteristic(c) 'c is the characteristics I want two write to

is done before a value is set. Setting the value is wrongly placed in the callback routine ble_CharacteristicWrite.

This is definitely the wrong place. In the callback the write to the device is already done.

You have to call SetStringValue before the first call of manager.WriteCharacteristic !!!!

so it should look like this

c.SetStringValue('Hallo') ' set the desired value this is only locally
manager.WriteCharacteristic(c) ' send it to the device

That is the main issue of the troubles writing a charateristics.

The setStringvalue routine only changes the value of the characteristics locally and the WriteCharacteristic send it to the device.

In the callback you can check if the write was successful and eventually read the characteristics.

The bleextended library itsself seems not to have an issue at least my Rfdunino example works fine with it.

I extend the BleExtended library a little bit to be able to get more infos about the characteristics but my example works also with the original library.

If someone is interested in the extended bleExtended library and my example I will post it here.

greetings

mkh42

I do! Please share!
 

mkh42

Member
Licensed User
Longtime User
OK here the extended bleExtended library called BleExtEx and the modified example BleSendData.
From the BleExtEx i add a library and also the java source code i compiled it from.

The modification I made on BleSendData is that I correct the wrong write behavior described two posts above. I also add some more information in the dump of the characteristics.

Also I fit BleSendData to the RFDuino device I use but this is only a minor modification and should easily be fitted to other devices.

The three buttons should now work reliable So you should now be able to press connect , send some data, receive some data, press disconnect then press connect again ...

I made still a bit ugly hack in BleSendData to make the scan process reliable. If you press connect I switch off BT 3 seconds and switch it on again and wait 3seconds and then i begin to scan. So you have to wait a little until the scan find devices and connect. If someone has a cleaner reliable method i would like to hear about.
This hack would not be necessary if i press the connect button for the first time starting the app but if i press disconnect also i try to disconnect the device it is obviously not finally disconnected and i'm not able to discover the device again if I press connect again. Switching of bluetooth for 3 seconds obviously disconnects the RFduino reliable and so the rfduino is discoverable again.

All read/write 'Action' you can only see in the logs and on your device.

The BleExtEx has some extension mainly to get moer infos about the charateristics

You can see there usesage in the modified BleSendData example.

There are also some more Setvalue methods so that it is simpler to write integer or floats and not only strings.

You can now set also the writetype of the characteristic (default, no_respone, signed) but I didn't used it until now so its for future use.

I want to hint if not already known that an interesting app to play with BLE devices is 'nRF Master Control Panel' .You can scan for all devices, list all characteristics and send/read data.

Hope this helps a little bit to get a step forward in this field.

Greetings

mkh42
 

Attachments

  • BleExtEx.zip
    13.7 KB · Views: 439
  • blueSenddata.zip
    41.7 KB · Views: 415
Last edited:
Top