1

我有一些代码可以监听蓝牙发现事件,列出发现的设备及其 UUID。

现在,这段代码在 Android 2.3 到 4.1 上运行良好。但是,在 4.3(没有尝试 4.2)上,突然之间,我再也看不到 UUID 了。唯一的区别似乎只是完全缺乏任何 UUID 信息。

这是广播接收器代码:

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        Log.d(TAG, "BTReceiver got action " + action);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            BluetoothDevice device =
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            Parcelable[] extraUIIDs = device.getUuids();

            if (extraUIIDs == null) {
                device.fetchUuidsWithSdp();
            }
        }

        if (BluetoothDevice.ACTION_UUID.equals(action)) {

            BluetoothDevice device =
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            Parcelable[] pUUIDs = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

            if (pUUIDs == null) {
                pUUIDs = device.getUuids();
                Log.d(TAG, "No UUIDs in intent, trying from device : "+pUUIDs);
            }

        }

    }

在较旧的 Android 中,我会在设备对象中看到 UUID,或者与 UUID 操作一起接收。在 4.3 中,我仍然收到 UUID 操作,但 UUID 数组null在意图和设备对象中。

同样,唯一的区别是 Android 版本(和设备),但只要它们低于 4.2,它就可以在我能拿到的所有 android 上始终如一地工作。

4

0 回答 0