#Region Project Attributes
#ApplicationLabel: BleTest
#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
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Dim rp As RuntimePermissions
Private ble As BleManager2
Private rp As RuntimePermissions
Private SERVICE_UUID As String = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
Private WRITE_UUID As String = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
Private NOTIFY_UUID As String = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Private btnSend As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
If FirstTime Then
ble.Initialize("ble")
End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
'xui.MsgboxAsync("Hello world!", "B4X")
StartScan
End Sub
Sub btnSend_Click
ble.WriteData(SERVICE_UUID, WRITE_UUID, "A".GetBytes("UTF8"))
Log("TX: A")
End Sub
Sub StartScan
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
ble.Scan(Array As String())
End If
End Sub
Sub ble_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Log("Trovato: " & Name)
If Name = "Remote" Then
ble.StopScan
ble.Connect(Id)
End If
End Sub
Sub ble_ServicesDiscovered (Services As List)
Log("Services OK")
Sleep(300)
ble.SetNotify(SERVICE_UUID, NOTIFY_UUID, True)
Log("Notify attivato")
End Sub
Sub ble_Connected (Services As List)
Log("Connesso!")
CallSubDelayed(Me, "EnableNotify")
End Sub
Sub ble_CharacteristicChanged (ServiceId As String, CharacteristicId As String, Value() As Byte)
Log("RAW RX: " & BytesToString(Value, 0, Value.Length, "UTF8"))
End Sub
Sub EnableNotify
Log("Attivo notify...")
Sleep(500)
'ble.SetIndication(SERVICE_UUID, NOTIFY_UUID, True)
ble.SetNotify(SERVICE_UUID, NOTIFY_UUID, True)
End Sub