Alex1987
Alex1987

Reputation: 9457

Find unique name of device?

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?

Upvotes: 0

Views: 2565

Answers (4)

Kevin Parker
Kevin Parker

Reputation: 17206

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

Upvotes: 0

Asynkronos
Asynkronos

Reputation: 195

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.

Upvotes: 1

Aleadam
Aleadam

Reputation: 40391

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.

Upvotes: 1

Houcine
Houcine

Reputation: 24181

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

Upvotes: 0

Related Questions