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

tmav

Member
Licensed User
Longtime User
I found a library that can do this;

Unfortunately I cannot open your zip file, but I managed to make a small patch to the BleManager.java library (thanks to Antti Mauranen for giving the source file of his library) and I compiled it with the Simple Library Compiler.
Now I can get the RSSI during scanning.

B4X:
Sub ble_DeviceFound (Name As String, MacAddress As String, rssi As Int)
   Log(Name & ", " & MacAddress & ", " & rssi)
End Sub

I made the following changes:
B4X:
  "DeviceFound (Name As String, MacAddress As String, rssi As Int)",


  public boolean Scan(final BA ba, long PeriodMs, String[] ServiceUUIDs) {

  scanCallback = new LeScanCallback() {


  @Override

  public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

  devices.put(device.getAddress(), device);

  ba.raiseEventFromDifferentThread(BleManager.this, null, 0, eventName + "_devicefound", false,

  new Object[] {device.getName(), device.getAddress(), rssi});

  }
  };
 

Attachments

  • BleExtEx_rssi_mod.zip
    22 KB · Views: 415

Regis44

Member
Licensed User
Longtime User
Hello,

this thread was really useful for me to understand and experiment how BLE works. I tried for a long time to find a simple read/write example with no success. So, finally, i found and start with CHK's terminal project.

I only keep things needed to simply read and write on a HM-11 device. App just has a button for Tx and a Label for Rx.

I used a HM-11 (like the HM-10 but smaller - firmware version V527) connected to an Arduino Uno with a softwareSerial on pin 4 and 5 (Rx, Tx), a Wiko Lenny 4.4.2, B4A 5.02, Android 19 platform and BleExtEx library.

I hope my simple project can help other people.
 

Attachments

  • BLEReadWrite.zip
    383.3 KB · Views: 636
Last edited:

fbritop

Active Member
Licensed User
Longtime User
In b4i I use the following for writting a verification key to the device:

B4X:
Dim data() As Byte
Dim bc As ByteConverter
data=bc.HexToBytes("80BEF5ACFF")
BLE.WriteData("FFFFFFF0-00F7-4000-B000-000000000000", "FFFFFFF5-00F7-4000-B000-000000000000", data)

However in b4a I cannot find the way to send a byte array
B4X:
BLES=Services.get("fffffff0-00f7-4000-b000-000000000000")
BLEC=BLES.GetCharacteristics.get("fffffff5-00f7-4000-b000-000000000000")
BLEC.SetStringValue("80bef5acff")

The device is returning that the key value is invalid, I asume that it is because i cannot read the byte array as I'm sending a string. How could these be achieved as in b4i?

Thanks
 

llestat

New Member
Licensed User
Longtime User
Unfortunately I cannot open your zip file, but I managed to make a small patch to the BleManager.java library (thanks to Antti Mauranen for giving the source file of his library) and I compiled it with the Simple Library Compiler.
Now I can get the RSSI during scanning.

B4X:
Sub ble_DeviceFound (Name As String, MacAddress As String, rssi As Int)
   Log(Name & ", " & MacAddress & ", " & rssi)
End Sub

I made the following changes:
B4X:
  "DeviceFound (Name As String, MacAddress As String, rssi As Int)",


  public boolean Scan(final BA ba, long PeriodMs, String[] ServiceUUIDs) {

  scanCallback = new LeScanCallback() {


  @Override

  public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

  devices.put(device.getAddress(), device);

  ba.raiseEventFromDifferentThread(BleManager.this, null, 0, eventName + "_devicefound", false,

  new Object[] {device.getName(), device.getAddress(), rssi});

  }
  };

Hey Tmav - any possibility of getting the library compiled with scanRecord? Phillips zip appears to be corrupted and the scan record normally holds the advertising data for URI Beacons (Eddystone URL). Any help or direction to is appreciated.
Thank You
 

llestat

New Member
Licensed User
Longtime User
Hey Tmav - any possibility of getting the library compiled with scanRecord? Phillips zip appears to be corrupted and the scan record normally holds the advertising data for URI Beacons (Eddystone URL). Any help or direction to is appreciated.
Thank You

Hey TMAV, pulled out some of my old rusty Java knowledge (last time I used it was 1998) and delved in. Attached is your modified version. Just added the scanRecord property to the code (around line 168, I used compare to diff the files in notepad++).
 

Attachments

  • BLEscanReport.zip
    16.7 KB · Views: 356

fbritop

Active Member
Licensed User
Longtime User
I started having an error which I cannot find out where to start:

The log states:
java.lang.NullPointerException: null receiver

This is fired over the BLE Event:
Sub BLEM_DeviceFound (Name As String, MacAddress As String)

The BLE functions are controled over a service

Any ideas?

Thanks
FBP
 

Dutch2

Member
Licensed User
Longtime User
@mkh42
First of all thank you very much for the BleExtEx library, it has helped my project very much.

I am able to read, write, and Enable Notifications.

The only issue I have is that I need to write to a device that expects an array of 260 bytes.

I can do c.SetStringValue("Test")
or c.SetIntValue(0x1234,c.FORMAT_UINT16,0)
Manager.WriteCharacteristic(c)
and it works correctly.

However, how do I send
Dim Temp(260) As Byte?

Thanks in advance,
Paul.
 

fbritop

Active Member
Licensed User
Longtime User
Try the following sub for writting an Array to the Characteristic

B4X:
Sub WriteBytes(bc As BleCharacteristic, Data() As Byte)
    Dim jo As JavaObject = bc
    jo.RunMethod("setValue", Array As Object(Data))
End Sub
 

walterf25

Expert
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
Hi mkh42, i was wondering if you could help me with this, please see this post here https://www.b4x.com/android/forum/threads/bluetooth-ble-with-blood-glucose-monitor.58137/ thanks for your work with this library, i'm new to Bluetooth BLE as you can guess, i need to retrieve all the blucose readings from this glucose monitor, but i can't figure out how to write the record access control point to the device, please let me know if you have any suggestions, i tried the example you attached to this thread and it works fine i can see the services being logged as well as the characteristics, but the SendData button is disabled, again i will really appreciate your help with this.

Thanks,
Walter
 

Dutch2

Member
Licensed User
Longtime User
Is it possible to use WriteCharacteristic when the characteristic's property is WRITE_NO_RESPONSE?

Does WriteCharacteristic always expect a response?

P.
 

AngeloDavalli

Member
Licensed User
Longtime User
Hello
I'm using TI Sensor Tag and to write characteristics I used BleExtended (V1.00).
I can compile successfully and I can discovery the devices.(I use manager.Scan(100000,Null))
Now I try to move to the new library BleExtEx1 (V.1.10) and still I can compile correctly but when I start the discovering process the app stops and the logs are:

An error occurred:

(Line: 0) null

java.lang.Exception: Sub ble_devicefound signature does not match expected signature.

public static anywheresoftware.b4a.pc.RemoteObject b4a.BleTest.main_subs_0._ble_devicefound(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception


Why ?

thanks
Angelo
 

walterf25

Expert
Licensed User
Longtime User
Try the following sub for writting an Array to the Characteristic

B4X:
Sub WriteBytes(bc As BleCharacteristic, Data() As Byte)
    Dim jo As JavaObject = bc
    jo.RunMethod("setValue", Array As Object(Data))
End Sub
I modified and added the method to be able to write bytes, I have been busy with my new job i haven't had time to post the new version, I will as soon as i get some time.

Walter
 

stari

Active Member
Licensed User
Longtime User
Hello,

this thread was really useful for me to understand and experiment how BLE works. I tried for a long time to find a simple read/write example with no success. So, finally, i found and start with CHK's terminal project.

I only keep things needed to simply read and write on a HM-11 device. App just has a button for Tx and a Label for Rx.

I used a HM-11 (like the HM-10 but smaller - firmware version V527) connected to an Arduino Uno with a softwareSerial on pin 4 and 5 (Rx, Tx), a Wiko Lenny 4.4.2, B4A 5.02, Android 19 platform and BleExtEx library.

I hope my simple project can help other people.

Great.
Work like a charm.
I don't have connected any device on HM-10. When i send "TX Hello World !" i don't get any answer. So i think, i must send my string via RS port of HM-10. Right ?
Regards

http://www.zlatnajedra.com
 

stari

Active Member
Licensed User
Longtime User
Hello,

what do you mean with "RS port of HM-10" ?

Thks for answer. No problem, i have connected Tx and RX with my PIC 16F887 and all thinks working Ok.
As RS port i mean serial connection, as i say.
 

Regis44

Member
Licensed User
Longtime User
That's what I though. Just to be sure before answer.

Cool if it works ! Happy to help you.:)
 

stari

Active Member
Licensed User
Longtime User
Hello,

this thread was really useful for me to understand and experiment how BLE works. I tried for a long time to find a simple read/write example with no success. So, finally, i found and start with CHK's terminal project.

I only keep things needed to simply read and write on a HM-11 device. App just has a button for Tx and a Label for Rx.

I used a HM-11 (like the HM-10 but smaller - firmware version V527) connected to an Arduino Uno with a softwareSerial on pin 4 and 5 (Rx, Tx), a Wiko Lenny 4.4.2, B4A 5.02, Android 19 platform and BleExtEx library.

I hope my simple project can help other people.
Hi,
as i say, all is Ok.
Only i wish to connect to 2 BLE devices at the same time. (HM-10 ).
Is this possible?
 
Top