Skip to content

Commit

Permalink
[*] fixes issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
nisrulz committed Jul 12, 2016
1 parent ae9f922 commit 3b0c988
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

/**
* The type Easy bluetooth mod.
Expand All @@ -45,8 +46,16 @@ public EasyBluetoothMod(Context context) {
String result = null;
if (context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
== PackageManager.PERMISSION_GRANTED) {
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
result = bta.getAddress();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Hardware ID are restricted in Android 6+
// https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id
// Getting bluetooth mac via reflection for devices with Android 6+
result = android.provider.Settings.Secure.getString(context.getContentResolver(),
"bluetooth_address");
} else {
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
result = bta != null ? bta.getAddress() : null;
}
}
return CheckValidityUtil.checkValidData(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
Expand Down Expand Up @@ -231,42 +233,45 @@ public String getIPv6Address() {
*/
@SuppressWarnings("MissingPermission") public String getWifiMAC() {
String result = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_WIFI_STATE)
== PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Hardware ID are restricted in Android 6+
// https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id
Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
e.printStackTrace();
}
while (interfaces != null && interfaces.hasMoreElements()) {
NetworkInterface iF = interfaces.nextElement();
NetworkInterface networkInterface = interfaces.nextElement();

byte[] addr = new byte[0];
try {
addr = iF.getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
}
if (addr == null || addr.length == 0) {
continue;
}
byte[] addr = new byte[0];
try {
addr = networkInterface.getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
}
if (addr == null || addr.length == 0) {
continue;
}

StringBuilder buf = new StringBuilder();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
String mac = buf.toString();
result = iF.getName().equals("wlan0") ? mac : result;
}
} else {
if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_WIFI_STATE)
== PackageManager.PERMISSION_GRANTED) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
result = wm.getConnectionInfo().getMacAddress();
StringBuilder buf = new StringBuilder();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
String mac = buf.toString();
String wifiInterfaceName = "wlan0";
result = wifiInterfaceName.equals(networkInterface.getName()) ? mac : result;
}
} else {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
result = wm.getConnectionInfo().getMacAddress();
}
}
return CheckValidityUtil.checkValidData(result);
}
Expand Down

0 comments on commit 3b0c988

Please sign in to comment.