Revision: 2446
http://sourceforge.net/p/swingme/code/2446
Author: yuranet
Date: 2021-05-12 11:45:17 +0000 (Wed, 12 May 2021)
Log Message:
-----------
better way of getting UniqueHardwareId
Modified Paths:
--------------
AndroidME/src_Android/net/yura/android/AndroidMeApp.java
Modified: AndroidME/src_Android/net/yura/android/AndroidMeApp.java
===================================================================
--- AndroidME/src_Android/net/yura/android/AndroidMeApp.java 2021-05-12 11:07:55 UTC (rev 2445)
+++ AndroidME/src_Android/net/yura/android/AndroidMeApp.java 2021-05-12 11:45:17 UTC (rev 2446)
@@ -366,60 +366,64 @@
return res == PackageManager.PERMISSION_GRANTED;
}
- private String getUniqueHardwareId(){
+ private String getUniqueHardwareId() {
String uniqueId = "";
- try
- {
- TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- if (telephonyManager != null){
- uniqueId = telephonyManager.getDeviceId();
- }
+ try {
+ TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- // if we can't retrieve the IMEI we can try and get the serial hardware number.
- // this is only on android gingerbread (v2.3) and up. according to google
- // devices WITHOUT telephony are required to report a unique device ID here;
- // http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
- if((isValidId(uniqueId)) && android.os.Build.VERSION.SDK_INT >= 9 ){
- //try getting from api 9 and up.
- try {
- // SERIAL is only available in API 9
- Class clazz = android.os.Build.class;
- uniqueId = (String) clazz.getField("SERIAL").get(clazz);
- } catch (Throwable e) {
- //#debug debug
- System.out.println(e.getStackTrace());
- }
+ if (telephonyManager != null) {
+ uniqueId = telephonyManager.getDeviceId();
+ }
+ }
+ catch (Throwable e) {
+ //#debug debug
+ System.out.println(e.getStackTrace());
+ }
- }
+ try {
+ // if we can't retrieve the IMEI we can try and get the serial hardware number.
+ // this is only on android gingerbread (v2.3) and up. according to google
+ // devices WITHOUT telephony are required to report a unique device ID here;
+ // http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
+ if(isInvalidId(uniqueId) && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
+ //try getting from api 9 and up. SERIAL is only available in API 9
+ uniqueId = android.os.Build.SERIAL;
+ }
+ }
+ catch (Throwable e) {
+ //#debug debug
+ System.out.println(e.getStackTrace());
+ }
- //fall back to WIFI. Android documentation says:
- //It may be possible to retrieve a Mac address from a device's WiFi or Bluetooth hardware. We do not recommend using this as a unique identifier.
- //To start with, not all devices have WiFi. Also, if the WiFi is not turned on, the hardware may not report the Mac address.
- if(isValidId(uniqueId)) {
- WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
- if(wifiMan != null){
- WifiInfo wifiInf = wifiMan.getConnectionInfo();
- if(wifiInf != null){
- uniqueId = wifiInf.getMacAddress();
- }
- }
- }
+ try {
+ //fall back to WIFI. Android documentation says:
+ //It may be possible to retrieve a Mac address from a device's WiFi or Bluetooth hardware. We do not recommend using this as a unique identifier.
+ //To start with, not all devices have WiFi. Also, if the WiFi is not turned on, the hardware may not report the Mac address.
+ if(isInvalidId(uniqueId)) {
+ WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
+ if(wifiMan != null){
+ WifiInfo wifiInf = wifiMan.getConnectionInfo();
+ if(wifiInf != null){
+ uniqueId = wifiInf.getMacAddress();
+ }
+ }
+ }
+ }
+ catch (Throwable e) {
+ //#debug debug
+ System.out.println(e.getStackTrace());
+ }
- } catch (Throwable e) {
- //#debug debug
- System.out.println(e.getStackTrace());
- }
//#debug debug
- System.out.println("Unique hardwar id rerieved and is: " + uniqueId);
+ System.out.println("Unique hardware id retrieved and is: " + uniqueId);
return uniqueId;
}
- private boolean isValidId(String uniqueId) {
- return uniqueId == null || uniqueId.length() == 0 || uniqueId.equals("000000000000000") || uniqueId.equals("unknown") || uniqueId.equals("0");
- }
+ private boolean isInvalidId(String uniqueId) {
+ return uniqueId == null || uniqueId.length() == 0 || uniqueId.equals("000000000000000") || uniqueId.equals("unknown") || uniqueId.equals("0");
+ }
-
private void setFileSystemProperties() {
final String URL_FILE_ROOT = "file:///";
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|