androidbluetoothdevice-name

Android Device Name Without Bluetooth


I wanna get android device name (i.e. the "name" that user can change through settings and that you can find under "Settings > About device > Device name").

Other developers recommend to get this name through bluetooth.

But on my device (Samsung Galaxy S4) this name is only updated when I enable bluetooth, so if I change the name when bluetooth is disabled : the new name won't be displayed until I enable bluetooth.

I'm wondering if there is a way to get the updated device name without enabling bluetooth? I can think of a workaround like :

but I was wondering if a more elegant solution exists.


Solution

  • The device name you are talking about is also known as the bluetooth friendly name and therefore reading it through the bluetooth API is perfectly valid.

    Be aware that if your app is running on a non-bluetooth device : this name don't exists.

    If you read carefully the doc of the bluetooth API you will see for the setName(...) method:

    ... If Bluetooth state is not STATE_ON, this API will return false. After turning on Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated value.

    In other words: this behavior is not particular to your device, but it works as specified.

    Your workaround is AFAIK the only way the get the updated name when name was changed while bluetooth was disabled (and unfortunately it has serious drawbacks : enabling/disabling BT is costly).

    Please also note that you must wait for ACTION_STATE_CHANGED with STATE_ON to get the updated value.