Get Bluetooth local mac address in Marshmallow

前端 未结 8 512
半阙折子戏
半阙折子戏 2020-11-29 06:23

Pre Marshmallow my app would obtain it\'s device MAC address via BluetoothAdapter.getDefaultAdapter().getAddress().

Now with Marshmallow Android is retu

8条回答
  •  温柔的废话
    2020-11-29 06:52

    Getting the MAC address via reflection can look like this:

    private static String getBtAddressViaReflection() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService");
        if (bluetoothManagerService == null) {
            Log.w(TAG, "couldn't find bluetoothManagerService");
            return null;
        }
        Object address = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs();
        if (address != null && address instanceof String) {
            Log.w(TAG, "using reflection to get the BT MAC address: " + address);
            return (String) address;
        } else {
            return null;
        }
    }
    

    using a reflection library (net.vidageek:mirror) but you'll get the idea.

提交回复
热议问题