Zach
Zach

Reputation: 10129

Get Android Device Name

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?

Upvotes: 214

Views: 260869

Answers (16)

Micer
Micer

Reputation: 8979

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.

Upvotes: 56

helloc
helloc

Reputation: 51

  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

Upvotes: 0

Neelam Verma
Neelam Verma

Reputation: 3274

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.

Upvotes: 13

Aishik kirtaniya
Aishik kirtaniya

Reputation: 538

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

Upvotes: 1

Shpand
Shpand

Reputation: 751

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

Upvotes: 7

cnst
cnst

Reputation: 27218

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

Upvotes: -1

hbhakhra
hbhakhra

Reputation: 4236

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

Upvotes: 323

jiong103
jiong103

Reputation: 131

 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.

Upvotes: 1

Yasiru Nayanajith
Yasiru Nayanajith

Reputation: 1737

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.

Upvotes: 2

Bhoomika Chauhan
Bhoomika Chauhan

Reputation: 171

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

Upvotes: 5

Blasanka
Blasanka

Reputation: 22437

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

Upvotes: 15

Alecs
Alecs

Reputation: 2930

Simply use

BluetoothAdapter.getDefaultAdapter().getName()

Upvotes: 2

Roger Huang
Roger Huang

Reputation: 637

@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 ("").

Upvotes: 2

Jared Rummler
Jared Rummler

Reputation: 38121

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)

Upvotes: 29

Ben Jima
Ben Jima

Reputation: 687

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

Upvotes: 56

user3606686
user3606686

Reputation: 285

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

Upvotes: 25

Related Questions