Reputation: 898
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
Reputation: 1286
The following line worked for me:
String userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");
However, I had to ensure that -
getString()
will only return the old device name.Upvotes: 0
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
Reputation: 3274
Following works for me
String deviceName = Settings.Global.getString(<Context>.getContentResolver(), Settings.Global.DEVICE_NAME);
Upvotes: 17
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:
Settings.System.getString(getContentResolver(), “bluetooth_name”);
(docs)Settings.Secure.getString(getContentResolver(), “bluetooth_name”);
(docs)BluetoothAdapter.getDefaultAdapter().getName();
(docs)Settings.System.getString(getContentResolver(), “device_name”);
(docs)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:
Source: https://medium.com/@pribble88/how-to-get-an-android-device-nickname-4b4700b3068c
Upvotes: 17
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
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
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