How do you get the user defined "Device Name" in android?

I am trying to get the user defined device name that is set in settings. I've tried several options and so far nothing.

If it helps or hurts I am needing it in a BroadcastReceiver

Thanks


This got me what I was needed... http://cmanios.wordpress.com/2012/05/09/bluetooth-adapter-device-name-and-mac-address-in-android/

BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
    String deviceName = myDevice.getName();

Make sure you add

<uses-permission android:name="android.permission.BLUETOOTH"/>

to your manifest


BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
String deviceName = myDevice.getName();

returns Galaxy S5.

String deviceName = android.os.Build.MODEL;
returns SM-G900H.

After some research I found the device_name in /data/data/com.android.providers.settings/databases/settings.db

so, to get it using shell command

shell@k3g:/ $ settings get system device_name
settings get system device_name
Milky Way

using java

String deviceName = Settings.System.getString(getContentResolver(), "device_name");
returns Milky Way

POC github project https://github.com/kakopappa/getdevcicename

detailed explanation: http://androidbridge.blogspot.com/2016/09/how-to-get-android-device-name-settings.html


In summary, there is no single consistent way to retrieve the user-customized device name.

However, there are numerous utilities that you can use that will work on different models and manufacturers:

  1. Settings.System.getString(getContentResolver(), “bluetooth_name”); (docs)
  2. Settings.Secure.getString(getContentResolver(), “bluetooth_name”); (docs)
  3. BluetoothAdapter.getDefaultAdapter().getName(); (docs)
  4. Settings.System.getString(getContentResolver(), “device_name”); (docs)
  5. Settings.Secure.getString(getContentResolver(), “lock_screen_owner_info”); (docs)

1, 2 and 3 appear to be the most reliable on the devices my colleague tested. Here is his full analysis:

user device name table of results

Source: https://medium.com/@pribble88/how-to-get-an-android-device-nickname-4b4700b3068c


Following works for me

String deviceName = Settings.Global.getString(<Context>.getContentResolver(), Settings.Global.DEVICE_NAME);

This one worked for me on API 25:

Settings.Secure.getString(getContentResolver(), "bluetooth_name");

Here's how to find it... (just replace the name of your device)

$ adb shell
$ settings list system | grep "<device_name>"
$ settings list secure | grep "<device_name>"