Error using a merge code source from "gps" and "TestTcpIp" examples.

pompierecattivo

Member
Licensed User
Longtime User
Hello all,
first, sorry for my english and my low-level knowing of b4a and java...

I'm triyng to write an app for send a data string to a server over tcp-ip. The string is a build of some data getting from gps. I will clean the code after i could run it completely... ;)

The strange thingh is i could send only the first data string, at the second one i receive an error msg like "java.lang.nullpointerexception" at line AStreams.Write (Buffer)


The code where tha app stop is the follow, i dont paste more code because i just copy and paste codes from "Gps" and "testtcp" as i wrote before...

Sub SendData(msg As String)
Dim Buffer() As Byte

Buffer = msg.GetBytes( "UTF8")
AStreams.Write (Buffer)
AStreams.Write(Array As Byte(254))
End Sub

Any suggest is appreciate, really sorry again for my poor knowledge about languages (all kind of... ;) )
Maurizio.
 

pompierecattivo

Member
Licensed User
Longtime User
Hello Erel,
thanks for answer.
I try to copy and paste the code, hope i havent to do nothing particular about it..
As i wrote in first post, i did a merge of two examples code that i think runs correctly each one..
I will clean code and study better on it and also insert my own, but the first thingh is make this one full functionally.
Just for explain: There are a maps free apllication for pc called "OkMap" that have a build-in server listenin for gps data string. I'm searching to connect it and send continuosly data to trace a float of devices...
Thanks again and again sorry for bad english.
Maurizio.



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 Socket1 As Socket 
   Dim ServerSocket1 As ServerSocket 
   'Dim InputStream1 As InputStream
   Dim AStreams As AsyncStreams 
   Dim OutputStream1 As OutputStream
   'Dim Conv As ByteConverter
   Dim Timer1 As Timer 
   Dim MyIP As String 
   Dim ServerIp As String 
   Dim iStr As String 'input string
   Dim Tracciato As String
   Dim GPS1 As GPS
   Dim Luppa As Int


   
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 lblConnect As Label
   Dim btnConnect As Button
   Dim lvLog As ListView
   Dim lblMsg As Label 
   
   Dim lblLon As Label
   Dim lblLat As Label
   Dim lblSpeed As Label
   Dim lblSatellites As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Dim t As String 
   
   Activity.LoadLayout("1")
   btnConnect.Enabled = False
   
   'Todo: Change typeface of lvLog
   lvLog.SingleLineLayout.ItemHeight = 16dip
   lvLog.SingleLineLayout.Label.TextSize = 12
   MyIP = ServerSocket1.GetMyIP 
   lblConnect.Text = MyIP
   
   If FirstTime = True Then
      btnConnect_Click
   End If
   
   If FirstTime Then
      GPS1.Initialize("GPS")
    End If
   
   Activity.LoadLayout("1")
   
   
End Sub

Sub Activity_Resume
   If GPS1.GPSEnabled = False Then
      ToastMessageShow("Please enable the GPS device.", True)
      StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
   Else
      GPS1.Start(0,0) 'Listen to GPS with no filters.
   End If

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   GPS1.Stop

End Sub

Sub btnConnect_Click
   Select Case btnConnect.Text 
      Case "Connect"
         btnConnect.Text = "Disconnect"
         Socket1.Initialize("Socket1")
         'Set correct ServerIp 
         Socket1.Connect("192.168.43.150",30175,10000)
      Case "Disconnect"
         btnConnect.Text = "Connect"
         Socket1.Close 
         'ExitApplication
      Case Else
   End Select
End Sub


Sub GPS_LocationChanged (Location1 As Location)
Luppa=Luppa+1
If Luppa > 4 Then
   lblLat.Text = "Lat = " & Location1.Latitude
   lblLon.Text = "Lon = " & Location1.Longitude
   lblSpeed.Text = "Speed = " & Location1.Speed
   
   Tracciato = "Maurizio;" & Location1.Longitude & ";" & Location1.Latitude & ";" & Location1.Altitude &";"&DateTime.Date(DateTime.Now)& " " & DateTime.Time(DateTime.Now) & ";" & Location1.Speed & ";" & Location1.Bearing & ";3;15"
      SendData (Tracciato)
      Luppa=0
End If
   
   
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
   ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
   lblSatellites.Text = "Satellites:" & CRLF
   For i = 0 To Satellites.Size - 1
      Dim Satellite As GPSSatellite
      Satellite = Satellites.Get(i)
      lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
         " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
         & " " & Satellite.Elevation
   Next
End Sub


Sub Socket1_Connected(Connected As Boolean)As Boolean 
   If Connected = True Then
      ToastMessageShow("Connected",True)
      lvLog.AddSingleLine("Connected!")
      AStreams.Initialize( Socket1.InputStream ,Socket1.OutputStream,"Astreams")
   Else
      ToastMessageShow("Server not available",True)
      lvLog.AddSingleLine("Server not available")
      btnConnect.Text = "Connect"
   End If
   btnConnect.Enabled = True
End Sub

Sub AStreams_NewData (Buffer () As Byte)
 '   Dim msg As String
'   Dim cMsg As String 
'   Dim t As String 
'   Dim x As Int 
'   Dim unsignedi As Int
'   Dim signedb As Byte
   
 '   msg = BytesToString( Buffer, 0, Buffer.Length, "ASCII")
'   lvLog.AddSingleLine (msg)
'   Log(msg)
'   For i = 0 To msg.Length -1
'      signedb = Buffer(i)
'      unsignedi = Bit.And(0xff, signedb)
'      cMsg = cMsg & Chr(unsignedi)
'   Next
'   lvLog.AddSingleLine(cMsg)
'   Log(cMsg)
'   lblMsg.Text = x & " / " & msg.Length & " - " & msg
   
End Sub

Sub SendData(msg As String)
     Dim Buffer() As Byte 

    Buffer = msg.GetBytes( "UTF8")
     AStreams.Write (Buffer)
     'AStreams.Write(Array As Byte(254))
     'AStreams.flush
End Sub

Sub btnTest_Click
   Dim msg As String
   
   msg = "Ciao !!!!!"
   SendData(msg)
End Sub
 
Last edited by a moderator:
Upvote 0
Top