Android Question BLE Manufacturers Specific Data

carrompl

New Member
Licensed User
Hi.

I'm trying to create small app (timer) that corresponds to my BLE beacon. I would like to read the Manufacturers Data (i put it in the Device Found event). Please check my code as it is not finding any BLE device. Please explain me or give a link to BLE2 library tutorial that i could follow. Are there any total beginners tutorials?

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

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private timer As Int
    Private bacondata As Int
    Dim screensaver As PhoneWakeState
    Private Timer1 As Timer
    Public manager As BleManager2
   
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.

    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("ctimerLayout1")
    timer = 15
    Button1.Text = timer
    Log (timer)
    Button1.Color = Colors.Green
    Timer1.Initialize("Timer1", 1000)
    Timer1.Enabled = False
    manager.Initialize ("Manager")
    manager.Scan2(Null, True)

End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Dim key As Int = -1 'type is important. Must be Int.

    Dim cnt As Int

    If Id="D0:F0:18:43:DE:29" Then
        If AdvertisingData.ContainsKey(key) Then
            Dim b() As Byte = AdvertisingData.Get(key)
            If b.Length > 8 Then
   
                If b(9)<0 Then
                    cnt=256-b(9)
                Else
                    cnt=b(9)
                End If
                cnt=Bit.ShiftRight(cnt,2)
         
            End If
        End If

    End If
   
bacondata=cnt
Log ("k"&bacondata)
End Sub




Sub Activity_resume
    screensaver.KeepAlive(True)
End Sub

Sub Timer1_Tick
    timer = timer - 1
    Button1.Text = timer
    Log(timer)
    If timer<1 Then
        Button1.Color=Colors.Red
    Else
        Button1.Color= Colors.Green
    End If
End Sub

Sub Button1_Click
    Timer1.Enabled = False
    Button1.Color = Colors.Green
    timer = 15
    Button1.Text = timer
    Timer1.Enabled = True
   
End Sub
Sub pause
    Button1.Text = timer
    Button1.Color = Colors.Blue
    Timer1.Enabled = False
End Sub

Sub Button1_LongClick
    pause
End Sub

thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with the BLE example: https://www.b4x.com/android/forum/threads/ble-2-bluetooth-low-energy.59937/

Does it find your device?

You should follow that example. Several issues that I quickly see in your code:
1. BLE feature should be implemented in a service.
2.
B4X:
                If b(9)<0 Then
                    cnt=256-b(9)
                Else
                    cnt=b(9)
                End If
                cnt=Bit.ShiftRight(cnt,2)
Equivalent to:
B4X:
cnt = Bit.ShiftRight(Bit.And(0xff, b(9)), 2)

3. Class to parse beacons: https://www.b4x.com/android/forum/threads/61127

4. Assuming that targetSdkVersion is 26 then you need to request the location permission. See the BLE example.
 
Upvote 0
Top