Android Question Getting the name of the connected Bluetooth device

techgreyeye

Member
Licensed User
Longtime User
I'm updating one of my apps to target SDK 28 (it previously didn't have a target) and the code which gets the name of the connected device no longer works on my Android 9 device but still works on Android 5 and 6.

errors on connectedAddress = ref.GetField("mAddress"):
Private Sub serial1_Connected (Success As Boolean)
    If Success Then
        Dim devices As Map
        devices = serial1.GetPairedDevices
        Dim i As Int
        Dim ref As Reflector
        Dim connectedAddress As String
        Dim workingsocket As Object

        ref.Target = serial1
        workingsocket = ref.GetField("workingSocket")
        ref.Target = workingsocket
        connectedAddress = ref.GetField("mAddress")
       
        For i = 0 To devices.Size - 1
            Dim address As String
            address = devices.GetValueAt(i)
            If address = connectedAddress Then
                connectedDevice = devices.GetKeyAt(i)
            End If
        Next

        BTStatus = "Connected to " & connectedDevice
        BTLog("Connected to " & connectedDevice & " at " & DateTime.Time(DateTime.Now), False)
       
        BTConnected = True
        StartAStream(serial1.InputStream, serial1.OutputStream)
    Else
        retries = retries + 1
        BTStatus = "Connection failed, retrying " & retries
        'BTLog("Error: " & LastException.Message, False)
        If retries >= 10 Then
            serial1.StopListening
            BTLog("Bluetooth listener failed. Please exit the app and try again.", False)
            retries = 0
        End If
    End If
    UpdateUI
End Sub

The error is :

java.lang.NoSuchFieldException: No field mAddress in class Landroid/bluetooth/BluetoothSocket; (declaration of 'android.bluetooth.BluetoothSocket' appears in /system/framework/framework.jar)


Anybody have any idea how I get the address, or maybe there is a better way of getting the device name now that I don't know?

Thanks in advance :)
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub GetDeviceName (MacAddress As String) As String
   Dim adapter As JavaObject   
   adapter = adapter.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethodJO("getDefaultAdapter", Null)
   Dim BluetoothDevice As JavaObject = adapter.RunMethod("getRemoteDevice", Array(MacAddress))
   Return BluetoothDevice.RunMethod("getName", Null)
End Sub
 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
Try this:
B4X:
Sub GetDeviceName (MacAddress As String) As String
   Dim adapter As JavaObject  
   adapter = adapter.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethodJO("getDefaultAdapter", Null)
   Dim BluetoothDevice As JavaObject = adapter.RunMethod("getRemoteDevice", Array(MacAddress))
   Return BluetoothDevice.RunMethod("getName", Null)
End Sub

Thanks, Erel.

From within serial1_connect, which address do I use to call GetDeviceName?

serial1.address always returns 02:00:00:00:00:00 as the MAC address.
 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
Are you listening for incoming connections? If not then you should use the mac address you passed to the Connect method.

Wow, 18 months since I asked this. Sorry Erel, I missed your follow-up question, I am now revisiting this.

Yes, I'm listening for incoming connections.
 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
I've solved this now...

Code:
Private Sub serial1_Connected (Success As Boolean)
    If Success Then
        Dim ref As Reflector
        Dim workingsocket As Object
        Try
            ref.Target = serial1
            workingsocket = ref.GetField("workingSocket")
            ref.Target = workingsocket
            ref.target = ref.RunMethod("getRemoteDevice")
            connectedDevice = ref.RunMethod("getName")
        Catch
            Log(LastException)
        End Try   
        If connectedDevice = "null" Or connectedDevice = "" Then
            BTStatus = "Connected"
            BTLog("Connected at " & DateTime.Time(DateTime.Now), False)
        Else
            BTStatus = "Connected to " & connectedDevice
            BTLog("Connected to " & connectedDevice & " at " & DateTime.Time(DateTime.Now), False)
        End If       
        BTConnected = True
        StartAStream(serial1.InputStream, serial1.OutputStream)
    End If
 
Upvote 0
Top