Android Question Bluetooth Serial Connect

rbw152

Member
Licensed User
Longtime User
Hi all,

Seems my barcode scanners can't connect to my device using Serial. They're paired and connected in androids own bluetooth screen, but I can't get my app to connect via Serial.

I get the following exception in Sub Serial1_Connected:

java.io.IOException: read failed, socket might closed or timeout, read ret: -1

Any idea what might be happening to throw that message?

Thanks.
 

wcieslik

Member
Licensed User
Longtime User
It is probably not listening for connections. At least not on the default UUID address.
I have exactly the same issue connecting to "some" BlueTooth ELM327's.
It first started after changing phones from a Galaxy Sii to a Sony Expiria Z3.
Some ELM's I can't connect to, but others are fine.
The exception message is identical to rbw152's.

in sub serial1_connected
MAIN:Serial1_connect:SERIAL1_CONNECTED. 'Success'=false
Main:Serial1_connect failed:Cannot connect to : OBDII at : 11:22:33:DD:EE:FF
Main:Serial1_connect failed:java.io.IOException: read failed, socket might closed or timeout, read ret: -1

More disturbingly, a number of people using my app have started reporting the same issue, always after a new phone or OS upgrade.

I've started looking into using serial.connect3 but I'm not sure if the port number arg has to be the port the device is litening on, or can I use any port number. ie just nominate a port... ?

fairly confused about this ...
Also curiouse to know if there is a way to change the timeout for serial connect... eg how long keeps trying before failing.

Regards,
Wit C.
 
Upvote 0

wcieslik

Member
Licensed User
Longtime User
just updating what I think is working for me (at least).
I stopped using serial1.connect and replaced it with serial1.connect3(mac,port) and used (arbitrarily) port 1

You can see what I've changed below, this seems to work OK.

B4X:
        'serial1.Connect(Config.BT_MAC)    'attempt to connect to the last known good MAC
        serial1.Connect3(Config.BT_MAC,1)
 
Upvote 0

wcieslik

Member
Licensed User
Longtime User
just updating what I think is working for me (at least).
I stopped using serial1.connect and replaced it with serial1.connect3(mac,port) and used (arbitrarily) port 1

You can see what I've changed below, this seems to work OK.

B4X:
        'serial1.Connect(Config.BT_MAC)    'attempt to connect to the last known good MAC
        serial1.Connect3(Config.BT_MAC,1)


Hmmm... This actually didn't fix the issue ....
Restarting the BT device brought me back to square one!

I tried stepping up through the ports with a loop, and then stepping up again using connectinsecure.
With this method I found connectinsecure worked at port 1 every time.
so this is now my fallback if the serial1.connect fails.

B4X:
        serial1.Connectinsecure(admin, Config.BT_MAC, 1)

I'm finding this connection 'times out' if no activity after about 20 seconds... I might need a keep alive ?
 
Upvote 0

sunish

Member
Licensed User
Longtime User
For me even connectinsecure is not letting me connect to the saved mac. However if I select from the saved paired list it invariably connects to the ELMbluetooth module.
I get the same error message "java.io.IOException: read failed, socket might closed or timeout, read ret: -1" on failed connection to previously saved MAC whether it is

serial1.Connect("84:73:03:F0:7E:D2") ' hard coded previous value
serial1.Connectinsecure(admin, LastBTMac, 1) ' connect insecure
serial1.Connect(LastBTMac)
serial1.Connect2(LastBTMac, "00001101-0000-1000-8000-00805F9B34FB")
serial1.Connect3(LastBTMac,1) ' with port number 1

One important observation is that when connecting to a saved mac using the above methods, the error message appears instantly as if there was no attempt to connect.
Erel, Is there some way to invoke or force attempt of connection ?
 
Upvote 0

sunish

Member
Licensed User
Longtime User
Sure for two reasons
1. A reconnect using the already paired list dialog box and selecting the device always connects without problems in my own app.
2. Any third parte terminal app/OBD tools also get connected, but they give the option to connect from the paired devices only.

My connect/disconnect button code is given below
B4X:
Sub btnconnect_Click

If btnconnect.Text = "Connect"  Then
AStream.Close
serial1.Disconnect

        Dim LastBTMac As String

        LastBTMac = StateManager.GetSetting ("LastConnectedBTMAC")
        mLogAdd("Opening BT Serial "&LastBTMac)

        If LastBTMac.Length <12 Then
            mLogAdd("Not connected to any BT device yet")
            LastBTMac= "84:73:03:F0:7E:D2"
            Dim PairedDevices As Map
            PairedDevices = serial1.GetPairedDevices
            Dim l As List
            l.Initialize
            For i = 0 To PairedDevices.Size - 1
              l.Add(PairedDevices.GetKeyAt(i))
            Next
            Dim res As Int
            res = InputList(l, "Choose device", -1) 'show list with paired devices
            If res <> DialogResponse.CANCEL Then
              serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address and connect
            'If devicemac <> "" Then
                'BTConnectToDevice
                'serial1.ConnectInsecure(admin, devicemac, 1)
               
            '    ProgressDialogShow("Connecting to Host....")
            '    Else
            '    ToastMessageShow("Please select a Mac Address to connect...", True)
            End If
        Else
        mLogAdd("Using saved Mac "&LastBTMac)
        'serial1.Connect("84:73:03:F0:7E:D2")
        serial1.Connectinsecure(admin, LastBTMac, 1)
        'serial1.Connect(LastBTMac)
        'serial1.Connect2(LastBTMac, "00001101-0000-1000-8000-00805F9B34FB")
        'serial1.Connect3(LastBTMac,1) ' with port number 1
        End If   
           
Else
AStream.Close
serial1.Disconnect
OnConnectedBtnsEnable(False)
End If

Is it that the newer version of Android requires that the user should be prompted to show the device to which connection is being made ?
 
Upvote 0
Top