Sub Class_Globals
Private manager As BleManager2
Private bc As ByteConverter
Private sUUID As String = "2320ae58-8394-4652-95f7-0a872ac0958f" ' Scan Service UUID
Private nUUID As String = "89A8591D-BB19-485B-9F59-58492BC33E24" ' Notify UUID
Private bUUID As String = "894C8042-E841-461C-A5C9-5A73D25DB08E" ' Button characteristic
Private firstRead As Boolean
Public currentStateText As String = "UNKNOWN"
Public currentState As Int
Public connected As Boolean = False
Public ConnectedName As String
Private ConnectedServices As List
Public rp As RuntimePermissions
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
manager.Initialize("Manager")
End Sub
Private Sub LWS(id As String) As String 'To lower string (B4i upper case)
Return id.ToLowerCase
End Sub
Public Sub ReadData
For Each s As String In ConnectedServices
manager.ReadData(s)
Next
End Sub
Public Sub Disconnect
manager.Disconnect
Manager_Disconnected
End Sub
Sub Manager_StateChanged (State As Int)
Select State
Case manager.STATE_POWERED_OFF
currentStateText = "POWERED OFF"
Case manager.STATE_POWERED_ON
currentStateText = "POWERED ON"
Case manager.STATE_UNSUPPORTED
currentStateText = "UNSUPPORTED"
End Select
currentState = State
CallSub(Main, "StateChanged")
End Sub
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
ConnectedName = Name
manager.StopScan
manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
End Sub
Public Sub StartScan
If manager.State <> manager.STATE_POWERED_ON Then
Log("Not powered on.")
Else If rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
Log("No location permission.")
Else
Dim ScanFilter As List
ScanFilter.Initialize
'
ScanFilter.Add(LWS(sUUID)) ' Find Device
manager.Scan(ScanFilter) ' Find Device with this sUUID
End If
End Sub
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
If firstRead Then
firstRead = False
manager.SetNotify(LWS(nUUID), LWS(bUUID), True)
Return
End If
Dim b() As Byte = Characteristics.Get(LWS(bUUID))
Log(b(0))
If (b(0)) = 1 Then
Log("Button pressed")
End If
If (b(0)) = 0 Then
Log("Button released")
End If
End Sub
Sub Manager_Disconnected
Log("Disconnected")
connected = False
CallSub(Main, "StateChanged")
End Sub
Sub Manager_Connected (services As List)
Log("Connected")
connected = True
ConnectedServices = services
firstRead = True
manager.ReadData(LWS(sUUID))
End Sub