Android Question I am having problems with opening two serial objects

davepamn

Active Member
Licensed User
I have one serial object connected to a blue tooth device in a service module and a second serial object connected to another blue tooth device in a second service module

I can connect to both serial objects in debug, but in release, the second serial object crashes the module.


If I stop the first service then the second service can discover the second blue tooth device and communicate with it. However, if I start the first service again, it crashes when connecting to the serial object.

it says "error connecting to the data"

The second bluetooth Admin cause the stream to not work

My work around, is use the "GetPairedDevices" api and instruct the user to manually pair to the second bluetooth device rather than bluetoothadmin. I would like to use bluetoothadmin for both serial objects.

I would like to know if you can recreate the error
 
Last edited:

davepamn

Active Member
Licensed User
the first service module connects to an input stream device.

The second service Module code connects to a output stream to a zebra bluetooth printer
-service module name is printservice
B4X:
sub Process_globals

Public printer_admin As BluetoothAdmin
    Public printer_AStream As AsyncStreams

    Public printer_serial1 As Serial

    Public printer_foundDevices As List

    Public PrinterList as List

    Public connectedPrinterDevice As NameAndMac
end sub

Sub HasPrinter(Paireddevices As Map,sPrinter As String) As Boolean

    Dim i As Int

    Dim bFound As Boolean=False

    Dim sKey As String

    For i =0 To Paireddevices.Size-1

        sKey=Paireddevices.GetKeyAt(i)

        If LTrim(sKey)=LTrim(sPrinter) Then

            bFound=True

        End If

    Next

    Return(bFound)

End Sub
Sub LTrim(s As String) As String

  Dim m As Matcher = Regex.Matcher("^(\s+)", s)

  If m.Find Then

    Return s.SubString(m.GetEnd(1))

  Else

    Return s

  End If

End Sub

Sub PrinterSerial_Connected(Success As Boolean)

    If Success Then

        Main.bGlobalPrinterSerialConnected=True

        If printer_AStream.IsInitialized = False Then

            printer_AStream.Initialize(printer_serial1.InputStream,printer_serial1.OutputStream,"PrinterAStream")

        End If

        Log("Printer connected")

    Else

        Main.bGlobalPrinterSerialConnected=False

        Log("Printer did not connect")

    End If


End Sub

Sub ConnectToPrinterBlueTooth()

    Try

        CallSubDelayed2(Main,"DisplayProgressDialogShow","Connecting to Printer Blue Tooth")


        Dim PairedDevices As Map

        If printer_serial1.IsInitialized=False Then

            printer_serial1.Initialize("PrinterSerial")

        EndIf

        PairedDevices=printer_serial1.GetPairedDevices

        If HasPrinter(PairedDevices,"GD200P")=False Then

            'CallSubDelayed2(Main,"DisplayServiceErrorMessage","You must pair the Blue Tooth Printer Manually then try again")



            If printer_admin.IsInitialized=False Then

                printer_admin.Initialize("PrinterAdmin")

            End If

  

            If printer_foundDevices.IsInitialized=False Then

                printer_foundDevices.Initialize

            End If

  

            printer_foundDevices.Clear



            If printer_admin.StartDiscovery    = False Then

                 CallSubDelayed2(Main,"DisplayProgressDialogShow","Error start discovery printer process")

            Else

                CallSubDelayed2(Main,"DisplayProgressDialogShow","Searching for printer devices")

              End If

        Else

            Dim oPDList As List

            oPDList.Initialize

            Dim nm AsNameAndMac

            If nm.IsInitialized=False Then

                nm.Initialize

            End If

            For i =0 To PairedDevices.Size-1

                nm=PairedDevices.get(i)

                If LTrim(PairedDevices.GetKeyAt(i))="GD200P" Then

                    oPDList.Add(PairedDevices.GetKeyAt(i))

                End If

            Next

            Dim bFound As Boolean=False

            For i =0 To oPDList.size-1

                printer_serial1.Connect(PairedDevices.Get(oPDList.get(i)))

                CallSubDelayed2(Main,"SetPrinterLabel",printer_serial1.name & " " & printer_serial1.address)

                bFound=True

            Next

            If bFound=False Then

            CallSubDelayed2(Main,"SetPrinterLabel","No Printer Devices Found")

            End If

        End If

        CallSubDelayed2(Main,"InvokeProgressDialogHide","")

    Catch

        CallSubDelayed2(Main,"DisplayServiceErrorMessage","printservice Error: ConnectToBlueTooth: " & LastException.Message)

End Try



End Sub

Sub PrinterAdmin_DeviceFound (Name As String, MacAddress As String)

    'Log(Name & ":" & MacAddress)

    Dim nm As NameAndMac

    If nm.IsInitialized=False Then

        nm.Initialize

    End If



    If Name.Contains("GD200P") Then

        nm.Name = Name

        nm.Mac = MacAddress

        printer_foundDevices.Add(nm)

    End If

     CallSubDelayed2(Main,"DisplayProgressDialogShow","Searching For Printer devices (~ device found)...".Replace("~", printer_foundDevices.Size))

End Sub



Sub PrinterAdmin_DiscoveryFinished

Try

    CallSubDelayed2(Main,"InvokeProgressDialogHide","")



    If Main.bGlobalPrinterSerialConnected=False Then

        If printer_foundDevices.Size = 0 Then

              CallSubDelayed2(Main,"SetPrinterLabel","No Printer Device Found")

        Else

      
            SelectPrinter

        End If

    End If

      

    CallSub2(Main,"PrinterAdminSelectionDone","")



Catch

    'oError.ShowLastException("Error : PrinterAdmin_DiscoveryFinished")

    CallSubDelayed2(Main,"DisplayServiceErrorMessage","printservice Error: PrinterAdmin_DiscoveryFinished: " & LastException.Message)

End Try

End Sub

Public Sub SelectPrinter()

    Try

        If oPrinterList.IsInitialized=False Then

            oPrinterList.Initialize

        End If
 

        oPrinterList.Clear
        
        For i = 0 To printer_foundDevices.Size - 1

            Dim nm As NameAndMac

            If nm.IsInitialized=False Then

                nm.Initialize

            EndIf

            nm = printer_foundDevices.Get(i)

            oPrinterList.Add(nm.Name)

        Next

  
        If connectedPrinterDevice.IsInitialized=False Then

            connectedPrinterDevice.Initialize

        End If

  

        CallSub2(Main,"SelectPrinter","")

      'Get the Mac address from an input dialog

        printer_serial1.Connect(connectedPrinterDevice.Mac)

        CallSubDelayed2(Main,"SetPrinterLabel",connectedPrinterDevice.name & " " & connectedPrinterDevice.Mac)



    Catch

        CallSubDelayed2(Main,"DisplayServiceErrorMessage","printservice Error: selectprinter " & LastException.Message)

End Try

End Sub

'****Main*****

Sub selectprinter(sMsg As String)

        Dim res As Int

        Dim connectedDevice As NameAndMac

        res = InputList(PrintService.oPrinterList, "Choose a printer device to connect", -1)

        If res <> DialogResponse.Cancel Then

            connectedDevice = PrintService.printer_foundDevices.Get(res)

            ProgressDialogShow("Trying to connect to printer : " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")

            ProgressDialogHide

            PrintService.connectedPrinterDevice.name=connectedDevice.name

            PrintService.connectedPrinterDevice.Mac=connectedDevice.Mac

        End If

End Sub
 
Last edited:

Beja

Expert
Licensed User
Hi,
I think the problem is not with the external I/O devices, but the number of serial channels in your phone or device. I think there is only one single channel used by the bluetooth.
If you know the MAC addresses of the external devices then you can multiplex the i/o operations, by closing the serial port and then open again with the second MAC address.

Disclaimer: Not so sure : )
 

davepamn

Active Member
Licensed User
Connect to a real device in USB debugging mode

I need the instruction for connecting in usb debug mode for a "Samsung Galaxy".

I current use "b4a-bridge" to do my debugging.
 
Last edited:
Top