French Relevé de trames NMEA depuis GPS Bluetooth.

coramrail

New Member
Licensed User
(android 3.2)

Bonjour à tous.
J'essaie de récupérer les trames NMEA d'un GPS Bluetooth mais je n'optiens qu'une seule lecture.
Le GPS est un NavGear StarHiker 65 qui fonctonne à merveille sous Windows (Delphi 10).
J'utile les librairies Serial et RandomAccessFile (AsyncStreams) mais rien y fait, je lis une seule fois les trames !
Comment lire ces trames de façon continu ? J'ai testé les programmes relevé dans " AsyncStreams Tutorial " et " Android Serial tutorial ", puis

1) j'ai du supprimer la ligne < Serial1.Listen 'listen for incoming connections > car j'avais le message suivant :

An error occurred in sub:main_activity_resume (java line: 244)
java.io.IOException: Not able to register SDP record for B4A
Continue?

2) j'ai remplacé la ligne :
AStreams.InitializePrefix(Serial1.InputStream,False,Null,"AStreams") par celle-ci AStreams.Initialize(Serial1.InputStream,Null,"AStreams") car j'avais le message suivant :

Fermeture soudaine de l'application GPS BT (du processus gps.bt). Merci de réessayer.
Forcer la fermeture​

Voici le code de l'essai :
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
    Dim AStreams As AsyncStreams
    Dim Serial1 As Serial
   Dim connected As Boolean
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.
    Dim edtLog As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    Activity.AddMenuItem("Connecter", "mnuConnect")
    Activity.AddMenuItem("Déconnecter", "mnuDisconnect")
   Activity.AddMenuItem("Quitter", "mnuQuitter")
    If FirstTime Then
        Serial1.Initialize("Serial1")
    End If
   edtLog.Initialize("edtLog")
'   edtLog.ForceDoneButton = True
   Activity.AddView(edtLog,10dip,10dip,600dip,500dip)
End Sub

Sub Activity_Resume
    If Serial1.IsEnabled = False Then
        Msgbox("Activez le Bluetooth S.V.P.", "")
    Else
'        Serial1.Listen 'listen for incoming connections
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub mnuConnect_Click
    Dim PairedDevices As Map
    PairedDevices = Serial1.GetPairedDevices
    Dim l As List
    l.Initialize
    For i = 0 To PairedDevices.Size - 1
        l.Add(PairedDevices.GetKeyAt(i))
    Next
    Dim res As Int
    res = InputList(l, "Choisir l'appareil", -1) 'show list with paired devices
    If res <> DialogResponse.CANCEL Then
        Serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
    End If
End Sub

Sub mnuDisconnect_Click
    Serial1.Disconnect
    AStreams.Close
    connected = False
End Sub

Sub Serial1_Connected (Success As Boolean)
    If Success Then
        ToastMessageShow("Connexion réussie.", False)
        connected = True
   '   AStreams.InitializePrefix(Serial1.InputStream,False,Null,"AStreams")
      AStreams.Initialize(Serial1.InputStream,Null,"AStreams")
    Else
        connected = False
        Msgbox(LastException.Message, "Erreur de connexion.")
    End If
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    If connected Then    
        msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        edtLog.text = edtLog.Text & msg
        edtLog.SelectionStart = edtLog.Text.Length
    End If 
End Sub

Sub AStreams_Error
    ToastMessageShow(LastException.Message & CRLF & "'' GPS BT ''" , True)
End Sub

Sub mnuQuitter_Click
   mnuDisconnect_Click
   ExitApplication
End Sub

En espérant trouver une solution à mon problème.
 

priusfan

Member
Licensed User
Bonjour,
j'ai développé un truc pour communiquer en BT avec un interface de diagnostic auto, à mon avis il manque dans ton dev :
Serial1.Listen 'listen for incoming connections
juste derriere
AStreams.Initialize(Serial1.InputStream,Null,"AStreams")
ci-joint mon bout de code qui marche parfaitement dans un service:
B4X:
Sub Serial1_Connected (Success As Boolean)
   If Success Then
'      ToastMessageShow("Connected successfully", False)
      AStreams.Initialize(Serial1.InputStream, Serial1.OutputStream, "AStreams")
      connected = True
'      Lbl_connect.Text="Connected"
'      Lbl_connect.Color=Colors.green
      ' we open the 2 log files
       DataLogger.Initialize(File.OpenOutput(File.DirRootExternal&"/BTHSD" , "fxm_"&DateTime.date(now)&"_"&DateTime.time(now) &".txt", True))
       DataLogs.Initialize(File.OpenOutput(File.DirRootExternal&"/BTHSD"  , "log_"&DateTime.date(now)&"_"&DateTime.time(now) &".txt", True))
      Start_Ticker=DateTime.Now
      Serial1.Listen 'listen for incoming connections
            Timer1.Initialize("Timer1", 1000)    ' 1000 = 1 second
            Timer1.Enabled = True
         DateTime.timeformat="HH:mm:ss.SSS"   ' now this format is ok for logging
      ptr=0
      mode="init"
      send_nextcmd                  
   Else
      connected = False
       ToastMessageShow(LastException.Message, True)

   '   Msgbox(LastException.Message, "Error connecting.")

   End If
CallSub(Main,"ShowConnectSts")

   
End Sub
 
Top