android 连接mac地址格式,【Android】获取系统的一些mac地址

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

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;

}

BLE mac

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

WIFI mac

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

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

String wifiMac = wifiInfo.getMacAddress();

WIFI DIRECT mac

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

public String getWFDMacAddress(){

try {

List 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

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值