How to get name of the connected Bluetooth device on android
Asked Answered
B

2

4

I'm trying to get the name of the device that is connected to the Android phone running android Oreo.

I was searching for an answer for the past two days, and none of them worked. suggestions mostly returning ioexception-read-failed-socket-might-closed-bluetooth error

The question is, is there any way to make Query that returns the connected Bluetooth device?

These are the links and suggestion which not working:

I can get information about the device that is previously paired and trying to make a connection or a device trying to pair to the device. what I want is the name or the connection state of the currently paired and connected device.

Beforetime answered 11/3, 2018 at 21:56 Comment(0)
B
10

here is the answer :

String name;
String address;
String threadName;
public void checkConnected()
{

       BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET);
}

private BluetoothProfile.ServiceListener serviceListener = new BluetoothProfile.ServiceListener()
{
    @Override
    public void onServiceDisconnected(int profile)
    {

    }

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy)
    {

        for (BluetoothDevice device : proxy.getConnectedDevices())
        {
            name = device.getName();
            address = device.getAddress();
            threadName = Thread.currentThread().getName();
            Toast.makeText(MainActivity.this, name+" " + address+ threadName,  Toast.LENGTH_SHORT).show();
            txtName.setText(name + " " + address);
            Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
                    + BluetoothProfile.STATE_CONNECTED + ")");
        }

        BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
    }
};
Beforetime answered 15/3, 2018 at 20:10 Comment(0)
P
2

Have you tried this?

BluetoothServerSocket bluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("abc", uuid);          
BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();
BluetoothDevice device = bluetoothSocket.getRemoteDevice();
String deviceName = device.getName();
Pentaprism answered 11/3, 2018 at 22:48 Comment(6)
looks like the code stock when it get to this line BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();Beforetime
yes, it's waiting for new incoming bluetooth connection requestPentaprism
Have you checked the documentation? developer.android.com/guide/topics/connectivity/… Check out the sections "Querying paired devices" and "Discovering devices". Especially the call to device.getName()Pentaprism
Thanks for your reply. But what I want is to get the connection status of the currently paired and connected Bluetooth device . No the ones that are trying to connect. For that situation I preferred to use broadcast ListersBeforetime
And about the link you mentioned, BluetoothDevice class doesn't provide a function to make a query of paired devices connection stateBeforetime
Thank you very muchElegist

© 2022 - 2024 — McMap. All rights reserved.