Chris Whittle
Chris Whittle

Reputation: 898

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

Upvotes: 41

Views: 47251

Answers (8)

ganjaam
ganjaam

Reputation: 1286

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.

Upvotes: 0

Shpand
Shpand

Reputation: 751

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

Upvotes: 3

Neelam Verma
Neelam Verma

Reputation: 3274

Following works for me

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

Upvotes: 17

Dan J
Dan J

Reputation: 25673

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

Upvotes: 17

TheHebrewHammer
TheHebrewHammer

Reputation: 3138

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>"

Upvotes: 7

kakopappa
kakopappa

Reputation: 5085

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

Upvotes: 13

Chris Whittle
Chris Whittle

Reputation: 898

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

Upvotes: 32

ArturSkowronski
ArturSkowronski

Reputation: 1792

Use:

String deviceName = android.os.Build.MODEL;

Upvotes: 4

Related Questions