Bluetooth Low Energy (BLE) library

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use the BLE2 library instead: https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/


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
 

Attachments

  • BLE.zip
    9.2 KB · Views: 1,404
Last edited:

bluedude

Well-Known Member
Licensed User
Longtime User
Erel,

Does it also have methods to read data from the BLE device? Or is it just discovery and capabilities?
 

bluedude

Well-Known Member
Licensed User
Longtime User
I run this on the galaxy Nexus Android 4.3 and compile with API 18 but it says "supported false" in code. I use the TI SensorTag.

So I guess the BLE implementation sucks?
 

bluedude

Well-Known Member
Licensed User
Longtime User
On the Nexus 7 (Android 4.3) same problem, I found you need to root it to enable BLE.
 

jobykpaul

Member
Licensed User
Longtime User
Hi,
I use galaxy s4 . I want to create an application to communicate to a BLE module (Blugiga BLE112) via serial . Please let me know how we do this. I hope that Erel can answer me.

Thank you

Joby
 

bluedude

Well-Known Member
Licensed User
Longtime User
I have tried running the code on Nexus 7 (old model) but no luck. It seems both Galaxy Nexus and Nexus 7 don't really support BLE.
 

bluedude

Well-Known Member
Licensed User
Longtime User
sdujolo,

I run 4.3 but I don't want to root it :) Can it run on 4.3 without rooting?
 

Antti Mauranen

Member
Licensed User
Longtime User
You say: "Writing is currently not supported though it should be simple to add this feature."

Do you have plans to add writing. Reading seems to function ok.
 

Antti Mauranen

Member
Licensed User
Longtime User
I use Nexus 7 (2012) and Bluegiga BLE112 module (TI CC2540 based). I just started to experiment Android/BLE using your example program and reading works ok. Connecting and scanning are having some problems, I have to switch power off from the module from time to time.

I am also experimenting with Android SDK example program and it does not have those problems with connecting and scanning. I have problems with writing with SDK example (added writing to it).

I will continue with test both with B4A and SDK example. Android/BLE is pretty important for me.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Antti, any guide on how to add these to a stock ROM? Where do I need to copy the directories to? Inside the stock ROM zip?

Which tools do you use to install a new ROM? Never done this before :)
 
Top