39

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

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
Chris Whittle
  • 868
  • 1
  • 8
  • 14

8 Answers8

31

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

Chris Whittle
  • 868
  • 1
  • 8
  • 14
  • 1
    You can accept your own answer. As it would have been a stretch to guess that you were talking about Bluetooth from your question, I added a tag and mention to the title. – Chris Stratton May 24 '13 at 02:08
  • 2
    In the settings it says nothing about Blue tooth on either of the devices I was working with. I went by trial and error until I found it. – Chris Whittle May 24 '13 at 12:10
  • 1
    Alright, I rolled back my edit. But your solution reveals the problem to be one which has no general solution - on many devices what you are trying to do is simply not possible at all, as there's no means for the user to name the device. – Chris Stratton May 24 '13 at 20:05
  • 1
    please take note. as this answer works, you still have to restart your device if you update your device name on your device settings for you to get the updated name. – ralphgabb May 20 '15 at 04:43
  • 1
    Since BT is mandatory to ship the phone with Google Apps, we can expect all devices except a few like Amazon Kindle to have BT.http://android.stackexchange.com/questions/24464/any-android-phones-that-dont-support-bluetooth/48233#48233 – garnet Jan 19 '16 at 12:41
  • Attempt to invoke virtual method 'java.lang.String android.bluetooth.BluetoothAdapter.getName()' on a null object reference –  Dec 20 '20 at 16:19
15

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

Dan J
  • 25,433
  • 17
  • 100
  • 173
  • Unfortunately, none of these methods work on Pixel 2 XL emulator (api 28). They all return null! – t-gao May 14 '19 at 09:47
14

Following works for me

String deviceName = Settings.Global.getString(<Context>.getContentResolver(), Settings.Global.DEVICE_NAME);
Felix
  • 6,885
  • 1
  • 29
  • 54
Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
13
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

kakopappa
  • 5,023
  • 5
  • 54
  • 73
  • 1
    this was returning a null :( – Lavekush Agrawal Nov 27 '17 at 08:04
  • 3
    This returns what the user named his device in settings: `settings get global device_name` and in java `Settings.Global.getString(getContentResolver(), "device_name");` – F0r3v3r-A-N00b Oct 16 '18 at 03:49
  • That shell command returns "null" on Android P (Pixel) despite having set the value in the Settings section of the phone. – Richard Le Mesurier Feb 05 '19 at 12:38
  • The `Settings.System` method now returns "null" as well. – Richard Le Mesurier Feb 05 '19 at 12:44
  • thanks for providing this answer interpreting the question as being about the device's user-assigned name, not the device's model name. as others have mentioned, none of the methods here seem to be working with my kindle (android 5.1.1). They return variously `null`, `Fire`, or `KFDOWI`. hm. [android docs](https://developer.android.com/reference/android/provider/Settings.Global.html#DEVICE_NAME) say that device_name was added in API level 25, which apparently means Fire OS 6, so i guess this makes sense. sort of. why is `settings get global device_name` returning anything at all then ? – orion elenzil May 15 '19 at 22:25
6

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>"
TheHebrewHammer
  • 3,018
  • 3
  • 28
  • 45
  • In my version of ADB (1.0.36) in macOS, the settings command does not have a `list` option, just `get` and `put`. – jk7 Feb 15 '17 at 19:15
  • 1
    Correction: The `settings` shell command on my cheap LG tracfone does not have the `list` option. – jk7 Feb 15 '17 at 19:37
  • Just to add another variant, my Samsung J2 has the device name in global settings. `settings list global | grep ""` – Sinc Mar 25 '20 at 20:33
4

Use:

String deviceName = android.os.Build.MODEL;
ArturSkowronski
  • 1,722
  • 13
  • 17
2

No of the above mentioned methods work for all devices. If you want a robust way to get user defined device name on almost all Android devices (including TV) and not requiring any permissions use this

String userDeviceName = Settings.Global.getString(getContentResolver(), Settings.Global.DEVICE_NAME);
if(userDeviceName == null)
    userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");
Shpand
  • 661
  • 9
  • 14
0

The following line worked for me:

String userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");

However, I had to ensure that -

  • The app is running on devices with API level 25 or higher
  • Bluetooth was turned on at least once after updating the device name. Otherwise, the getString() will only return the old device name.
ganjaam
  • 1,030
  • 3
  • 17
  • 29