#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
Private ConnectedServices As List
Dim BlueATemperatur,BlueALuftfeuchtigkeit As Double = 0
Dim Twert,Fwert As Float
Dim Zaehler As Int
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Create
manager.Initialize("manager")
End Sub
Public Sub Scan
If manager.State <> manager.STATE_POWERED_ON Then
Log("Not powered on")
Else
manager.Scan(Null) 'alle Geräte scannen
End If
End Sub
Public Sub ReadData
For Each s As String In ConnectedServices
manager.ReadData(s)
Next
End Sub
Public Sub Disconnect
manager.Disconnect
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
End Sub
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Log("Name: " & Name)
Log("Mac Adresse: " & Id)
Log("RSSI: " & RSSI)
Log("Zusatzdaten: " & AdvertisingData)
ConnectedName = Name
manager.StopScan
manager.Connect(Id)
End Sub
Public Sub writeData1
'Temperatur und Feuchtresensor
manager.SetNotify("f000aa20-0451-4000-b000-000000000000","f000aa21-0451-4000-b000-000000000000",True)
Log("Set Notify")
Sleep(2000)
Dim valoriByte(1) As Byte
valoriByte(0)=0x01
manager.WriteData("f000aa20-0451-4000-b000-000000000000","f000aa22-0451-4000-b000-000000000000",valoriByte)
Log("Set WriteData1")
Sleep(2000)
' Dim valoriByte(1) As Byte 'Period
' valoriByte(0)=0x64
' manager.WriteData("f000aa20-0451-4000-b000-000000000000","f000aa23-0451-4000-b000-000000000000",valoriByte)
' Log("Set WriteData2")
' Sleep(2000)
'Period AA23* R/W 2.55 sec (0xFF). Default 1 second (0x64).
End Sub
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
Private tem,hum As Int
Private temp1,temp2,hum1 As Float
Dim ArrayBytes() As Byte = Characteristics.Get("f000aa21-0451-4000-b000-000000000000")
tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff))
hum = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(3), 8), 0x0000ff00), Bit.And(ArrayBytes(2), 0x000000ff))
temp1 = (tem/65536)*165-40 ' = Grad
temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit
hum1 = (hum/65536)*100 ' = Relative Luftfeuchtigkeit
Log("Temperatur-Grad = " & NumberFormat(temp1, 1, 2))
Log("Temperatur-Fahrenheit = " & NumberFormat(temp2, 1, 2))
Log("Relative-Feuchte = " & NumberFormat(hum1, 1, 2))
'Mittelwert aller 10 Ergebnisse.
Zaehler = Zaehler + 1
Twert = Twert + temp1
Fwert = Fwert + hum1
If Zaehler = 10 Then
temp1 = Twert / 10
hum1 = Fwert / 10
If hum1 > 100 Then hum1 = 100
BlueATemperatur = NumberFormat(temp1, 1, 2) 'Übergabedaten
BlueALuftfeuchtigkeit = NumberFormat(hum1, 1, 2) 'Übergabedaten
Zaehler = 0
Twert = 0
Fwert = 0
End If
End Sub
Sub Manager_Connected (services As List)
Log("Connected")
connected = True
ConnectedServices = services
Sleep(2000)
writeData1
End Sub
Sub Manager_Disconnected
Log("Disconnected")
connected = False
End Sub
Sub Manager_RssiAvailable (Success As Boolean, RSSI As Double)
Log(Success)
Log(RSSI)
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub