Android Question How can I tell which blue tooth devices are connected with the tablet

davepamn

Active Member
Licensed User
Longtime User
I need to know which blue tooth devices are connected with the tablet when my app starts.

After I connect the first time then close my app and reopen it, I get "No blue tooth Devices Found" because they are not discovered.

The Zebra GX420d display is showing the "Blue Tooth is connected"

B4X:
Sub btnBlueToothConnect_Click

  admin.Initialize("Admin")


  If admin.StartDiscovery= False Then
      ToastMessageShow("Error starting discovery process.", True)
  Else
       ProgressDialogShow("Searching for devices...")
  End If

  Dim Tablet As PhoneWakeState

  Tablet.KeepAlive(True)

End Sub

Sub Admin_DeviceFound (Name As String, MacAddress As String)

'Log(Name & ":" & MacAddress)

Dim nm AsNameAndMac

If nm.IsInitialized=False Then

  nm.Initialize

End If


nm.Name = Name

nm.Mac = MacAddress

foundDevices.Add(nm)

ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))

End Sub


Sub Admin_DiscoveryFinished

Try

ProgressDialogHide

If foundDevices.Size = 0 Then

   ToastMessageShow("No blue tooth devices found.", True)

Else

Dim l As List

l.Initialize

For i = 0 To foundDevices.Size - 1

Dim nm AsNameAndMac

If nm.IsInitialized=False Then

   nm.Initialize

End If

nm = foundDevices.Get(i)

l.Add(nm.Name)

Next

Dim res As Int

Dim connectedDevice As NameAndMac

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 & ")")

ProgressDialogHide

serial1.Connect(connectedDevice.Mac)

If AStream.IsInitialized = False Then

AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")

End If

End If

End If

Catch

oError.ShowLastException("Error : TestAdmin_DiscoveryFinished")

EndTry

End Sub


Sub AStream_NewData (Buffer() As Byte)

Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))

End Sub

Sub AStream_Error

ToastMessageShow("Connection is broken.", True)

End Sub


Sub AStream_Terminated

AStream_Error

End Sub
 
Top