Bluetooth Low Energy (BLE) library

merlin2049er

Well-Known Member
Licensed User
Longtime User
So, basically put the code here.

B4X:
Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)
Log("CR: " & Success & ": " & Characteristic.Uuid)
If Success Then 
label1.text = Characteristic.GetStringValue(0)
Log("cr: " & Characteristic.GetStringValue(0))
End Sub
Sub ble_Disconnected
Log("Disconnected")
End Sub
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
hmm, still. The characteristics property type are of type [notify]. I can't read them (that's why it's returning false).
 

sdujolo

Member
Licensed User
Longtime User
I hade same problem first time I had notify working later it stop working no change in code. I made a button and call "manager.SetCharacteristicNotification(c1, True) 'Define a Notification to get data via Changed event" if I press button it start to notify but not if a call it in "ble_Connected"
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Android 4.3 adds support for Bluetooth Low Energy. This library allows you to access the new API. It allows you to search for BLE devices, connect to a device and read its services and characteristics.

Writing is currently not supported though it should be simple to add this feature.

From my testings BLE is not yet ready for production :(
It is unreliable. I also tested it with Google example app and the results were the same.

Seems like others have encountered such issues as well:
http://code.google.com/p/android/issues/detail?id=58381
http://stackoverflow.com/questions/17870189/android-4-3-bluetooth-low-energy-unstable


In order to use this library you will need to download platform level 18 from Android SDK and configure the IDE (Tools - Configure Paths) to use it.

Simple code that starts searching when you click on the activity:
B4X:
Sub Process_Globals
   Dim manager As BleManager
   Private cs As List
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     manager.Initialize("ble")
     Log("Supported: " & manager.BleSupported)
   End If
End Sub
Sub Activity_Click
   Log("Searching for devices")
   manager.Scan(10000, Null)
End Sub
Sub ble_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ", " & MacAddress)
   manager.Connect(MacAddress, True)
End Sub

Sub BLE_Connected (Services As Map)
   cs.Initialize
   Dim s As BleService = Services.GetValueAt(0)
'Read the manufacturer characteristic
   Dim c As BleCharacteristic = s.GetCharacteristics.Get("00002a00-0000-1000-8000-00805f9b34fb")
   manager.ReadCharacteristic(c)
End Sub

Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)
   Log("CR: " & Success & ": " & Characteristic.Uuid)
   If Success Then Log("cr: " & Characteristic.GetStringValue(0)) 
End Sub
Sub ble_Disconnected
   Log("Disconnected")
End Sub

Sub ble_DiscoveryFinished
   Log("DiscoveryFinished")
End Sub

Hmm, I ran a little test using two free ble scanning apps. One for the Android, the other for IOS. I enable notifications and checked the characteristics on both. I was able to get more characteristics updated from the Iphone, than with my app and the free ble scanner I used.
 

bioident

Member
Licensed User
Longtime User
Hi,

I can't get characteristic from BLE device from original Erel code.

I get only this:

Searching for devices
null, D6:FC:13:0F:37:8D
Discovering services.

Please help what is wrong.

Telephone: Samsung S4 and Android 4.4.2

This is FULL LOG:

LogCat connected to: B4A-Bridge: samsung SM-G900F
--------- beginning of /dev/log/main
in addTimaSignatureService
Cannot add TimaSignature Service, License check Failed
Added TimaKesytore provider
GFXPNG PNG bitmap created width:72 height:72 bitmap id is 180
GFXPNG PNG bitmap created width:72 height:72 bitmap id is 181
GFXPNG PNG bitmap created width:13 height:41 bitmap id is 182
GFXPNG PNG bitmap created width:18 height:18 bitmap id is 183
GFXPNG PNG bitmap created width:18 height:18 bitmap id is 184
GFXPNG PNG bitmap created width:18 height:18 bitmap id is 185
Parent view is not a TextView
GFXPNG PNG bitmap created width:72 height:72 bitmap id is 186
GFXPNG PNG bitmap created width:72 height:72 bitmap id is 187
GFXPNG PNG bitmap created width:72 height:72 bitmap id is 188
GFXPNG PNG bitmap created width:128 height:128 bitmap id is 189
<qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.24.00.08+13
Build Date: 03/20/14 Thu
Local Branch: 0320_AU200_patches
Remote Branch:
Local Patches:
Reconstruct Branch:
Enabling debug mode 0
** Activity (main) Create, isFirst = true **
GFXPNG PNG bitmap created width:78 height:96 bitmap id is 190
GFXPNG PNG bitmap created width:78 height:96 bitmap id is 191
GFXPNG PNG bitmap created width:78 height:96 bitmap id is 192
GFXPNG PNG bitmap created width:78 height:96 bitmap id is 193
GFXPNG PNG bitmap created width:78 height:96 bitmap id is 194
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 195
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 196
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 197
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 198
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 199
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 200
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 201
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 202
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 203
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 204
** Activity (main) Resume **
GFXPNG PNG bitmap created width:96 height:96 bitmap id is 205
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:ble.RG
sending message to waiting queue (CallSubDelayed - UpdateStatus)
** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Searching for devices
null, D6:FC:13:0F:37:8D
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Searching for devices
null, D6:FC:13:0F:37:8D
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Searching for devices
null, D6:FC:13:0F:37:8D
Searching for devices
null, D6:FC:13:0F:37:8D
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Searching for devices
null, D6:FC:13:0F:37:8D
** Activity (main) Pause, UserClosed = true **
running waiting messages (3)
** Activity (main) Resume **
** Service (service1) Destroy **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:ble.RG
** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Searching for devices
null, CC:9F:4F:E4:2F:45
null, CC:9F:4F:E4:2F:45
DiscoveryFinished
null, DE:0A:8B:E2:9B:4B
null, D6:FC:13:0F:37:8D
null, CC:9F:4F:E4:2F:45
null, CC:9F:4F:E4:2F:45
null, D6:FC:13:0F:37:8D
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
sending message to waiting queue (ble_devicefound)
running waiting messages (13)
null, D6:FC:13:0F:37:8D
null, CC:9F:4F:E4:2F:45
null, D6:FC:13:0F:37:8D
null, CC:9F:4F:E4:2F:45
null, CC:9F:4F:E4:2F:45
null, D6:FC:13:0F:37:8D
null, D6:FC:13:0F:37:8D
null, DE:0A:8B:E2:9B:4B
null, D6:FC:13:0F:37:8D
null, CC:9F:4F:E4:2F:45
null, D6:FC:13:0F:37:8D
null, D6:FC:13:0F:37:8D
null, CC:9F:4F:E4:2F:45
** Activity (main) Resume **
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Installing file.
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:ble.RG
** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Searching for devices
null, D6:FC:13:0F:37:8D
Discovering services.
Searching for devices
null, D6:FC:13:0F:37:8D
Discovering services.
Failed to destroy process 12048
libcore.io.ErrnoException: kill failed: ESRCH (No such process)
at libcore.io.Posix.kill(Native Method)
at libcore.io.ForwardingOs.kill(ForwardingOs.java:86)
at java.lang.ProcessManager$ProcessImpl.destroy(ProcessManager.java:260)
at anywheresoftware.b4a.phone.Phone$LogCat.LogCatStop(Phone.java:773)
at anywheresoftware.b4a.b4abridge.service1._stoplogcat(service1.java:638)
at anywheresoftware.b4a.b4abridge.service1._streams_newdata(service1.java:699)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA$3.run(BA.java:320)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Failed to destroy process 12048
libcore.io.ErrnoException: kill failed: ESRCH (No such process)
at libcore.io.Posix.kill(Native Method)
at libcore.io.ForwardingOs.kill(ForwardingOs.java:86)
at java.lang.ProcessManager$ProcessImpl.destroy(ProcessManager.java:260)
at anywheresoftware.b4a.phone.Phone$LogCat.LogCatStop(Phone.java:773)
at anywheresoftware.b4a.phone.Phone$LogCat.LogCatStart(Phone.java:718)
at anywheresoftware.b4a.b4abridge.service1._startlogcat(service1.java:631)
at anywheresoftware.b4a.b4abridge.service1._streams_newdata(service1.java:695)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA$3.run(BA.java:320)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Searching for devices
null, D6:FC:13:0F:37:8D
Discovering services.
 

bioident

Member
Licensed User
Longtime User
Dear Erel,

If I click once i only have one search and program is like freezing .
DO nothing
No any connect / disconnect.

LOG:

LogCat connected to: B4A-Bridge: samsung SM-G900F
--------- beginning of /dev/log/main

Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:ble.RG
** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Searching for devices
null, D6:FC:13:0F:37:8D
 
Top