Android Question Bluetooth Connection in code

Declan

Well-Known Member
Licensed User
Longtime User
I am using the Bluetooth Example and I am able to search for BT devices and establish a connection with the InputList.
I would like to be able to connect with the code so that if the connectedDevice.Name is = "Remote_BTName" the device must connect and these is no required user intervention.
How would I have to edit this code:
B4X:
Sub Admin_DiscoveryFinished
    ProgressDialogHide
    If foundDevices.Size = 0 Then
        ToastMessageShow("No device found.", True)
    Else
        Dim l As List
        l.Initialize
        For i = 0 To foundDevices.Size - 1
            Dim nm As NameAndMac
            nm = foundDevices.Get(i)
            l.Add(nm.Name)
        Next
        Dim res As Int
        res = InputList(l, "Choose device to connect", -1)
        If res <> DialogResponse.CANCEL Then
            connectedDevice = foundDevices.Get(res)
            ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
            serial1.Connect(connectedDevice.Mac)
        End If
    End If
End Sub
 

Declan

Well-Known Member
Licensed User
Longtime User
OK, sorted:
I edited the above code to:
B4X:
Sub Admin_DiscoveryFinished
   
    Dim MyBTdevice As String = "HC-05"
   
    ProgressDialogHide
    If foundDevices.Size = 0 Then
        ToastMessageShow("No device found.", True)
    Else
        Dim l As List
        l.Initialize
        For i = 0 To foundDevices.Size - 1
            Dim nm As NameAndMac
            nm = foundDevices.Get(i)
            l.Add(nm.Name)
        Next
       
        MyBTdevice = nm.name       
        Msgbox("Got: " & MyBTdevice, "Devices foundDevices")
       
       
        If nm.Name = MyBTdevice Then
            ProgressDialogShow("Connecting to Channel: " & MyBTdevice )
            serial1.Connect(nm.Mac)
        End If
    End If
End Sub
and works great
 
Upvote 0
Top