Android Question Async in a service not reading in all BT data

Graham

Member
Licensed User
Longtime User
I have a service running BT comm and reading in data. I seem to not be getting all the data read in at once.

Log (Start & " " & Stop) reports varying numbers.

Here is one complete string of data outputted from my PCB with the other BT chip. I logged this with other software.

R,0000,1003,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,$GPGGA,202552.39,0201.237001,N,00338.715607,W,2,04,3.00,0.00,M,-20.71,M,0.0,0000*4B,E

Here is some data reported by the two Log lines of code
-1 -1

R,0000,1003,0000,0000,0000,0000,0000

0 -1

,0000,0000,0000,0000,0000

-1 -1

,0000,0000,0000,0000,0000,0000,0000,

-1 -1

0000,0000,0000,0000,0000,000

-1 -1

0,0000,$GPGGA,204722.79,0201.248578,N,00338.737854,W,2,04,3.00,0

-1 -1

.00,M,-20.71,M,0.0,0000*47,E

At the bottom of the code is the AStreams_NewData routine

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
   Dim BT As BluetoothAdmin
   Dim Serial1 As Serial
   Dim btConnected As Boolean 
   Dim AStreams As AsyncStreams
   Dim MyDeviceName As String  
   Dim Notification1 As Notification
   Dim ServiceMessage As String
   Dim Awake As PhoneWakeState
End Sub
Sub Service_Create
   
   MyDeviceName = "INTERFACE"  'Put the name of your coupled device here!
   ToastMessageShow("Trying to connect to " & MyDeviceName, True)
  
   Try
      BT.Initialize("BT")
      Serial1.Initialize("Serial1")
   Catch
      ToastMessageShow("No BlueTooth Device visible...", True)
   End Try
      
          'Start Bluetooth
      Try
         If BT.IsEnabled = False Then
            BT.Enable
         Else
            'connect to device
            BTConnectToDevice   
         End If
      Catch
      End Try

    Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
End Sub

Sub Service_Start (StartingIntent As Intent)
    ServiceMessage="GCBasic"
    Notification1.SetInfo("GCBasic", "Map Service " & ServiceMessage, Main)
    Notification1.Sound = False
    'Make sure that the process is not killed during the download
    'This is important if the download is expected to be long.
    'This will also show the status bar notification
    Service.StartForeground(1, Notification1)
    Log("service running")
End Sub

Sub Service_Destroy

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
   Dim PairedDevices As Map
  
   PairedDevices = Serial1.GetPairedDevices
   Try
      Serial1.Connect3(PairedDevices.Get(MyDeviceName),1)
   Catch
      ToastMessageShow("Device not available",True)
   End Try
End Sub

Sub Serial1_Connected (Success As Boolean)
   Dim msg As String
  
   If Success = True Then
      ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
      AStreams.Initialize(Serial1.InputStream,Serial1.OutputStream,"AStreams")
      'timBT.Enabled = True
      Log("BT Connected")

   Else   'disconnected
      ToastMessageShow("Connection to " & Serial1.Address & _
                  " broken!", True)
      btConnected = False
   End If
End Sub

Sub AStreams_NewData (Buffer() As Byte)

'If Main.ScrID>3 Then  '3
'Msgbox("NewData","GC")
  Dim Incoming As String
  Dim Start As Int
  Dim Stop As Int                                    '0 Buffer.Length
  Incoming= BytesToString(Buffer, 0,Buffer.Length , "ISO8859_1")
  Log (Incoming)
  Start = Incoming.IndexOf("R")
  Stop = Incoming.IndexOf("E")
  Log (Start & " " & Stop) 
'End If 
End Sub
 

Graham

Member
Licensed User
Longtime User
When I go to the APp when I have all code within the Activity I get the log results for Astream_newdata as


R,0000,1003,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,$GPGGA,215520.44,0201.249271,N,00338.801903,W,2,04,3.00,0.00,M,-20.71,M,0.0,0000*4F,E

This is correct


Then when I go back to the APP with the BT and Serial in the service the log reports

����R,0000,1003,0000,0000,0000,0000,00

00,0000,0000,0000,0000,0000,

0000,0000,0000,0000,0000,0000,0000,000

0,0000,0000,0000,0000,000

0,0000,$GPGGA,215718.04,0201.106639,N,00338.801903,W,2

,04,3.00,0.0

0,M,-20.71,M,0.0,0000*42,E

The only thing I have changed from the code I posted in the initial post to now is not Logging on a separate line of code the
Start = Incoming.IndexOf("R")
Stop = Incoming.IndexOf("E")
Log (Start & "" & Stop)

I commented the Log (Start & "" & Stop) and I get different results for the Log(Incoming). I also get ���� which I bet is the Prefix data.
 
Upvote 0

Graham

Member
Licensed User
Longtime User
I found the error, AStream.InitializePrefix(serial1.InputStream, False, serial1.OutputStream, "AStream")
I forgot the InitializePrefix part!!!
 
Upvote 0
Top