0

I'm developing an app that uses zeroconf (bonjour) to discover devices - so I need to give each android device some kind of name (not just a bunch of numbers and letters, but something meaningful like "Alex's Device"). In iOS it can be easily done - is this possible in android?

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
Alex1987
  • 9,397
  • 14
  • 70
  • 92

4 Answers4

1

There can be many accounts linked to the device. You can use the AccountManager to get them. For example, emails on google accounts:

AccountManager am = AccountManager.get(this);

Account[] ac = am.getAccountsByType("com.google");

for (Account account : ac) {
  Log.d ("Account", ac.name);
}

Alternatively, you can use android.os.Build.MODEL or similar.

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • the account name isn't unique. i can have two devices linked to the same google account. – Jeffrey Blattman Jun 01 '11 at 21:32
  • So you can have two iphones with the same user name. I am trying to give the answer that more closely targets the question. – Aleadam Jun 01 '11 at 21:36
  • that's not what he asked. "so I need to give each android device". EACH ... DEVICE, not EACH ... USER. – Jeffrey Blattman Jun 03 '11 at 01:10
  • @farble what do you mean "that's not what he asked"? If the OP accepted the answer there must be a reason for it... I try to read the **context** of the question, not just the words. – Aleadam Jun 03 '11 at 02:53
  • let me try again, to quote the OP: "I need to give each android device some kind of name". notice that he said he needs to give "each device", not the user. account names are user names, and they are not per device as the OP requested. got it now? – Jeffrey Blattman Jun 03 '11 at 17:01
  • @farble you forgot to quote *something meaningful like "Alex's Device"* . If the OP didn't accept the answer, I would be willing to evaluate your objection. But he did, thus I was correct. It is always more important here to answer with regards to **the OP's intention, rather than the literal wording**. If you have an issue with how the question is formulated, fell free to edit it. Let me know you did it and I'll approve the changes. – Aleadam Jun 03 '11 at 17:59
1

Assuming your application requires a device to have a network connection, you can try using the MAC address of the device, which should be globally unique. This can be obtained with the WifiInfo class:

WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String macAddress = info.getMacAddress();

You'll also need to set the ACCESS_WIFI_STATE permission in your manifest.

Asynkronos
  • 195
  • 5
0

you can get the IMEI of the Android Phone , it's Unique ,

you can Get it by using the method getDeviceId() of the Class TelephonyManager

NOTE : you will need to add a permission in your manifest : READ_PHONE_STATE

Houcine
  • 24,001
  • 13
  • 56
  • 83
  • i said Android Phone , i did'nt say tablets, and i think that on tablets ,there is an Identifient , i didn't work or use it , just "i think " :) – Houcine Jun 01 '11 at 22:04
0

For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched.

I personally find this acceptable but not ideal. No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique.

The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally.

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105