【Android】获取系统的一些mac地址

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【Android】获取系统的一些mac地址脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

  在开发过程中,有时我们需要获取系统的一些硬件信息。在这里,介绍一些硬件信息的获取方法,其中包括BT mac, BLE mac,wifi mac, WIFI DIRECT mac.

  1. BT mac

  BT mac是指设备的蓝牙地址。获取方法如下:

BluetoothManager btManager = (BluetoothManager) getContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = btManager.getAdapter();
String btMac;
//在6.0版本以后,获取硬件ID变得更加严格,所以通过设备的地址映射得到mac地址
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  btMac = android.PRovider.Settings.Secure.getString(getContext.getContentResolver(), "bluetooth_address");
}else {
  btMac = btAdapter.getAddress();
}

private String getBluetoothMacAddress() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String bluetoothMacAddress = "";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
        try {
            Field mServiceField = bluetoothAdapter.getClass().getDeclareDField("mService");
            mServiceField.setAccessible(true);

            Object btManagerService = mServiceField.get(bluetoothAdapter);

            if (btManagerService != null) {
                bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
            }
        } catch (NoSuchFieldException e) {

        } catch (NoSuchMethodException e) {

        } catch (IllegalAccessException e) {

        } catch (InvocationTargetException e) {

        }
    } else {
        bluetoothMacAddress = bluetoothAdapter.getAddress();
    }
    return bluetoothMacAddress;
}
  1. BLE mac

  BLE mac是指设备的蓝牙低能耗模块的地址,一般来说,其值和BT mac相等

  1. WIFI mac

  WIFI mac是指设备进行WiFi连接时的地址,获取方法如下:

@H_126_111@WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
String wifiMac = wifiInfo.getMacAddress();
  1. WIFI DIRECT mac

  WIFI DIRECT mac是指设备进行wifi直连时自身的地址,获取方法如下(由于android自身的api目前还没有直接获取的方法,所以使用java的api进行获取):

public String getWFDMacAddress(){
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ntwInterface : interfaces) {

            if (ntwInterface.getName().equalsIgnoreCase("p2p0")) {
                byte[] byteMac = ntwInterface.getHardwareAddress();
                if (byteMac==null){
                    return null;
                }
                StringBuilder strBuilder = new StringBuilder();
                for (int i=0; i<byteMac.length; i++) {
                    strBuilder.append(String.format("%02X:", byteMac[i]));
                }

                if (strBuilder.length()>0){
                    strBuilder.deleteCharAt(strBuilder.length()-1);
                }

                return strBuilder.toString();
            }

        }
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}

参考:
How do you retrieve the WiFi Direct MAC address?--stackoverflow

脚本宝典总结

以上是脚本宝典为你收集整理的【Android】获取系统的一些mac地址全部内容,希望文章能够帮你解决【Android】获取系统的一些mac地址所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。