208

How to get Android device name? I am using HTC desire. When I connected it via HTC Sync the software is displaying the Name 'HTC Smith' . I would like to fetch this name via code.

How is this possible in Android?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zach
  • 9,989
  • 19
  • 70
  • 107
  • What do you mean by `device name`? A lot of Androids don't have a device name. Some Samsungs, for example, have an editable device name. All Android's, though, do have a `Model number`, which is a string (confusing). – AlikElzin-kilaka Feb 28 '14 at 07:42
  • Related: http://stackoverflow.com/questions/16704597/how-do-you-get-the-user-defined-device-name-in-android – AlikElzin-kilaka Feb 28 '14 at 07:48
  • See updated related post: https://stackoverflow.com/questions/44452942/android-get-device-name-on-android-tv – Richard Le Mesurier Feb 05 '19 at 13:01
  • This question is not the same as *[Get Android Phone Model Programmatically](https://stackoverflow.com/questions/1995439)*: here the OP wants to see the device *given name* which can be changed by the end user, while `Build.MODEL` is hardcoded by manufacturer. – Alex Cohn Mar 03 '21 at 19:12

16 Answers16

319

In order to get Android device name you have to add only a single line of code:

android.os.Build.MODEL;

Found here: getting-android-device-name

Cœur
  • 37,241
  • 25
  • 195
  • 267
hbhakhra
  • 4,206
  • 2
  • 22
  • 36
  • 109
    Just to clarify - `BUILD.MODEL` only gives the device's model number and not the device's name. For example, GT-I9100 instead of Samsung Galaxy S2. If you want the model's name, you'll have to provide your own list and match it against a combination of `android.os.Build.MANUFACTURER` + `android.os.Build.PRODUCT`. – ChuongPham Sep 18 '12 at 15:38
  • 2
    Actually, I find that Build.MODEL is the model name, for instance mine show "Nexus 7" and "Motorola Electrify" on my devices. – Tony Maro Feb 01 '13 at 19:42
  • 8
    @Chuong - Where can I get such a list? – Andrew Bullock Feb 25 '13 at 08:16
  • 1
    @TonyMaro: Not all mobile devices / tabs display similar info as you have with your Nexux/Motorola device. Therefore, it's not reliable to use `android.os.Build.MODEL` and assume all mobile devices / tabs from all manufacturers will display the same info. You need to create your own list if you want to match device model to device name. – ChuongPham Apr 07 '13 at 04:09
  • 1
    @AndrewBullock: You need to create your own list. Look at websites like [Samsung Updates](http://samsung-updates.com/) or [SamMobile](http://www.sammobile.com/firmwares/1/) to get the device names. Then, create an Android resource file containing device models and names and reference that in your codes. – ChuongPham Apr 07 '13 at 04:14
  • Build.MANUFACTURER; – Rakesh Aug 13 '19 at 04:09
  • @ChuongPham `android.os.Build.PRODUCT` returns `beyond1ltexx`, very useful for Samsung S10... – user924 Dec 03 '20 at 18:42
55

You can see answers at here Get Android Phone Model Programmatically

public String getDeviceName() {
   String manufacturer = Build.MANUFACTURER;
   String model = Build.MODEL;
   if (model.startsWith(manufacturer)) {
      return capitalize(model);
   } else {
      return capitalize(manufacturer) + " " + model;
   }
}


private String capitalize(String s) {
    if (s == null || s.length() == 0) {
        return "";
    }
    char first = s.charAt(0);
    if (Character.isUpperCase(first)) {
        return s;
    } else {
        return Character.toUpperCase(first) + s.substring(1);
    }
}
Ben Jima
  • 677
  • 5
  • 10
54

I solved this by getting the Bluetooth name, but not from the BluetoothAdapter (that needs Bluetooth permission).

Here's the code:

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

No extra permissions needed.

UPD: It doesn't work on targetSdk 32 or higher.

Evgen Bodunov
  • 5,438
  • 2
  • 29
  • 43
Micer
  • 8,731
  • 3
  • 79
  • 73
  • 3
    And as a bonus (or penalty, depending on your needs), this reports the user-entered device name if the user has entered a custom name. – jk7 May 30 '18 at 20:29
  • 8
    "bluetooth_name" from Settings.Secure will show the user-defined name only after a device restart, while "device_name" from Settings.Global (API 17+) will show the user-defined name immediately on a Samsung S6 active. The location(s) where the name can be found may vary from one manufacturer or model to another. – jk7 May 30 '18 at 20:35
  • 1
    This works, recently tested in 2019. Thank you for a clean response. – truedat101 May 14 '19 at 06:54
  • @Vlad there's no `Settings.Global` in my answer. – Micer Oct 29 '19 at 12:43
  • Right, sorry. Probably it depends on the device and Global is working on Samsung. – Micer Oct 29 '19 at 13:22
  • 2
    Just to add, it may be abit old. But you can use Settings.Global. You need to define the android provider: example: android.provider.Settings.Global.getString(getContentResolver(), "device_name"); – Dave Hamilton Jan 14 '20 at 11:10
  • ATTENTION! This wont' work if your target sdk ist 32 or higher in future! see https://stackoverflow.com/questions/72271363/what-is-changed-with-bluetooth-in-android-sdk-32 – Aiko West Oct 04 '22 at 12:30
28

On many popular devices the market name of the device is not available. For example, on the Samsung Galaxy S6 the value of Build.MODEL could be "SM-G920F", "SM-G920I", or "SM-G920W8".

I created a small library that gets the market (consumer friendly) name of a device. It gets the correct name for over 10,000 devices and is constantly updated. If you wish to use my library click the link below:

AndroidDeviceNames Library on Github


If you do not want to use the library above, then this is the best solution for getting a consumer friendly device name:

/** 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;
}

Example from my Verizon HTC One M8:

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

Result:

HTC6525LVW

HTC One (M8)

Community
  • 1
  • 1
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • @Jared..this library not violets the google policies right ..because recently apps are getting rejected from play store due third party library violets google policies. – ShriKant A May 14 '21 at 11:02
25

Try it. You can get Device Name through Bluetooth.

Hope it will help you

public String getPhoneName() {  
        BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
        String deviceName = myDevice.getName();     
        return deviceName;
    }
user3606686
  • 285
  • 3
  • 5
  • It didnt work for. Neither on an emulator, nor on my android phone. i tried after switching on BlueTooth on the phone too. – Amna Ali Jun 19 '14 at 17:59
  • 1
    What result are you getting instead? Keep in mind this approach also requires additional bluetooth permissions in the manifest. – Andrew T. Jun 19 '14 at 20:44
  • 2
    Works for me on Samsung S3 and a Nexus 5 (in an app that already had BT permissions). You should just also check for myDevice == null, so you wont run into problems on devices that don't have Bluetooth. In that case, I instead use: deviceName = Build.MANUFACTURER + " " + Build.MODEL; – Wookie Jan 07 '15 at 18:19
  • This needs extra permissions just for a name! – Themelis Nov 23 '18 at 09:52
  • This won't work on an emulator (at least not the ones distributed with the Android SDK) since they don't provide an emulated Bluetooth adapter. – Nathan Osman Dec 13 '20 at 05:19
  • Since I have the same but on Xamarin, I will not provide code but it's worth mentioning that since Android 12 you need to also ask for a runtime permission for this (before there was only an install time permission) which is `BLUETOOTH_CONNECT`. – iBobb Sep 07 '22 at 15:15
14

You can use:

From android doc:

MANUFACTURER:

String MANUFACTURER

The manufacturer of the product/hardware.

MODEL:

String MODEL

The end-user-visible name for the end product.

DEVICE:

String DEVICE

The name of the industrial design.

As a example:

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

Furthermore, their is lot of attribute in Build class that you can use, like:

  • os.android.Build.BOARD
  • os.android.Build.BRAND
  • os.android.Build.BOOTLOADER
  • os.android.Build.DISPLAY
  • os.android.Build.CPU_ABI
  • os.android.Build.PRODUCT
  • os.android.Build.HARDWARE
  • os.android.Build.ID

Also their is other ways you can get device name without using Build class(through the bluetooth).

Blasanka
  • 21,001
  • 12
  • 102
  • 104
13

Following works for me.

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

I don't think so its duplicate answer. The above ppl are talking about Setting Secure, for me setting secure is giving null, if i use setting global it works. Thanks anyways.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
7

universal way to get user defined DeviceName working for almost all devices and not requiring any permissions

String userDeviceName = Settings.Global.getString(getContentResolver(), Settings.Global.DEVICE_NAME);
if(userDeviceName == null)
    userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");
Shpand
  • 661
  • 9
  • 14
  • 3
    Note that after API 31 (S), using `Settings.Secure.getString` to obtain "bluetooth_name" will result in an exception. I tried it myself and it's available in the publicly [available code here](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/provider/Settings.java) – iBobb Sep 07 '22 at 15:52
  • @iBobb is there any other way how to get the user specified device name in API 32 and above? – Martin Růžek Jan 06 '23 at 05:58
  • I am still targeting API 31 and using this solution and if it returns nothing I display `DeviceInfo.Name` (from Xamarin.Essentials). Haven't gotten around to refactoring to support API 32 as Google is not forcing us to do so yet. Depending on how important this is for you, you can just use `DeviceInfo.Name` or let the user know in order to show this information correctly you need to request bluetooth permissions from them and take the information from some bluetooth API that Xamarin exposes. – iBobb Jan 06 '23 at 13:22
5

Try this code. You get android device name.

public static String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return model;
    }
    return manufacturer + " " + model;
}
2

@hbhakhra's answer will do.

If you're interested in detailed explanation, it is useful to look into Android Compatibility Definition Document. (3.2.2 Build Parameters)

You will find:

DEVICE - A value chosen by the device implementer containing the development name or code name identifying the configuration of the hardware features and industrial design of the device. The value of this field MUST be encodable as 7-bit ASCII and match the regular expression “^[a-zA-Z0-9_-]+$”.

MODEL - A value chosen by the device implementer containing the name of the device as known to the end user. This SHOULD be the same name under which the device is marketed and sold to end users. There are no requirements on the specific format of this field, except that it MUST NOT be null or the empty string ("").

MANUFACTURER - The trade name of the Original Equipment Manufacturer (OEM) of the product. There are no requirements on the specific format of this field, except that it MUST NOT be null or the empty string ("").

Roger Huang
  • 597
  • 1
  • 8
  • 15
2

UPDATE You could retrieve the device from buildprop easitly.

static String GetDeviceName() {
    Process p;
    String propvalue = "";
    try {
        p = new ProcessBuilder("/system/bin/getprop", "ro.semc.product.name").redirectErrorStream(true).start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            propvalue = line;
        }
        p.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return propvalue;
}

But keep in mind, this doesn't work on some devices.

Yasiru Nayanajith
  • 1,647
  • 17
  • 20
1

Simply use

BluetoothAdapter.getDefaultAdapter().getName()

Alecs
  • 2,900
  • 1
  • 22
  • 25
1
 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 "";
        }
    }

enter image description here

you can get 'idol3' by this way.

jiong103
  • 111
  • 7
1

Tried These libraries but nothing worked according to my expectation and was giving absolutely wrong names.

So i created this library myself using the same data. Here is the link

AndroidPhoneNamesFinder

To use this library just add this for implementation

implementation 'com.github.aishik212:AndroidPhoneNamesFinder:v1.0.2'

Then use the following kotlin code

DeviceNameFinder.getPhoneValues(this, object : DeviceDetailsListener
{  
    override fun details(doQuery: DeviceDetailsModel?) 
    {  
        super.details(doQuery)  
        Log.d(TAG, "details: "+doQuery?.calculatedName)  
    }  
})

These are the values you will get from DeviceDetailsModel

val brand: String? #This is the brandName of the Device  
val commonName: String?, #This is the most common Name of the Device  
val codeName: String?,  #This is the codeName of the Device
val modelName: String?,  #This is the another uncommon Name of the Device
val calculatedName: String?, #This is the special name that this library tries to create from the above data.

Example of Android Emulator -

brand=Google 
commonName=Google Android Emulator 
codeName=generic_x86_arm 
modelName=sdk_gphone_x86 
calculatedName=Google Android Emulator
Aishik kirtaniya
  • 478
  • 5
  • 19
0

Within the GNU/Linux environment of Android, e.g., via Termux UNIX shell on a non-root device, it's available through the /system/bin/getprop command, whereas the meaning of each value is explained in Build.java within Android (also at googlesource):

% /system/bin/getprop | fgrep ro.product | tail -3
[ro.product.manufacturer]: [Google]
[ro.product.model]: [Pixel 2 XL]
[ro.product.name]: [taimen]
% /system/bin/getprop ro.product.model
Pixel 2 XL
% /system/bin/getprop ro.product.model | tr ' ' _
Pixel_2_XL

For example, it can be set as the pane_title for the status-right within tmux like so:

tmux select-pane -T "$(getprop ro.product.model)"

cnst
  • 25,870
  • 6
  • 90
  • 122
0
  1. Gets an Android system property, or lists them all
adb shell getprop >prop_list.txt
  1. Find your device name in prop_list.txt to get the prop name

e.g. my device name is ro.oppo.market.name

  1. Get oppo.market Operator
adb shell getprop ro.oppo.market.name
  1. My case on windows as follows
D:\winusr\adbl

λ *adb shell getprop ro.oppo.market.name*

OPPO R17

helloc
  • 31
  • 7
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 07 '21 at 12:37
  • The idea behind this question is to do it programmatically, not using the `adb` command – Simon Forsberg Jun 22 '22 at 12:06