如何获取Android设备名称?我正在使用HTC欲望。当我通过HTC Sync连接它时,软件显示“HTC Smith”的名称。我想通过代码获取这个名称。

这在Android中是如何实现的呢?


当前回答

我通过获取蓝牙名称解决了这个问题,但不是从BluetoothAdapter(需要蓝牙权限)获得的。

代码如下:

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

不需要额外的权限。

其他回答

我通过获取蓝牙名称解决了这个问题,但不是从BluetoothAdapter(需要蓝牙权限)获得的。

代码如下:

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

不需要额外的权限。

在许多流行的设备上,该设备的市场名称是不可用的。例如,在三星Galaxy S6上,Build的值。MODEL可以是“SM-G920F”、“SM-G920I”或“SM-G920W8”。

我创建了一个小型库,用于获取设备的市场(消费者友好)名称。它为超过10,000个设备获得正确的名称,并不断更新。如果你想使用我的图书馆,请点击下面的链接:

Github上的AndroidDeviceNames库


如果你不想使用上面的库,那么这是获得一个消费者友好的设备名称的最佳解决方案:

/** Returns the consumer friendly device name */
public static String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return capitalize(model);
    }
    return capitalize(manufacturer) + " " + model;
}

private static String capitalize(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    char[] arr = str.toCharArray();
    boolean capitalizeNext = true;
    String phrase = "";
    for (char c : arr) {
        if (capitalizeNext && Character.isLetter(c)) {
            phrase += Character.toUpperCase(c);
            capitalizeNext = false;
            continue;
        } else if (Character.isWhitespace(c)) {
            capitalizeNext = true;
        }
        phrase += c;
    }
    return phrase;
}

以我的Verizon HTC One M8为例:

// using method from above
System.out.println(getDeviceName());
// Using https://github.com/jaredrummler/AndroidDeviceNames
System.out.println(DeviceName.getDeviceName());

结果:

HTC6525LVW HTC One (M8)

你可以使用:

来自android doc:

制造商: 字符串制造商 产品/硬件的制造商。 模型: 字符串模型 最终产品的最终用户可见的名称。 设备: 字符串的设备 工业设计的名称。

举个例子:

String deviceName = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
//to add to textview
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(deviceName);

此外,在Build类中有很多你可以使用的属性,比如:

android操作系统。棋盘构建。 android操作系统。布兰德构建。 android操作系统。BOOTLOADER构建。 显示屏构建android操作系统。。 android操作系统。CPU_ABI构建。 android os。构建的广告。 os硬件构建android。 android操作系统。ID构建。

还有其他方法可以在不使用Build类的情况下获取设备名称(通过蓝牙)。

 static String getDeviceName() {
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method getMethod = systemPropertiesClass.getMethod("get", String.class);
            Object object = new Object();
            Object obj = getMethod.invoke(object, "ro.product.device");
            return (obj == null ? "" : (String) obj);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

你可以通过这种方式得到'idol3'。

简单地使用

BluetoothAdapter.getDefaultAdapter () . getname ()