Italian Gestire un evento di un servizio [SOLVED]

Star-Dust

Expert
Licensed User
Non sarebbe più corretto cosi?

La call dopo aver assegnato GSR

B4X:
Sub AStream_NewData (Buffer() As Byte)

    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1

        Dim c As Char = s.CharAt(i)
        If i = 0 And c = Chr(10) Then '\n...And
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            'IptMain.GSR=s.SubString2(start, i)
            'CallSub2(Training,"ValoreGSR",IptMain.GS)
            start = i + 1
        Else If c = Chr(13) Then '\r
            '          
            If start =0 And i=7 Then
                IptMain.GSR=s.SubString2(start, i)
                Log("Conduttanza " & IptMain.GSR)
                CallSub2(Training,"GSROk",IptMain.GSR)
            End If
            If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next

    If start > 0 Then sb.Remove(0, start)
 
End Sub

Però io avrei usato Timer e non la SUB ATTENDI che hai realizzato tu...
 

Star-Dust

Expert
Licensed User
certo ma callsub2 è Rem

Io mi riferivo alla seconda.
B4X:
If start =0 And i=7 Then
    IptMain.GSR=s.SubString2(start, i)
    Log("Conduttanza " & IptMain.GSR)
    CallSub2(Training,"GSROk",IptMain.GSR)
End If
Ho corretto anche quella in REM nel caso tu la attivassi
 

Angelo Messina

Active Member
Licensed User
Conduttanza 0.78754
Conduttanza 0.78610
Conduttanza 0.78251
Conduttanza 0.77964
Conduttanza 0.77892
Conduttanza 0.78108
Conduttanza 0.77677
Conduttanza 0.77390
Conduttanza 0.77390
Conduttanza 0.77318
Conduttanza 0.77318
Conduttanza 0.77605
Conduttanza 0.77749
Conduttanza 0.77390

Conduttanza 0.77318
** Activity (training) Resume **
Lettura 5 0.83871
Lettura 7 0.83871
Lettura 12 0.83871
Lettura 15 0.83871
0.83871/0.83871/0.83871/0.83871
Conduttanza 0.77103
Conduttanza 0.76171
Conduttanza 0.74882
Conduttanza 0.76816
 

Angelo Messina

Active Member
Licensed User
Conduttanza 0.78754
Conduttanza 0.78610
Conduttanza 0.78251
Conduttanza 0.77964
Conduttanza 0.77892
Conduttanza 0.78108
Conduttanza 0.77677
Conduttanza 0.77390
Conduttanza 0.77390
Conduttanza 0.77318
Conduttanza 0.77318
Conduttanza 0.77605
Conduttanza 0.77749
Conduttanza 0.77390

Conduttanza 0.77318
** Activity (training) Resume **
Lettura 5 0.83871
Lettura 7 0.83871
Lettura 12 0.83871
Lettura 15 0.83871
0.83871/0.83871/0.83871/0.83871
Conduttanza 0.77103
Conduttanza 0.76171
Conduttanza 0.74882
Conduttanza 0.76816
 

Star-Dust

Expert
Licensed User
il messaggio Lettura 5 0.83871 da quale sub viene prodotta?
 

Angelo Messina

Active Member
Licensed User
stessa cosa con
CallSub2(Training,"GSROk",IptMain.GSR)


Sub GSROk(Valore As String)
GSR=Valore
End Sub

Lettura15=GSR
 

Angelo Messina

Active Member
Licensed User
Sub img_Click

....

End Sub


del modulo training

dove è contanuta anche la

Sub GSROk(Valore As String)
GST=Valore
End Sub
 

Star-Dust

Expert
Licensed User
Ok e se invece di salvare su IptMain.GSR salvi con file Write?

B4X:
Sub AStream_NewData (Buffer() As Byte)

    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1

        Dim c As Char = s.CharAt(i)
        If i = 0 And c = Chr(10) Then '\n...And
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            'IptMain.GSR=s.SubString2(start, i)
            'CallSub2(Training,"ValoreGSR",IptMain.GS)
            start = i + 1
        Else If c = Chr(13) Then '\r
            '
            If start =0 And i=7 Then
                File.WriteString(File.DirInternalCache,"lettura.txt", s.SubString2(start, i))
                Log("Conduttanza " & s.SubString2(start, i))
                CallSub2(Training,"GSROk",s.SubString2(start, i))
            End If
            If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next

    If start > 0 Then sb.Remove(0, start)

End Sub

Dall'altra parte

B4X:
Sub Globals
Dim Lettura5 As String
Dim Lettura7 As String
Dim Lettura10 As String
Dim Lettura12 As String
Dim Lettura15 As String

End Sub

Lettura5=File.ReadString(File.DirInternalCache,"lettura.txt")
Log("Lettura 5 " & Lettura5)

Attendi(5)
 

Angelo Messina

Active Member
Licensed User
File.WriteString(File.DirInternalCache,"AA", s.SubString2(start, i))




Dim BB As String
Lettura5=File.ReadString(File.DirInternalCache,BB)

cosi !
 

Star-Dust

Expert
Licensed User
File.WriteString(File.DirInternalCache,"AA", s.SubString2(start, i))




Dim BB As String
Lettura5=File.ReadString(File.DirInternalCache,BB)

cosi !

vedi il mio esempio il nome del file, su read e su write dev'essere uguale ovviamente.

"AA" e BB non mi danno l'idea che ha lo stesso nome
 

Star-Dust

Expert
Licensed User
Per il timer farei cosi

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Tim As Timer
    Dim Secondi As Int = 5
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Tim.Initialize("Tim",Secondi  * DateTime.TicksPerSecond)
    Tim.Enabled=False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Tim_Tick
    Log("Lettura: " & File.ReadString(File.DirInternalCache,"lettura.txt"))
    Select Secondi
        Case 5
            Secondi = 7
            Tim.Interval=Secondi * DateTime.TicksPerSecond
        Case 7
            Secondi = 10
            Tim.Interval=Secondi * DateTime.TicksPerSecond
        Case 10
            Secondi = 12
            Tim.Interval=Secondi * DateTime.TicksPerSecond
        Case 12
            Secondi = 15
            Tim.Interval=Secondi * DateTime.TicksPerSecond
        Case 15
            Tim.Enabled=False
    End Select
End Sub

e poi nel servizio

B4X:
Sub Process_Globals
   
    Dim BT As BluetoothAdmin
    Dim Serial1 As Serial
    Dim timerBT As Timer
    Dim timerLettura As Timer
    Dim AStreams As AsyncStreams
    Dim tr As TextReader
    Dim MyDeviceName As String
    Dim MacAddress As String
    Private sb As StringBuilder
    Public charset As String = "UTF8"
       
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)
    timerBT.Initialize("timerBT",5000)
    timerBT.Enabled = False
   
    Try
        sb.Initialize
        BT.Initialize("BT")
        Serial1.Initialize("Serial1")
    Catch
        ToastMessageShow("No BlueTooth Device visible...", False)
    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 ...", False)
    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
        timerBT.Enabled = False
        Log("BT Disconnect")
    End If
End Sub

Sub BTConnectToDevice
    Try
        'Serial1.Connect(MacAddress)
        Serial1.Connect3(MacAddress,1)
    Catch
        ToastMessageShow("Device not available",False)
    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")
        timerBT.Enabled = True
    Else
        ToastMessageShow("Connection to " & Serial1.Address & " broken!", False)
        timerBT.Enabled = False
        'Service_Destroy
    End If

End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim Valore As String
   
    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1

        Dim c As Char = s.CharAt(i)
        If i = 0 And c = Chr(10) Then '\n...And
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            'CallSub2(Training,"ValoreGSR",s.SubString2(start, i))
            'GSR=s.SubString2(start, i)
            start = i + 1
        Else If c = Chr(13) Then '\r
            '            CallSub2(Training,"GSROk",s.SubString2(start, i))
            If start =0 And i=7 Then
                Valore=s.SubString2(start, i)
                File.WriteString(File.DirInternalCache,"lettura.txt",Valore)
                Log("Conduttanza " & Valore)
            End If
            If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next

    If start > 0 Then sb.Remove(0, start)
   
End Sub


Sub timerBT_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
    StopService(Me)
End Sub
 

Star-Dust

Expert
Licensed User
Funziona?
 
Top