网上搜了很多,在8.0以下能用,但是在8.0以上就不行了,请问有没有大佬懂呢!

解决方案 »

  1.   

    反射
    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;
    }
      

  2.   

    抛什么异常?如果是不能反射系统api的异常,见这个GitHubhttps://github.com/tiann/FreeReflection
      

  3.   


    抛InvocationTargetException这个异常。应该不是反射的事情。
      

  4.   

    我也被这个东西困扰过。给你几点提示吧。
    1.  在 Android8.1  Settings --->  System --> About --> Status --> Bluetooth address  中有显示的,不知道你的版本有没有。
    2.  仔细看了一下代码是这样的
        private void setBtStatus() {
            BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
            if (bluetooth != null && mBtAddress != null) {
                String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
                if (!TextUtils.isEmpty(address)) {
                   // Convert the address to lowercase for consistency with the wifi MAC address.
                    mBtAddress.setSummary(address.toLowerCase());
    Log.e("AHU.MAIN_ACTIVITY","address = " + address.toLowerCase());
                } else {
                    mBtAddress.setSummary(mUnavailable);
                }
            }
        }代码在  \vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\deviceinfo\Status.java你把用这段代码的程序放在system/priv-app/下面是可以获取的,而且获取的跟系统的一样。注意了是/system/pri-app,不是system/app3.  可以用 Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");获取,不过要改一下源码了。
    加一下这个  MOVED_TO_GLOBAL.add(Settings.Global.BLUETOOTH_ADDRESS); 也可以的。你可以尝试一下。
      

  5.   

    你说的是传统蓝牙吧,难道安卓也和IOS学习了  隐藏了蓝牙地址?