Ho preparato un servizio che gira e preleva dati da una device Bluetooth. ma ho un problema a recepire i dati corretti
Questo e il sorgente :
Mentre l'Activity che deve ricevere i dati :
GSR è dimensionata
su
Sub Globals
Dim GSR as String
End Sub
Va in errore
appena tolgo Valore = GSR
sostituendo con Valore = 1.2 niente errore ma chiaramente niente dati
I ToastMessageShow Mi fa vedere i dati prelevati da blutooth
Questo e il sorgente :
B4X:
Sub Process_Globals
Dim BT As BluetoothAdmin
Dim Serial1 As Serial
Dim timBT As Timer
Dim AStreams As AsyncStreams
Dim tr As TextReader
Dim DatiLetti As String
Dim MyDeviceName As String
Dim MacAddress As String
Dim ValoreGSR As String
End Sub
Sub Service_Create
If File.Exists(File.DirRootExternal, "Bluetooth.txt") Then
tr.Initialize2(File.OpenInput(File.DirRootExternal, "Bluetooth.txt"), "Windows-1252")
MacAddress = tr.ReadLine
MyDeviceName = tr.ReadLine
tr.Close
End If
ToastMessageShow("Connessione " & MyDeviceName & " MacAddress "& MacAddress, True)
ToastMessageShow("Trying to connect to " & MyDeviceName, True)
timBT.Initialize("timBT",5000)
timBT.Enabled = False
Try
BT.Initialize("BT")
Serial1.Initialize("Serial1")
Catch
ToastMessageShow("No BlueTooth Device visible...", True)
End Try
End Sub
Sub Service_Start (StartingIntent As Intent)
'Start Bluetooth
Try
If BT.IsEnabled = False Then
BT.Enable
Else
BTConnectToDevice
End If
Catch
ToastMessageShow("No BlueTooth Device Connect ...", True)
End Try
End Sub
Sub BT_StateChanged(NewState As Int,OldState As Int)
If NewState = BT.STATE_ON Then
BTConnectToDevice
Log("BT Connect")
Else
Serial1.Disconnect
timBT.Enabled = False
Log("BT Disconnect")
End If
End Sub
Sub BTConnectToDevice
Try
Serial1.Connect(MacAddress)
Catch
ToastMessageShow("Device not available",True)
End Try
End Sub
Sub Serial1_Connected (Success As Boolean)
If Success = True Then
ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
AStreams.Initialize(Serial1.InputStream,Serial1.OutputStream,"AStream")
timBT.Enabled = True
Else
ToastMessageShow("Connection to " & Serial1.Address & " broken!", True)
timBT.Enabled = False
End If
End Sub
Sub AStream_NewData (Buffer() As Byte)
DatiLetti = (BytesToString( Buffer, 0, Buffer.Length, "UTF8") )
PrendiValori(DatiLetti)
Log("Valore 1 Orginale: " & DatiLetti)
End Sub
Sub PrendiValori( Contenuto As String )
ValoreGSR = Contenuto.Trim
Log("Valore 2: " & ValoreGSR)
End Sub
Sub GetValue(Richiesta As Boolean)
If Richiesta Then
CallSubDelayed2(Training, "GetResult", ValoreGSR)
End If
End Sub
Sub timBT_Tick
'Communicte with the device here
'if the device answers, fine
'if there is no response, communication might be lost
'Stop the timer and you should call BTConnectToDevice again
End Sub
Sub Service_Destroy
End Sub
Mentre l'Activity che deve ricevere i dati :
B4X:
Sub GetResult(PresoQuesto As String)
GSR = PresoQuesto
End Sub
Sub LeggiDatiDaGSR As Float
Dim Valore As Float
CallSubDelayed2(ModuloGSR, "GetValue",True)
ToastMessageShow(GSR,False)
Valore = GSR
Return Valore
End Sub
GSR è dimensionata
su
Sub Globals
Dim GSR as String
End Sub
Va in errore
appena tolgo Valore = GSR
sostituendo con Valore = 1.2 niente errore ma chiaramente niente dati
I ToastMessageShow Mi fa vedere i dati prelevati da blutooth