#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
''' 'Private Service For Microchip MLDP
''' Private MLDP_PRIVATE_SERVICE As String = "00035b03-58e6-07dd-021a-08123a000300"
''' 'Characteristic For MLDP Data, properties - notify, write
''' Private MLDP_DATA_PRIVATE_CHAR As String = "00035b03-58e6-07dd-021a-08123a000301"
Private MLDP_PRIVATE_SERVICE As String = "49535343-fE7D-4AE5-8FA9-9FAFD205E455"
Private MLDP_DATA_PRIVATE_CHAR As String = "49535343-1E4D-4BD9-BA61-23C647249616"
End Sub
Sub Service_Create
manager.Initialize("manager")
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,True)
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)
' Log("Sending: " & Data)
msgAll = Data
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
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) ' sending multiple back to back packets fails without this delay.
'Sleep(25) ' using sleep as a delay does not work in service module.
manager.WriteData(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, D)
delay(125)
manager.SetNotify(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, True)
End Sub
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
End Sub
Public Sub Disconnect
Log("Disconnecting")
If connected Then
manager.SetNotify(MLDP_PRIVATE_SERVICE, MLDP_DATA_PRIVATE_CHAR, False)
manager.Disconnect
End If
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"
' 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)
'''If Name.Contains("ZWGDO-01") Then
If Name="BLE-0000" Then
TimeConnected = DateTime.Now
ConnectedName = Name
ConnId = Id
manager.StopScan
'''manager.Connect(Id)
manager.Connect2(Id,False)
'manager.Connect2(Id,True)
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
If ConnRetry < 5 Then ' max of 5 retries
Log ("Retry = " & ConnRetry)
'''manager.Connect(ConnId)
manager.Connect2(ConnId,False)
End If
End If
CallSub(Main, "StateChanged")
End Sub
Sub Manager_Connected (services As List)
Log("Connected")
TimeConnected = DateTime.Now
connected = True
CallSub(Main, "StateChanged")
End Sub
public Sub delay(ms As Int)
Dim temp As Int
Dim futuretime As Long
futuretime = DateTime.Now + ms
Do While DateTime.Now < futuretime
temp=1
Loop
End Sub