Bluetooth Low Energy (BLE) library

merlin2049er

Well-Known Member
Licensed User
Longtime User
hmm, I'm looking at the services characteristic using an APP called BLE Scanner.

It seems that the services characteristic that they want me to read are all of the property [notify].

I can read values from services that are read/write.
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Ok, I know which services I want, but they are notification types. I enabled them with the ble scanner and it basically updates the service with a value every 5 seconds. Is there a method to enable these notification types?
 

KY Leng

Active Member
Licensed User
Longtime User
Hi Erel, could we send/read data via asynchronous stream as bluetooth class 2. if possible could you please put a short working example ? and what is the Bluetooth characteristic ? thank you.
 

Denny Hopp

Member
Licensed User
Longtime User
Hi Erel -- I used the example that you posted over a year ago to connect to a Microchip RN4020 module and everything works fine. I also set a timer to periodically read the characteristic. I just want to make sure I am using the most current BLE library (the one I am using is from January 2014). Also, is there any way to perform a write to the module?
 

Denny Hopp

Member
Licensed User
Longtime User
Hi Merlin. Here is the code that I used. I just added a timer tick to periodically read a characteristic. I am trying to figure out where the 128-bit characteristic came from and am also trying to figure out how to write to the module.

#Region Project Attributes
#ApplicationLabel: RN_4020 Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Dim manager As BleManager
Private cs As List
Dim dataTimer As Timer
Dim s As BleService
Dim c As BleCharacteristic
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)
dataTimer.Initialize("dataTimer", 500)
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
s = Services.GetValueAt(0)
'Read the manufacturer characteristic
c = s.GetCharacteristics.Get("00002a00-0000-1000-8000-00805f9b34fb")
manager.ReadCharacteristic(c)
dataTimer.Enabled = True
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


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

Denny Hopp

Member
Licensed User
Longtime User
The only thing I need to know is if there is a way to write to the BLE module. Anyone have any ideas?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Hi Merlin. Here is the code that I used. I just added a timer tick to periodically read a characteristic. I am trying to figure out where the 128-bit characteristic came from and am also trying to figure out how to write to the module.

#Region Project Attributes
#ApplicationLabel: RN_4020 Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Dim manager As BleManager
Private cs As List
Dim dataTimer As Timer
Dim s As BleService
Dim c As BleCharacteristic
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)
dataTimer.Initialize("dataTimer", 500)
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
s = Services.GetValueAt(0)
'Read the manufacturer characteristic
c = s.GetCharacteristics.Get("00002a00-0000-1000-8000-00805f9b34fb")
manager.ReadCharacteristic(c)
dataTimer.Enabled = True
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


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


Hmm, I need to read 5 characteristics. Do I use one event (BLE_connected) to read them all? Or put each in it's own event?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
ok, I'm reworking this to read characteristics using a timer. They all show up as being false. The characteristics are of type notify, does that matter?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
B4X:
Sub dataTimer_Tick
    ' cs.Initialize
    ' s = Services.GetValueAt(3)
    'Read the manufacturer characteristic
   
       
    c = s.GetCharacteristics.Get("0000fff2-0000-1000-8000-00805f9b34fb")   '  percent changed
    Label1.Text = manager.ReadCharacteristic(c)
      Log( manager.ReadCharacteristic(c))
 
      c = s.GetCharacteristics.Get("0000fff4-0000-1000-8000-00805f9b34fb")           'DSC
     Label2.Text = manager.ReadCharacteristic(c)
       Log(manager.ReadCharacteristic(c))

       c = s.GetCharacteristics.Get("0000fff8-0000-1000-8000-00805f9b34fb")  '  Alarm
     Label5.Text= manager.ReadCharacteristic(c)

       Log(manager.ReadCharacteristic(c))
   
    c = s.GetCharacteristics.Get("0000fff9-0000-1000-8000-00805f9b34fb" )  '  Volts
     Label3.Text=  manager.ReadCharacteristic(c)
  
       Log(manager.ReadCharacteristic(c))
   
    c = s.GetCharacteristics.Get("0000fffc-0000-1000-8000-00805f9b34fb" )   '  AMPS
     Label4.Text=  manager.ReadCharacteristic(c)

      Log(manager.ReadCharacteristic(c))
   
   

End Sub

These are all returning false.
 
Top