B4A Library BleExtended

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

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

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
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?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…