3

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.

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Hossain Ehsani
  • 385
  • 2
  • 16

2 Answers2

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);
    }
};
Hossain Ehsani
  • 385
  • 2
  • 16
2

Have you tried this?

BluetoothServerSocket bluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("abc", uuid);          
BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();
BluetoothDevice device = bluetoothSocket.getRemoteDevice();
String deviceName = device.getName();
rbdk
  • 93
  • 1
  • 6
  • looks like the code stock when it get to this line `BluetoothSocket bluetoothSocket = bluetoothServerSocket.accept();` – Hossain Ehsani Mar 11 '18 at 23:35
  • yes, it's waiting for new incoming bluetooth connection request – rbdk Mar 12 '18 at 10:15
  • Have you checked the documentation? https://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices Check out the sections "Querying paired devices" and "Discovering devices". Especially the call to device.getName() – rbdk Mar 12 '18 at 10:49
  • 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 Listers – Hossain Ehsani Mar 12 '18 at 11:40
  • And about the link you mentioned, BluetoothDevice class doesn't provide a function to make a query of paired devices connection state – Hossain Ehsani Mar 12 '18 at 11:58
  • Thank you very much – naima May 05 '20 at 16:22