B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=7.3
@EndOfDesignText@
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
'------------------------------------------------
Sub Process_Globals
Public manager As BleManager2
Public currentStateText As String = "UNKNOWN"
Public currentState As Int
Public connected As Boolean = False
Public ConnectedName As String
Public TimeConnected As Long
Public TimeDiscon As Long
Public ConnId As String
Public ConnRetry As Int
Public WriteStatus As Int
Public msgLen As Int ' for sendStr
Public msgPtr As Int ' for sendStr
Public msgPkt As String ' for sendStr
Public msgAll As String
Public CR_LF As String = Chr(13) & Chr(10)
Public Name0 As String = "BTTEST" '//Modified by Stefanoxjx
Public Name1 As String = "MyBleVT"
Private MLDP_PRIVATE_SERVICE As String = "00002902-0000-1000-8000-00805f9b34fb" '//Modified by Stefanoxjx
Private MLDP_DATA_PRIVATE_CHAR As String = "00002902-0000-1000-8000-00805f9b34fb" 'TX '//Modified by Stefanoxjx
End Sub
'------------------------------------------------
Sub Service_Create
manager.Initialize("manager")
'event, Sub Manager_StateChanged will be rised
End Sub
'------------------------------------------------
Sub Service_Start (StartingIntent As Intent)
End Sub
'------------------------------------------------
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
'------------------------------------------------
Sub Service_Destroy
End Sub
'------------------------------------------------
Public Sub Scan
Log ("Scanning")
ConnRetry = 0
'manager.Scan(Null)
manager.Scan2(Null,False) 'all devices, no duplicates
End Sub
'------------------------------------------------
public Sub stopScan
Log ("scan stopped")
manager.StopScan
End Sub
'------------------------------------------------
Public Sub ReadData
' Read message on the data channel
manager.ReadData2(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR)
End Sub
'------------------------------------------------
Public Sub SendStr(Data As String) 'CallSub2(BT_Service, "SendStr", "Chat test" & CR) 'identify app to RN4020.
' Public CR As String = Chr(13) & Chr(10)
Log("Sending: " & Data)
msgAll = Data
msgAll = msgAll & CR_LF
msgLen = msgAll.Length
msgPtr=0
writePkt
End Sub
'------------------------------------------------
Sub Manager_WriteComplete (Characteristic As String, Status As Int)
If connected = False Then Return
Log("Writecomplete: " & Characteristic)
Log("Status=" & Status)
writePkt
End Sub
'------------------------------------------------
Sub writePkt
' if the string to send is longer than 20 bytes (BLE limit) this breaks
' it down to multiple "packets" of 20 to complete the message.
If msgLen = 0 Then Return 'meer dan 19 bytes werkt niet
If msgLen > 20 Then
msgPkt = msgAll.SubString2(msgPtr,msgPtr+20)
msgPtr = msgPtr+20
msgLen = msgLen -20
Else
msgPkt = msgAll.SubString(msgPtr)
msgLen = 0
End If
Dim D() As Byte = msgPkt.GetBytes("UTF8")
delay(125) '125 sending multiple back to back packets fails without this delay.
manager.WriteData(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, D)
manager.SetNotify(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, True)
End Sub
'------------------------------------------------
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)'MLDP_PRIVATE_SERVICE
Log("DA")
Log(ServiceId)
Log(Characteristics)
CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
End Sub
'------------------------------------------------
Public Sub Disconnect
Log("Disconnecting")
If connected Then 'Boolean
manager.SetNotify(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, False)
manager.Disconnect
End If
End Sub
'------------------------------------------------
Sub Manager_StateChanged (State As Int) 'will be raised at start
Select State
Case manager.STATE_POWERED_OFF
currentStateText = "POWERED OFF"
Case manager.STATE_POWERED_ON
currentStateText = "POWERED ON"
Log("BT State = Powered On")
Case manager.STATE_UNSUPPORTED
currentStateText = "UNSUPPORTED"
End Select
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)
ConnectedName = Name
ConnId = Id
If ConnectedName = Null Then
Log("no name found")
ConnectedName = "no name"
End If
If ConnId = "DB:38:03:98:58:6D" Then '//Modified by Stefanoxjx
manager.StopScan
CallSub2(Main, "putStatus", "Connecting")
manager.Connect2(ConnId,False)
Log(ConnectedName & " connected")
Return
End If
End Sub
Sub Manager_Disconnected
Log("Disconnected")
connected = False
TimeDiscon = DateTime.Now
Log ("Connect duration = " & (TimeDiscon - TimeConnected) & " miliseconds")
If (TimeDiscon - TimeConnected)< 400 Then ' if < 400 msec do a connect retry
ConnRetry = ConnRetry + 1
CallSub2(Main,"putStatus","Retry " & ConnRetry)
If ConnRetry < 5 Then ' max of 5 retries
Log ("Retry = " & ConnRetry)
manager.Connect2(ConnId,False)
End If
End If
CallSub(Main, "BT_Disconnected")
End Sub
'------------------------------------------------
Sub Manager_Connected (services As List) 'list my services in the log
Log("Manager-Connected-EVENT")
Dim List1 As List
List1.Initialize
List1.AddAll(services)
For i=0 To List1.Size-1
Log(List1.Get(i))
Next
TimeConnected = DateTime.Now
connected = True
CallSub(Main, "BT_Connected")
End Sub
'------------------------------------------------
public Sub delay(ms As Int) 'delay between packets
Dim temp As Int
Dim futuretime As Long
futuretime = DateTime.Now + ms
Do While DateTime.Now < futuretime
temp=temp+1 'dummy stuff, not used
Loop
End Sub
'------------------------------------------------