B4R Library rESP32SimpleBLE - ESP32 BLE

Erel

Administrator
Staff member
Licensed User
This library makes it very simple to advertise data through BLE. You just need to call ble.Advertise with the string that you want to advertise as the device name.

Note that it doesn't support making connections.


Usage example:

B4X:
#StackBufferSize: 600
Sub Process_Globals
   Public Serial1 As Serial
   Private ble As ESP32SimpleBLE
   Private timer1 As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   timer1.Initialize("timer1_Tick", 500)
   timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
   Dim data As String = JoinStrings(Array As String("B4RTime: ", NumberFormat(Millis, 0, 0)))
   If ble.Advertise(data) = False Then
       Log("failed to set name.")
   End If
End Sub
iOS app:


The clients need to scan for near by devices and check the device name.
B4i and B4A clients examples are attached.

On Android the name is cached so we need to get it from the advertising data map.
On iOS it stops scanning after a while so we restart the scanning process every 10 seconds.
 

Attachments

MathiasM

Member
Licensed User
I'm a bit confused by the 'name' vs 'data' in this library and your explanation.

On Android the name is cached so we need to get it from the advertising data map.
But is the data the same as the name here? This seems to imply this:

If ble.Advertise(data) = False Then
Log("failed to set name.")
End If
Altough in the B4A code I see this:

'The name is cached by the OS so we need to get it from the advertising data.
If AdvertisingData.ContainsKey(9) Then
Dim RawName() As Byte = AdvertisingData.Get(9)
Dim n As String = BytesToString(RawName, 0, RawName.Length, "utf8")
If n.StartsWith("B4R") Then
CallSub2(Main, "NewData", n)
End If
End If
So the name is never actually used here? It's just checking if the data starts with a B4R?

Does a 'name' actually exist in this advertise context? Isn't everything data?
Sorry for my questions, I'm just trying to understand.

My ESP32 is not yet in my possesion, so I cannot check out. I see you can send a string, but would it be possible to set bytes and convert them to a string. I'm willing to implement the Eddystone protocol with this code: https://github.com/google/eddystone/tree/master/eddystone-uid

Thanks for your clarification.
 

Erel

Administrator
Staff member
Licensed User
The B4R code sets the advertised device name.

The B4A event is:
B4X:
DeviceFound (Name As String, DeviceId As String, AdvertisingData As Map, RSSI As Double)
In most cases you will use the Name parameter to get the advertised device name. However as Android caches the value we cannot use it in this case and instead we read it from the advertising map directly. If you try it you will see that the Name parameter is not being updated. It will keep the first value that was sent.
 
Top