4

i want to know how to get the name of the connected bluetooth device in android here is the code

NetworkInfo bluetooth = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
if(bluetooth.isConnected())
                {
                    Toast.makeText(myprofile3Context,"bluetooth is connected", Toast.LENGTH_SHORT).show();
                } 

here i check if bluetooth device is connected or not. if bluetooth is connected i want know how to get the name of connected device.

faisal iqbal
  • 724
  • 1
  • 8
  • 20
  • this will help you https://cmanios.wordpress.com/2012/05/09/bluetooth-adapter-device-name-and-mac-address-in-android/ – Sagar Nayak Jun 29 '16 at 10:18

2 Answers2

0

try this

public String getLocalBluetoothName(){
    if(mBluetoothAdapter == null){
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }
    String name = mBluetoothAdapter.getName();
    if(name == null){
        System.out.println("Name is null!");
        name = mBluetoothAdapter.getAddress();
    }
    return name;
}
Morse
  • 8,258
  • 7
  • 39
  • 64
Mark
  • 89
  • 8
0
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                          //you can get name by device.getName()

        } else if (BluetoothAdapter.ACL_DISCONNECTED
                .equals(action)) {

        }
    }
 };

Use extends BroadcastReceiver and add permissions in manifest

kiran kumar
  • 1,402
  • 17
  • 34