简体   繁体   English

你如何在android中获得用户定义的“设备名称”?

[英]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如果它有帮助或伤害我在 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/这让我得到了我所需要的...... 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.返回 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经过一番研究,我在 /data/data/com.android.providers.settings/databases/settings.db 中找到了 device_name

so, to get it using shell command所以,使用shell命令获取它

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

using java使用Java

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

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

detailed explanation: http://androidbridge.blogspot.com/2016/09/how-to-get-android-device-name-settings.html详细解释: 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.在我同事测试的设备上,1、2 和 3 似乎是最可靠的。 Here is his full analysis:以下是他的完整分析:

用户设备名称结果表

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

以下对我有用

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

This one worked for me on API 25:这个在 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>"

采用:

String deviceName = android.os.Build.MODEL;

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如果您想要一种稳健的方式在几乎所有 Android 设备(包括电视)上获取用户定义的设备名称并且不需要任何权限,请使用此方法

String userDeviceName = Settings.Global.getString(getContentResolver(), Settings.Global.DEVICE_NAME);
if(userDeviceName == null)
    userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM