首页 > 解决方案 > Android 蓝牙 - 为蓝牙发现的设备的 UUID 获取 NULL

问题描述

我想在不配对的情况下在两个蓝牙设备之间共享数据,并且在两部手机中都将安装我的应用程序,所以我使用了这种方法(https://arxiv.org/ftp/arxiv/papers/1507/1507.00650.pdf)蓝牙设备使用 UUID 编号发布服务列表。

这是如何注册服务=====>

 String uuid = "f2989d52-b3d6-4205-b34d-04f35ea264e5";
 BluetoothAdapter  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 BluetoothServerSocket btSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("MYAPP", UUID.fromString(uuid))

回调的意图===>

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter1);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_UUID);
    registerReceiver(mReceiver, filter2);

之后我打电话

    bluetoothAdapter.startDiscovery();  // to start discovery of devices

处理意图的代码==>

    private  final BroadcastReceiver mReceiver = new
        BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                Log.e("SUNIL","receiver");
                String action = intent.getAction();
                Log.e("SUNIL","receiver action"+action);
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Boolean uuid1 = device.fetchUuidsWithSdp();
                    mDeviceList.add(device);
                    Log.e("SUNIL","device uuid result"+uuid1);
                    Log.e("SUNIL","device"+device.getAddress()+device.getUuids()+device.getBondState());
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    // discovery has finished, give a call to fetchUuidsWithSdp on first device in list.
                    if (!mDeviceList.isEmpty()) {
                        BluetoothDevice device = mDeviceList.remove(0);
                        boolean result = device.fetchUuidsWithSdp();
                        Log.e("SUNIL", "device uuid result223" + result+"==uuid finished"+device.getUuids());
                    }
                }
                else if (BluetoothDevice.ACTION_UUID.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Log.e("SUNIL","device.getUuids()"+device.getUuids());
                    Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
                    if(uuids != null) {
                        for (Parcelable ep : uuids) {
                            if (uuids != null) {
                                String uuid = ep.toString();
                                Log.e("SUNIL", "uuid===>" + uuid+"===="+device.getName());
                                             **getting uuid null here**
                            }

                        }
                    }
                }
            }
        };

请帮助我,我是这个蓝牙 API 的新手, 我已经用 redmi 6A 和 redmi 6 PRO 设备验证了

标签: androidbluetoothandroid-bluetooth

解决方案


推荐阅读