Android Question Bluetooth

Terradrones

Active Member
Licensed User
Hi All

I need help again please.

I have declared the Bluetooth Functions as a Class.

Firstly I get 2 listings of the same Bluetooth device in my list where I display all the Bluetooth Devices.

Next, I cannot connect and it gives me this error message:

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

Here is my Bluetooth class:

B4X:
[/
Sub Class_Globals
    'Bluetooth
    Public Serial As Serial
    Public Admin As BluetoothAdmin
    Public AStream As AsyncStreams
    Public FoundDevices As List
    Type NameAndMac (Name As String, Mac As String)
    Dim nm As NameAndMac
    Public Data As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Try
        Admin.Initialize("admin")
        Serial.Initialize("serial")
        If AStream.IsInitialized Then AStream.Close
        Admin.Enable
        FoundDevices.Initialize
        If Admin.IsEnabled = False Then
            If Admin.Enable = False Then
                ToastMessageShow("Error Enabling Bluetooth...", True)
            End If
        End If
    Catch
        Log(LastException)
    End Try
    Try
        AStream.Initialize(Serial.InputStream, Serial.OutputStream, "AStream")
    Catch
        Log(LastException)
    End Try
End Sub

Public Sub ConnectTo (Device As NameAndMac)
    Try
        Serial.Connect(Device.Mac)
    Catch
        Log(LastException)
    End Try
End Sub

Public Sub Disconnect
    If AStream.IsInitialized Then AStream.Close
    Serial.Disconnect
End Sub

Public Sub Disable
    Admin.Disable
End Sub

Public Sub Enable
    Admin.Enable
End Sub

Public Sub SearchForDevices As Boolean
    Log("Start")
    Return Admin.StartDiscovery
End Sub

Public Sub Admin_DiscoveryFinished
    CallSub(Parameter, "DiscoverFinished")
End Sub

Public Sub Admin_DeviceFound (Name As String, MacAddress As String)
    Log(Name)
    nm.Name = Name
    nm.Mac = MacAddress
    FoundDevices.Add(nm)
End Sub

Public Sub StopReading(A As String)
    Serial.StopListening
End Sub

Public Sub AStream_NewData (Buffer() As Byte)
    Data=BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    NewData
End Sub

Public Sub NewData
    Data=Data
End Sub

Public Sub SendDataToDevice(A As String)
    AStream.Write(A.GetBytes("utf8"))
End Sub

Public Sub CloseConnection
    
End Sub

Public Sub ReadDeviceDataAsync
    
End Sub

Public Sub ClearOutputData
    
End Sub

Public Sub ClearBuffer
    
End Sub
]

This is the code that calls the class:

[CODE=b4x][/

#Region ********************************************************* Bluetooth ************************************************

Sub FindDevices_Click
    HideParaPanels
    Panel6.Visible=True
    FindDevice
End Sub

Sub ExitBT_Click
    HideParaPanels
    Para4.Visible=True
End Sub

Sub FindAgain_Click
    FindDevice
End Sub

Sub FindDevice
    CLV.Clear
    BT.FoundDevices.Initialize
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No Permission...", False)
        Return
    End If
    Dim success As Boolean = BT.SearchForDevices
    If success = False Then
        ToastMessageShow("Error Starting Discovery Process.", True)
    Else
        ProgressDialogShow2("Searching For Devices...", False)
    End If
End Sub

Sub DiscoverFinished
    Try
        ProgressDialogHide
        If BT.FoundDevices.Size = 0 Then
            ToastMessageShow("No Device Found.", True)
        Else
            For Each nm As NameAndMac In BT.FoundDevices
                CLV.AddTextItem($"${nm.Name}"$, nm)
            Next
        End If
    Catch
        Log(LastException)
    End Try
End Sub

Sub CLV_ItemClick (Index As Int, Value As Object)
    Dim nm As NameAndMac = Value
    ToastMessageShow($"Trying to connect to: ${nm.Name}"$, True)
    BT.ConnectTo(nm)    ''Mac)        
    BT.Serial.Connect(nm.Mac)
    Wait For Serial_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        AfterSuccessfulConnection
        If TS.Checked=True Then
            'Total Station
            CGlobals.DF(6)=1
            CGlobals.DF(7)=nm.Mac
        Else If GPS.Checked=True Then
            'GPS
            If Base.Checked=True Then
                CGlobals.DF(34)=1
                CGlobals.DF(35)=nm.Mac
            Else If Rover.Checked=True Then
                CGlobals.DF(53)=1
                CGlobals.DF(54)=nm.Mac
            End If
        End If
    End If
    
End Sub

Sub Serial_Connected (Success As Boolean)
    If Success Then
        AfterSuccessfulConnection
    End If
End Sub

Sub AfterSuccessfulConnection
'    If BT.AStream.IsInitialized Then BT.AStream.Close
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
    ''AStream.InitializePrefix(CGlobals.serial.InputStream, False, CGlobals.serial.OutputStream, "astream")
'    ConnectionState = True
End Sub

#End Region
]
 
Top