Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the Android bluetooth device name

I know it's possible to get the local device name as described in the solution to this question Display Android Bluetooth Device Name

What I'm interested in knowing is, can I change the local buetooth name (the one other devices see when I'm in discovery mode) programaticlly. I know you can change it by hand, but I'm writing and app and I want to be able to change the name (add a simple flag) so other devices with the same application can scan and instantly know if the phone is also running the app.

tl;dr: How can I change the bluetooth device name on android?

like image 524
Daisetsu Avatar asked Dec 04 '11 18:12

Daisetsu


People also ask

Can I rename Bluetooth devices on Android?

Look for the Bluetooth device you want to rename and tap the settings cog next to it. Once you're on the edit page, tap Rename down at the bottom. Type in your new name and tap Rename.

Can I change Bluetooth device name?

Renaming A Bluetooth Device On Android First, swipe down from the top of your screen and tap on the gear icon to open settings. Click on “Connected Devices” from the list, and you'll see Bluetooth listed there. Tap on the device's name to change it or open the three-dot menu. Click “Rename” at the bottom or “Save”.

How do you change your Bluetooth name on Android head unit?

If you select “Connection Preferences,” you'll now see “Bluetooth.” Your device name will be listed in the Bluetooth settings. On some devices, you can simply tap the name to change it… …others will require opening the three-dot menu.


3 Answers

Yes you can change your device name using setName(String name) of BluetoothAdapter type.Following is the sample code:

    private BluetoothAdapter bluetoothAdapter = null;
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    void ChangeDeviceName(){
                    Log.i(LOG, "localdevicename : "+bluetoothAdapter.getName()+" localdeviceAddress : "+bluetoothAdapter.getAddress());
                    bluetoothAdapter.setName("NewDeviceName");
                    Log.i(LOG, "localdevicename : "+bluetoothAdapter.getName()+" localdeviceAddress : "+bluetoothAdapter.getAddress());
                }
like image 152
Maverick Avatar answered Oct 19 '22 16:10

Maverick


Thanks for the original answer, here are a few things I found when implementing that might help someone else out.

1) BT has to be enabled for setName() to work.

2) It takes time for BT to Enable. ie. you Can't just call enable() then setName()

3) It takes time for the name to "sink in". ie. you can't call getName() right after setName() and expect the new name.

So, here is a snippet of code I came up with to use a runnable to get the job done in the background. It is also time bound to 10seconds, so it won't run forever if there is a problem.

Finally, this is part of our power on check, and we normally leave BT disabled (due to battery). So, I turn BT back off after, you may not want to do that.

// BT Rename
//
final String sNewName = "Syntactics";
final BluetoothAdapter myBTAdapter = BluetoothAdapter.getDefaultAdapter();
final long lTimeToGiveUp_ms = System.currentTimeMillis() + 10000;
if (myBTAdapter != null)
{
    String sOldName = myBTAdapter.getName();
    if (sOldName.equalsIgnoreCase(sNewName) == false)
    {
        final Handler myTimerHandler = new Handler();
        myBTAdapter.enable();
        myTimerHandler.postDelayed(
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        if (myBTAdapter.isEnabled())
                        {
                            myBTAdapter.setName(sNewName);
                            if (sNewName.equalsIgnoreCase(myBTAdapter.getName()))
                            {
                                Log.i(TAG_MODULE, "Updated BT Name to " + myBTAdapter.getName());
                                myBTAdapter.disable();
                            }
                        }
                        if ((sNewName.equalsIgnoreCase(myBTAdapter.getName()) == false) && (System.currentTimeMillis() < lTimeToGiveUp_ms))
                        {
                            myTimerHandler.postDelayed(this, 500);
                            if (myBTAdapter.isEnabled())
                                Log.i(TAG_MODULE, "Update BT Name: waiting on BT Enable");
                            else
                                Log.i(TAG_MODULE, "Update BT Name: waiting for Name (" + sNewName + ") to set in");
                        }
                    }
                } , 500);
    }
}
like image 27
syntactics Avatar answered Oct 19 '22 17:10

syntactics


To change the bluetooth name properly you need to take care of following things:

1) You need following permissions: android.permission.BLUETOOTH android.permission.BLUETOOTH_ADMIN

2) Check the bluetooth state from adapter as you can only change the name of bluetooth is turned on.

val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.state == BluetoothAdapter.STATE_ON){
   bluetoothAdapter.setName("NewDeviceName");
}

3) If the bluetooth is not turned on then you can turn it on with the following command:

bluetoothAdapter.enable()

4) Last thing, please don't use static timers to wait for bluetooth state changes instead the proper way is that you can register for android.bluetooth.adapter.action.STATE_CHANGED broadcast and useBluetoothAdapter.EXTRA_STATE to get the new state of bluetooth whenever it is changed.

Note: Not all devices behave the same when it comes to bluetooth and changing the name due to caching and hw address, so never expect same outcome from all devices.

like image 33
Farhan Masood Avatar answered Oct 19 '22 18:10

Farhan Masood