17

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?

3 Answers 3

28

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());
                }
Sign up to request clarification or add additional context in comments.

2 Comments

This does seem to work, but there does also appear to be some caching issues (ie. connected devices always had old name). If anyone comes across this issue go into the phones bluetooth settings and view the devices, this seemed to clear the cache for me
yes this seem like not working
9

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);
    }
}

3 Comments

Something to add, which may be useful for some future readers is found in the documentation, stating: Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, although many remote devices can only display the first 40 characters, and some may be limited to just 20. Additionally it requires the permission "android.permission.BLUETOOTH_ADMIN"
This is not working for Samsung devices. The moment I open Bluetooth settings, it gets changed back to the device name found under "About Phone".
For the above example the log is generate but actual name is not update, have you found solution for this ?
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.