Android Question Bluetooth-connection-problem "java.io.IOException"

D

Deleted member 103

Guest
Hello,

with the first Bluetooth connection I store the MAC address in the settings, so that with the next connection, the connection goes faster.
In the Google Report but always this error comes, and I do not understand why, or I find in my code no errors.

Here is my code:
B4X:
Sub Admin_DiscoveryFinished
    CallSub3(Main,"ShowProgressDialog","",False)
    If foundDevices.Size = 0 Then
        ToastMessageShow(Starter.language.value("strBluetoothNoFound"), True)
    Else
        Dim Macfound As NameAndMac
        Macfound.Initialize
        Macfound.Name = "" 'Sicherstellen das der Name Leer ist.
        
        Dim lst As List
        lst.Initialize
        For i = 0 To foundDevices.Size - 1
            Dim nm As NameAndMac
            nm = foundDevices.Get(i)
            lst.Add(nm.Name)
            
            If Starter.manager.GetString("BtMacAddress") = nm.Mac Then
                Macfound = nm
                Exit
            End If
        Next
        If Macfound.Name <> "" Then
            ConnectToKnownDevice(Macfound)
        Else
            'If lst.Size > 0 Then CallSub2(Main,"ChooseDevice", lst)
            CallSub2(Main,"ChooseDevice", lst)
        End If
    End If
End Sub

Sub ConnectToKnownDevice(BtMac As NameAndMac)
    connectedDevice = BtMac
    CallSub3(Main,"ShowProgressDialog",Starter.language.value("strBluetoothConnect") & " " & BtMac.Name & " (" & BtMac.Mac & ")",True)
    serial1.Connect(BtMac.Mac)
End Sub

and here the crash report:
20. Okt. 17:59 in der App-Version 97
Samsung Galaxy S5 Neo (s5neolte), Android 6.0
Bericht 1 von 1
java.io.IOException
:
at android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord (BluetoothDevice.java:1886)
at anywheresoftware.b4a.objects.Serial.Connect2 (Serial.java:147)
at anywheresoftware.b4a.objects.Serial.Connect (Serial.java:117)
at fg.cronomillemiglia.clsbluetooth._vvvvvvvvvvvvvvvvvvvvvvvvvvv5 (clsbluetooth.java:436)
at fg.cronomillemiglia.clsbluetooth._admin_discoveryfinished (clsbluetooth.java:149)
at java.lang.reflect.Method.invoke (Native Method)
at anywheresoftware.b4a.BA.raiseEvent2 (BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent (BA.java:171)
at anywheresoftware.b4a.objects.Serial$BluetoothAdmin$1$1.run (Serial.java:341)
at android.os.Handler.handleCallback (Handler.java:739)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:158)
at android.app.ActivityThread.main (ActivityThread.java:7230)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

b41.PNG


b42.PNG


It would be nice if someone might find the mistake.
 
D

Deleted member 103

Guest
Check that Bluetooth is enabled before you start the discovery process.

That's my code, it should work, right?
B4X:
Main:
Private Sub mnuBtButton_Click  
    bluetooth.ConnectBtButton
End Sub

BT-Class:
Public Sub ConnectBtButton
    If HasFeature("android.hardware.bluetooth") Then
        Try
            If admin.IsEnabled = False Then
                If admin.Enable = False Then
                    ToastMessageShow(Starter.language.value("strBluetoothErrorEnabling"), True)
                Else
                    ToastMessageShow(Starter.language.value("strBluetoothEnabling"), False)
                    'the StateChanged event will be soon raised
                End If
            Else           
                Admin_StateChanged(admin.STATE_ON, 0)
            End If
        Catch
            Log("ConnectBtButton-ERROR:" & LastException)
        End Try
    Else
        ToastMessageShow(Starter.language.value("strBluetoothNotPresent"), True)
    End If
End Sub

Sub Admin_StateChanged (NewState As Int, OldState As Int)
    If NewState = admin.STATE_ON Then
        ToastMessageShow(Starter.language.value("strBluetoothIsEnabled"), False)
        Searchdevice
    End If
End Sub

Sub Searchdevice
    tmpList.Clear
    foundDevices.Clear
    If admin.StartDiscovery    = False Then
        ToastMessageShow(Starter.language.value("strBluetoothErrorProcess"), True)
    Else
        CallSub3(Main,"ShowProgressDialog",Starter.language.value("strBluetoothSearch"),True)
    End If
End Sub
 
Upvote 0
D

Deleted member 103

Guest
After the implementation of crashlytics now these errors come.

I also changed the previous sub "ConnectToKnownDevice", unfortunately without success.
B4X:
Sub ConnectToKnownDevice(BtMac As NameAndMac)
    Try
        connectedDevice = BtMac
        CallSub3(Main,"ShowProgressDialog",Starter.language.value("strBluetoothConnect") & " " & BtMac.Name & " (" & BtMac.Mac & ")",True)
        serial1.Connect(BtMac.Mac)       
    Catch
        Log("ConnectToKnownDevice: " & LastException.Message)
    End Try
End Sub
upload_2018-10-26_7-43-49.png


Fatal Exception: java.io.IOException
at android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(BluetoothDevice.java:2294)
at anywheresoftware.b4a.objects.Serial.Connect2(Serial.java:147)
at anywheresoftware.b4a.objects.Serial.Connect(Serial.java:117)
at fg.MasterOfRegolarity.clsbluetooth._connecttoknowndevice(clsbluetooth.java:425)
at fg.MasterOfRegolarity.clsbluetooth._admin_discoveryfinished(clsbluetooth.java:146)
at java.lang.reflect.Method.invoke(Method.java)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.Serial$BluetoothAdmin$1$1.run(Serial.java:341)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 
Upvote 0
Top