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

Eldritch

Member
Licensed User
Longtime User
Hi

I am trying to use Erels Example for using an bluetooth device HC-05 with an arduino Mega. My problem is that the example code won't connect to the Bluetooth device. I am using the example as it is but has change the name of the device to linvor. I have downloaded Bluetooth terminal from google play and the device functioned as it should with this so the problem appears to be in the example.

/Added. Oh and if I step though the code 9 out of 10 times i does connect.

the line I change is line 27 in starter
Changed:
    If Name = "linvor" Then

the log shows

Device found: Gear Fit2 (F2AD)
Device found: Gear Fit2 (F2AD)
Device found: linvor
Trying to connect...
Device found: linvor
Trying to connect...
(IOException) java.io.IOException: read failed, socket might closed or timeout, read ret: -1
(IOException) java.io.IOException: read failed, socket might closed or timeout, read ret: -1

/Allan
 
Last edited:

Eldritch

Member
Licensed User
Longtime User
So I solved the problem. Moved admin.StartDiscovery to the Service_Create and buffered the mac and commented the connect in devicefound. I really don't think connect belongs there. But what do I know Added the Connect to the sub Connect and now it works every time. Now I don't know if buffering the correct mac is the Android way.

What I changed:
Sub Process_Globals
    Private serial As Serial
    Private admin As BluetoothAdmin
    Private ast As AsyncStreamsText
    Public connected As Boolean
    Public connecting As Boolean
    Public ConnectMac As String
End Sub

Sub Service_Create
    serial.Initialize("serial")
    admin.Initialize("admin")
    admin.StartDiscovery
End Sub

Public Sub Connect
    'admin.StartDiscovery
    connecting = True
    serial.Connect(ConnectMac)
    CallSub(Main, "SetState")
End Sub

Private Sub admin_DeviceFound (Name As String, MacAddress As String)
    Log($"Device found: ${Name}"$)
    If Name = "linvor" Then
        Log("Trying to connect...")
        admin.CancelDiscovery
        ConnectMac=MacAddress
        'serial.Connect(MacAddress)

    End If
End Sub
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
Your fix works, but I have digged a little bit deeper and found that the problem is caused by admin_DeviceFound called twice for each bluetooth device. That is also the reason why the exception is shown twice. So when the app tries to connect to the device, admin_DeviceFound is called again and it immediately tries to connect once more while the former connection is already in progress. As a result, both connections fail.

So my proposal is to introduce a flag in order to stop further connection attempts once the device is found:

B4X:
Sub Process_Globals
    Private serial As Serial
    Private admin As BluetoothAdmin
    Private ast As AsyncStreamsText
    Public connected As Boolean
    Public connecting As Boolean
    Public deviceName As String = "linvor"
    Public deviceFound As Boolean
End Sub

Sub Service_Create
    serial.Initialize("serial")
    admin.Initialize("admin")
End Sub

Public Sub Connect
    Log("Connect")
    connecting = True
    deviceFound = False
    admin.StartDiscovery
    CallSub(Main, "SetState")
End Sub

Private Sub admin_DeviceFound (Name As String, MacAddress As String)
    Log($"Device found: ${Name}"$)
    If Name = deviceName Then
        If deviceFound Then Return
        deviceFound = True
        admin.CancelDiscovery
        serial.Connect(MacAddress)
        Log("Trying to connect to " & MacAddress )
    End If
End Sub

I have no idea why every device shows up twice. Maybe somebody else can shed some light on this?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…