Java: Get name and serial number of mobile connected to the computer

1

Dear, first: This is not a question of Android development.

I am doing automation in java, and would like to know if there is any way to strings the device name and serial number when connecting via USB to the computer. Currently I do this by manually entering the "My Computer > Selecting the right cell" properties and then copying this information.

Is there any way to do this via Java?

Right now,

Thank you!

    
asked by anonymous 04.08.2016 / 19:35

1 answer

-1

Speak Luís,

The serial is simple, just call the build, example:

String deviceSerial;

try {
   deviceSerial = (String) Build.class.getField("SERIAL").get(null);
 } catch (IllegalAccessException e) {
      e.printStackTrace();
 } catch (NoSuchFieldException e) {
      e.printStackTrace();
 }

Log.d("serial", deviceSerial);

And to get the name, it's a bit trickier, for example:

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;

    StringBuilder phrase = new StringBuilder();
    for (char c : arr) {
        if (capitalizeNext && Character.isLetter(c)) {
            phrase.append(Character.toUpperCase(c));
            capitalizeNext = false;
            continue;
        } else if (Character.isWhitespace(c)) {
            capitalizeNext = true;
        }
        phrase.append(c);
    }

    return phrase.toString();
}

Then to call the result, you do:

String deviceName = getDeviceName();

Hugs.

    
04.08.2016 / 19:47