Android设备如何获取广播到外部设备的蓝牙Mac?

时间:2018-09-04 00:58:39

标签: android macos bluetooth bluetooth-lowenergy

所以,这是我的问题,我想知道主要设备如何获取蓝牙mac本身。我尝试使用adapter.getAddress()并反映各种方法,但没有一种能正常工作。 ..请帮我一个忙!

BluetoothAdapter.getDefaultAdapter().startLeScan(callback)

BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(BluetoothDevice deviceTmp, int rssi,
                         byte[] scanRecord) {

        String strDeviceName = deviceTmp.getName();
        if (strDeviceName != null) {
            Log.i(tag, "action_found , " + deviceTmp.getName()  + "mac = "  + deviceTmp.getAddress());

2018年9月4日10:49:15添加新代码

public static String getBtAddressByReflection() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Field field = null;
    try {
        field = BluetoothAdapter.class.getDeclaredField("mService");
        field.setAccessible(true);
        Object bluetoothManagerService = field.get(bluetoothAdapter);
        if (bluetoothManagerService == null) {
            return null;
        }
        Method method = bluetoothManagerService.getClass().getMethod("getAddress");
        if(method != null) {
            Object obj = method.invoke(bluetoothManagerService);
            if(obj != null) {
                return obj.toString();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

0 个答案:

没有答案