Bis jetzt scheint es zu laufen, hier der versprochene Code.
Vielleicht weis ja noch einer wie der Code besser zu gestalten ist.
Vielleicht weis ja noch einer wie der Code besser zu gestalten ist.
B4X:
#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
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Create
manager.Initialize("manager")
End Sub
Public Sub Scan
manager.Scan(Null) 'alle Geräte scannen
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(1000)
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(1000)
Dim valoriByte(1) As Byte 'Period
valoriByte(0)=0xFF
manager.WriteData("f000aa20-0451-4000-b000-000000000000","f000aa23-0451-4000-b000-000000000000",valoriByte)
Log("Set WriteData2")
Sleep(1000)
'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 = (ArrayBytes(1)*256) + ArrayBytes(0)
hum = (ArrayBytes(3)*256) + ArrayBytes(2)
hum = Bit.And(0x0000ffff, hum)
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))
If hum1 > 100 Then hum1 = 100
BlueATemperatur = NumberFormat(temp1, 1, 2) 'Übergabedaten
BlueALuftfeuchtigkeit = NumberFormat(hum1, 1, 2) 'Übergabedaten
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