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

Eldritch

Member
Licensed 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
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
 
Top