Get Bluetooth local mac address in Marshmallow

前端 未结 8 513
半阙折子戏
半阙折子戏 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:57

    Worked great

     private String getBluetoothMacAddress() {
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            String bluetoothMacAddress = "";
            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 | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {
    
            }
            return bluetoothMacAddress;
        }
    

提交回复
热议问题