Android Question BT Issues with Survey Instruments

Terradrones

Active Member
Hi All

I need help again please. I can pair with a TS (Total Station) with no issues. But when I send a command to a Topcon TS to read a distance and the angles, there is no reaction on the TS.

I have a BT class, which is as follows:
BT Class:
[/
Sub Class_Globals
    'Bluetooth
    Public Serial As Serial
    Public Admin As BluetoothAdmin
    Public AStream As AsyncStreams
    Public FoundDevices As List
    Type NameAndMac (Name As String, Mac As String)
    Dim nm As NameAndMac
    Public ReadData As String
'    Private rp As RuntimePermissions
    Public BluetoothState, ConnectionState As Boolean
    Dim i As Int
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Try
        Admin.Initialize("Admin")
        Serial.Initialize("Serial")
        Admin.Enable
        Serial.Listen
        If Admin.IsEnabled = False Then
            If Admin.Enable = False Then
                ToastMessageShow("Error Enabling Bluetooth...", True)
            End If
        Else
            BluetoothState = True
        End If
    Catch
        Log(LastException)
    End Try
    i=0
End Sub

Private Sub Admin_StateChanged (NewState As Int, OldState As Int)
'    Log("state changed: " & NewState)
    BluetoothState = NewState = Admin.STATE_ON
    NotifyOfStateChanged
End Sub

Private Sub NotifyOfStateChanged
'    For Each Target In Array(Main, ChatActivity)
'        CallSub(Target, "UpdateState")
'    Next
End Sub

Public Sub Connect As Boolean
    Dim PairedDevices As Map
    
    PairedDevices = Serial.GetPairedDevices
    Dim l As List
    l.Initialize
    For i = 0 To PairedDevices.Size - 1
        l.Add(PairedDevices.GetKeyAt(i))
    Next
    Serial.Disconnect
    Dim Res As Int
    Res = InputList(l, "Select Instrument", -1) 'show list with paired devices
    If Res <> DialogResponse.CANCEL Then
        ProgressDialogShow2("Trying To Connect...", True)
        Serial.Connect(PairedDevices.Get(l.Get(Res)))         'convert the name to mac address
        CGlobals.IMac = PairedDevices.Get(l.Get(Res))        'Mac Address
        CGlobals.IName = PairedDevices.GetKeyAt(Res)        'Name
        Return True
    Else
        Return False
    End If
End Sub

Sub Serial_Connected (Success As Boolean)
    ConnectionState = Success
    If Success = False Then
        ProgressDialogHide
        'Return
        i=i+1
        If i<10 Then
            ConnectTo(CGlobals.DeviceCon)
        Else
            ToastMessageShow("Error Connecting To Instrument", True)
            Return
        End If
    Else
        Try
            If AStream.IsInitialized Then AStream.Close
            'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
            AStream.Initialize(Serial.InputStream, Serial.OutputStream, "astream")
            If CGlobals.Code1=1 Then Instruments
            ProgressDialogHide
            Serial.Listen
            Return
        Catch
            Log(LastException)
        End Try
    End If
    NotifyOfStateChanged
End Sub

Sub Instruments
    ProgressDialogHide
    If CGlobals.li = 0 Then
        CGlobals.DF(24)=CGlobals.IName
        CGlobals.DF(25)=1
        CGlobals.DF(26)=CGlobals.IMac
        Msgbox2Async("CEASER Has Connected To Level: " & CGlobals.IName, "Connected", "OK", "", "", Null,False)
    Else If CGlobals.Li=1 Then
        'Total Station
        CGlobals.DF(5)=CGlobals.IName
        CGlobals.DF(6)=1
        CGlobals.DF(7)=CGlobals.IMac
        Msgbox2Async("CEASER Has Connected To TS: " & CGlobals.IName, "Connected", "OK", "", "", Null,False)
    Else If CGlobals.Li=2 Then
        'GPS
        If CGlobals.BaseChecked=1 Then
            CGlobals.DF(33)=CGlobals.IName
            CGlobals.DF(34)=1
            CGlobals.DF(35)=CGlobals.IMac
            Msgbox2Async("CEASER Has Connected To Base: " & CGlobals.IName, "Connected", "OK", "", "", Null,False)
        Else If CGlobals.RoverChecked=1 Then
            CGlobals.DF(52)=CGlobals.IName
            CGlobals.DF(53)=1
            CGlobals.DF(54)=CGlobals.IMac
            Msgbox2Async("CEASER Has Connected To Rover: " & CGlobals.IName, "Connected", "OK", "", "", Null,False)
        End If
    End If
End Sub

Public Sub ConnectTo (Device As NameAndMac)
    Sleep(350)
    Try
        Serial.Disconnect
        Sleep(450)
        Serial.Connect(Device.Mac)
        'AStream.Initialize(Serial.InputStream, Serial.OutputStream, "AStream")

        Sleep(450)
    Catch
        Serial.Initialize("Serial")
    End Try
End Sub

Public Sub Disconnect
    If AStream.IsInitialized Then AStream.Close
    Serial.Disconnect
End Sub

Public Sub Disable
    Admin.Disable
End Sub

Public Sub SearchForDevices As Boolean
    Return Admin.StartDiscovery
End Sub

Public Sub Admin_DiscoveryFinished
    CallSub(Parameter, "DiscoverFinished")
End Sub

Public Sub Admin_DeviceFound (Name As String, MacAddress As String)
    If Name<>"" And MacAddress<>"" Then
        Dim nm As NameAndMac
        nm.Name = Name
        nm.Mac = MacAddress
        FoundDevices.Add(nm)
    End If
End Sub

Public Sub AfterSuccessfulConnection
    If AStream.IsInitialized Then AStream.Close
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
    AStream.Initialize(Serial.InputStream, Serial.OutputStream, "AStream")
    Serial.Listen
End Sub

Public Sub StopReading'(A As String)
    Serial.StopListening
End Sub

Public Sub ReadDeviceDataAsync (Buffer() As Byte)
    ReadData=BytesToString(Buffer, 0, Buffer.Length, "UTF8")
End Sub

Public Sub SendDataToDevice(A2 As String)
    AStream.Write(A2.GetBytes("utf8"))
End Sub

Public Sub CloseConnection
    
End Sub

Public Sub ClearOutputData
    'Serial.OutputStream'=Null
End Sub

Public Sub ClearBuffer
    
End Sub

Public Sub Pair
    
    
    
    
'    Serial.Listen
'    
'    Dim StartingIntent As String
'    
'    If StartingIntent.Action = "ACTION_PAIRING_REQUEST" Then
'        Dim jo As JavaObject = StartingIntent
'        Dim device As JavaObject = jo.RunMethod("getParcelableExtra", Array("android.bluetooth.device.extra.DEVICE"))
'        device.RunMethod("setPairingConfirmation", Array(True))
'    End If
End Sub
]

Then I have a routine to send a command to the TS:

[CODE lang="b4x" title="Read TS"][/
Sub ReadTopoData
    Try
        'TarHgt.Text = NumberFormat2(Target.Text,1,3,3,False)
        'EditText1.RequestFocus
        'If CGlobals.ReadFlag = 0 Then CGlobals.ReadFlag = 1 Else CGlobals.ReadFlag = 0 ' CGlobals.ReadFlag = 1 is reading , flgread = 0 activate when stop reading
        Select Case CGlobals.Li
            Case 1
                'This is for the TS....
'                If CGlobals.ReadFlag <> 1 And CGlobals.InstType < 10 Then
'                    If CheckBubble() = -1 Then Return
'                End If
                If CGlobals.ReadFlag = 0 Then CGlobals.ReadFlag = 1 Else CGlobals.ReadFlag = 0 ' CGlobals.ReadFlag = 1 is reading , flgread = 0 activate when stop reading
                'Target.Text = NumberFormat2(Target.Text,1,3,3,False)
                'CGlobals.PH=NumberFormat2(Target.Text,1,3,3,False)
                If CGlobals.ReadFlag = 1 Then
                    ReadLabel.Visible = True 
                    ReadTopo.Text = "Stop"
                    ManualTSRead.ReadMode = "ReadHVS"
                    ReadInst.ReadAll()
'                    Wait For ReadInstComplete
                    If CGlobals.DF(3)>0 Then
                        Dim job As HttpJob
                        job.Initialize("",Me)
                        Wait For (job) Jobdone (job As HttpJob)
                    Else
                        Sleep(0)
                    End If
                    If ReadInst.HVD(1) > -999999 And ReadInst.HVD(2) > -999999 And ReadInst.HVD(3) > 0 Then
                        HAngle.Text = NumberFormat2(ReadInst.HVD(1), 1,4,4,False)
                        VAngle.Text = NumberFormat2(ReadInst.HVD(2), 1,4,4,False)
                        SDist.Text = NumberFormat2(ReadInst.HVD(3), 1,3,3,False)
                        CalcTopo
                        CGlobals.ReadFlag = 0
                        ReadTopo.Text = "Read"
                        ReadLabel.Visible = False
                    Else
                        ReadTopo.Text = "Read"
                        ReadLabel.Visible = False
                        'StopRead()
                        CGlobals.ReadFlag = 0
                    End If
                Else
                    ReadTopo.Text = "Read"
                    ReadLabel.Visible = False
                    'StopRead()
                End If
            Case 2
                'This is for the GPS
'                If CGlobals.ReadFlag = 1 Then
'                    Timer1.Enabled=True
'                    ReadTopo.Text = "Log"
'                Else
'                    If CGlobals.Y3<>0 Then
'                        ReadTopo.Text = "Start"
'                        Timer1.Enabled=False
                        CalcTopo
'                    End If
'                End If
            End Select
    Catch 
        
    End Try
    
End Sub

]

Here I send the command to the TS:

[CODE lang="b4x" title="Send Command"][/
Sub IntTopcon(F As Int, KOM As String, R As String)
    Dim i1, i3 As Int
    Dim TempStr As String = ""
    Dim strLen As Int = 24

    't - angles only
    'u - angles and distance
    If R = "t" Then strLen = 24 Else strLen = 30

'    Try

        A = ""
        Select Case F
            Case 0
                'Sets the Mode - angles & distances
                BT.SendDataToDevice(KOM & CRLF)
                Sleep(0)
                BT.ClearOutputData()
                Return
            Case 1
                'Read
                BT.ClearOutputData()
                BT.SendDataToDevice(KOM & CRLF)
                Sleep(0)
                'BT.SendDataToDevice("NTS_Read_distance_SD()" & CRLF)
                'NTS_Read_distance_SD(void)
                A=BT.ReadData
            'BT.ReadDeviceDataAsync(A)
                Sleep(0)
                For i3 = 0 To 1000
                    Sleep(0)
                    If CGlobals.ReadFlag = 0 Then
                        Try
                            BT.StopReading'(AK & CRLF)
                        Catch
                            Log(LastException)
                        End Try
                        Return
                    End If
                Next

                Do Until (A.Length > 30 And (A.Contains("?+") Or A.Contains("(+"))) Or (A.Length > 24 And (A.Contains("<") Or A.Contains("'")))
                    'A.Lenght more then 30 is for distance measurment
                    'A.Lenght more then 24 is for angles only measurments
                    For i3 = 0 To 1000
                        Sleep(0)
                        If CGlobals.ReadFlag = 0 Then
                            Try
                                BT.StopReading'(AK & CRLF)
                            Catch
                                Log(LastException)
                            End Try
                            Return
                        End If
                    Next

                    'Sleep(50)
                    'BT.ReadDeviceDataAsync(Bit)
                    A = A & BT.ReadData
                    If A.Contains("(+") Then A = A.Substring(A.IndexOf("(+"))
                    If A.Contains("?+") Then A = A.Substring(A.IndexOf("?+"))
                    If A.Contains("<") Then A = A.Substring(A.IndexOf("<"))
                    If A.Contains("'") Then A = A.Substring(A.IndexOf("'"))
'                    If CGlobals.ReadFlag = 0 Then
'                        Try
'                            'BT.StopReading(AK & CRLF)
'                        Catch
'                            Log(LastException)
'                        End Try
'                        Return
'                    End If
                Loop
                'TraceToFile(String.Format("ReadInst-IntTopcon-RAW AFTER CUT={0}", A), TraceTypes.Regular)
                ''Msgbox(A)
        End Select
'    Catch
'        Log(LastException)
'    End Try

    'TraceToFile("ReadInst-IntTopcon-AFTER READING LOOP", TraceTypes.Regular)
    Try
        'BT.StopReading(AK & CRLF)
    Catch
        Log(LastException)
    End Try

    If R = "t" Then
        'angles only
        Try
            'Fine - GoWin
            ''06601080+04459580+02590d117

            'Corse
            '<2703507+0522637+0008d083
'            If Not A.Contains("d") Then 'if angles in gon or mil
'                'TraceToFile("ReadInst-IntTopcon-Angles in GON", TraceTypes.Regular)
'                Return
'            End If

            If A.Contains("<") Then
                i1 = A.IndexOf("<")
            Else
                i1 = A.IndexOf("'")
            End If

            If i1 > -1 Then
                HVD(2) = A.Substring2(i1 + 1, 7)
                If IsNumber(HVD(2)) = True Then
                    HVD(2) = HVD(2) / 10000
                End If

                'TraceToFile(String.Format("ReadInst-IntTopcon-Angles-HVD(2)={0}", HVD(2).ToString()), TraceTypes.Regular)

                i1 = A.IndexOf("+")
                HVD(1) = A.Substring2(i1 + 1, 7)
                If IsNumber(HVD(1)) = True Then
                    HVD(1) = HVD(1) / 10000
                End If

                'TraceToFile(String.Format("ReadInst-IntTopcon-Angles-HVD(1)={0}", HVD(1).ToString()), TraceTypes.Regular)
            End If
        Catch
            Log(LastException)
        End Try
    Else
        'Fine readings of 0.2mm - GoWin
        '(+000022800m06555580+35524130d+000020818t60+0000+000**074
        '(+011784812m08520300+12030400d+011745724t15+0000+025**066ETX(CRLF)

        'Corse readings 0f 1mm
        '?+00002280m0655558+3552413d+000020818t60+0000+000**074
        '?+01178481m0852030+1203040d+01174572t15+00+25099ETX(CRLF)
        Try
            If A.Contains("f") Then 'distance in feet
                'Msgbox("Please change distance to meters")
                'TraceToFile("ReadInst-IntTopcon-Distance not in meters", TraceTypes.Regular)
                Return
            End If
'            If Not A.Contains("d") Then 'if angles in gon or mil
'                'TraceToFile("ReadInst-IntTopcon-Angles-1 in GON", TraceTypes.Regular)
'                'Return
'            End If

            i1 = A.IndexOf("m") + 1
            If i1 > -1 Then

                TempStr = A.Substring2(i1, A.IndexOf("+") - i1) 'Getting data between the "m" and second "+"

                If TempStr.Length = 8 Then 'if it in Fine mode it will have one digit more at the end
                    HVD(2) = TempStr.Substring2(0, 7)
                Else
                    HVD(2) = TempStr
                End If

                If IsNumber(HVD(2)) = True Then
                    HVD(2) = HVD(2) / 10000
                End If

                i1 = A.IndexOf("+") + 1
                TempStr = A.Substring2(i1, A.IndexOf("d") - i1) 'Getting data between the second "+" and "d"

                If TempStr.Length = 8 Then 'if it in Fine mode it will have one digit more at the end
                    HVD(1) = TempStr.Substring2(0, 7)
                Else
                    HVD(1) = TempStr
                End If

                If IsNumber(HVD(1)) = True Then
                    HVD(1) = HVD(1) / 10000
                End If

                i1 = A.IndexOf("+") + 1
                TempStr = A.Substring2(i1, A.IndexOf("m") - i1) 'Getting data between first "+" and "m"

                If TempStr.Length = 9 Then
                    HVD(3) = TempStr.Substring2(0, 8)
                Else
                    HVD(3) = TempStr
                End If

                ''Msgbox("SD:" + HVD(3).ToString())
                'If IsNumber(HVD(3)) = True Then HVD(3) = HVD(3) / 1000
                If IsNumber(HVD(3)) = True Then
                    HVD(3) = HVD(3) / 1000
                    If CGlobals.SCF = 0 Then CGlobals.SCF = 1
                    HVD(3) = ((HVD(3) + CGlobals.DF(18)) / 1000) * CGlobals.SCF
                End If

                'TraceToFile(String.Format("ReadInst-IntTopcon-Angles-HVD(3)={0}", HVD(3).ToString()), TraceTypes.Regular)
            End If
        Catch
            Log(LastException)
        End Try
    End If

    'TraceToFile(String.Format("ReadInst-IntTopcon-Exit-HVD(1)={0} HVD(2)={1} HVD(3)={2}", HVD(1).ToString(), HVD(2).ToString(), HVD(3).ToString()), TraceTypes.Regular)

End Sub

]

But nothing is happening.

Any help please?
 

Terradrones

Active Member
Hi Erel
I am using the correct protocols for the different Total Stations and GPS equipment. They are correct as I use them in the Windows Mobile version of the Ceaser program and they work.
 
Upvote 0
Top