Android Question Wifi for Printer with Cellular for Internet

B4AinNY

Member
Licensed User
This is really an Android question more than B4X, but here's the situation.

My app needs to have internet access for uploading / downloading data
and at the same time needs to be able to print.
Unfortunately the Printer will not be on a network with internet access.

What I've found is that connecting to the printer for WiFi works great
but it seems to disable the cellular Internet connection and and
insists on relying on WiFi for the internet access.
If there is no internet access over WiFi, the app can't connect
to the internet over the tablet's cellular network connection.
If I turn off WiFi, then the app can connect to the internet but can't
print.

Is there a solution where one can connect to a printer over WiFi,
but use the Cellular connection for Internet access ?

If I used a Bluetooth printer, would Bluetooth connection have
the same problem? ( interference with Cellular internet access )


- Jeff ( B4AinNY )

=====================================
 

MitchBu

Well-Known Member
Licensed User
Longtime User
I wonder if you could not set your phone as hot spot and connect the printer to it as tether.

This should let you use the cell data Internet, and access the printer at the same time.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If I used a Bluetooth printer, would Bluetooth connection have
the same problem?
It will not have this problem.

You can use this library to enable or disable the wifi: Simple WIFI library
Disable it when you want to connect to the internet and reenable it when you want to connect to the printer.
 
Upvote 0

B4AinNY

Member
Licensed User
> You can use this library to enable or disable the wifi: Simple WIFI library

Thanks - I'll definitely try it, and really appreciate taht.
But Oy ! - I am dreading the reaction of users to the delay
I expect reconnecting to WiFi for each print . . .
- these days those seconds can seem like eternity to an end user.

- Jeff ( B4AinNY )
 
Upvote 0

B4AinNY

Member
Licensed User
It is very annoying that Androids can not use Cellular internet connection when connected to a WiFi network.
When in a truly mobile environment Mobile printer is unlikely to be on a network that has internet access and Bluetooth printers are no longer being made.

So - I am hoping to use the Simple WIFI library to turn on WiFi and connect to a specific WiFi Printer
that is not on a network with Internet access, and then turn WiFi back off again
when done so the phone reconnects to Internet cellularly.

I'd welcome any feedback on my approach or code. ( I'm still pretty new with this )
If my code works for anyone else, feel free to use it.

B4X:
 Sub ConnectToWiFiPrinter_and_then_Print
           
        ' Process for Printing

          Dim strDesired_WiFi_SSID As String = "SSID OF THE DESIRED PRINTER GOES HERE"

        ' See if We are connected
            If Not ( wifi.isWifiConnected )Then
                ' Connect to WiFi
                wifi.EnableWifi(True)
                ' Wait for WiFi Connection
                Dim startWaitTime As Long  = DateTime.Now
                Do Until (wifi.isWifiConnected)
                    DoEvents
                    ' Make sure we can break out if this is going on forever
                    If DateTime > startWaitTime + 3000 Then   ' check if over 3 seconds
                        Dim result As Int
                        result = Msgbox2("Continue to Wait ? ", "Waiting for WiFi", "Yes", "" , "No", Null)
                        If result = DialogResponse.Positive Then
                            ' User ok to wait, reset startWaitTime for next interval
                            startWaitTime = DateTime.Now
                        Else
                            Msgbox("No WiFi Printer found, Exiting Printing", "WiFi Status")
                            Return ' Don't bother with printing
                                    ' NOTE - append a note to e-mail that Printing failed
                                    ' or ask user to write Ticket # down somewhere
                        End If
                    End If
                Loop   ' WiFi.IsOnline
            End If
           

            ' Scan for the desired printer WiFi
            ' '     Scan only once in app
            ' '     After that use Update
            ' ' WifiScanner.startScan("WifiScanner")  ' Will trigger ScanDone   ' orriginally passed "Scan"
            WifiScanner.updateWifiList
            DoEvents
           
            ' Go through list of WiFis found and get Entry Number of desired SSID
            Dim CommaLocation As Int
            Dim tmpString As String
            Dim strEntry As String
            Dim EntryNumber As Int
            EntryNumber = -1
            Dim i As Int
            For i = 0 To WifiScanner.Wifis.Length - 1
                tmpString = WifiScanner.Wifis(i)
                CommaLocation = tmpString.IndexOf(",")
                strEntry = tmpString.SubString2(0, CommaLocation)
                If (strEntry = strDesired_WiFi_SSID ) Then  EntryNumber = i
            Next
           
            ' Abandon Printing if Printer's WiFi is not found
            If EntryNumber =-1 Then
                Msgbox ("Desired SSID not found, Exiting ", "WiFi Printer lblStatus")
                Return
            End If
               
            ' When WiFi connection is made, check that it is proper SSID
            If  Not (wifi.SSID = strDesired_WiFi_SSID ) Then
                Msgbox("WiFi found but wrong SSID - waiting to connect to Printer", "WiFi Status")
                ' connect to the desired device
                WifiScanner.connectToAP(EntryNumber)
                'Wait to connect to this device

                startWaitTime = DateTime.Now

                Do Until (wifi.SSID = strDesired_WiFi_SSID )
                    DoEvents
                    ' Make sure we can break out if this is going on forever
                    If DateTime > startWaitTime + 3000 Then   ' check if over 3 seconds
                        Dim result As Int
                        result = Msgbox2("Continue to Wait ? ", "Waiting for WiFi", "Yes", "" , "No", Null)
                        If result = DialogResponse.Positive Then
                            ' User ok to wait, reset startWaitTime for next interval
                            startWaitTime = DateTime.Now
                        Else
                            Msgbox("Desired Printer not found, Exiting Printing", "Printer Access Status")
                            Return ' Don't bother with printing
                                    ' NOTE - append a note to e-mail that Printing failed
                                    ' or ask user to write Ticket # down somewhere
                        End If
                    End If
                Loop
            End If

            Print    ' Goes to print routine
            'After Printing, Disconnect from wifi if printer network doesn't have internet
            'This allows us to be ready to use Cellular internet elsewhere in the app
            If wifi.Isonline = False Then wifi.EnableWifi(False)
        End Sub
 
Upvote 0

B4AinNY

Member
Licensed User
I've found a number of problems with my earlier posted code. I am resolving these and will post an update when I have this working for others to see.

- Jeff
 
Upvote 0
Top