Android BluetoothDevice createBond 权限错误

Android BluetoothDevice createBond permissions error

我有一个项目

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

在其中我使用(简化)调用扫描 BT 设备:

final BluetoothLeScanner scanner = BluetoothAdapter.getDefaultAdapter()
        .getBluetoothLeScanner()
ArrayList<ScanFilter> scanFilters = ArrayList<>();
scanFilters.add(new ScanFilter.Builder()
        .setServiceUuid(new ParcelUuid(MY_SERVICE_UUID))
        .build());
ScanSettings scanSettings = new ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .build();
ScanCallback scanCallback = new ScanCallback() {
    @Override
    void onScanFailed(int errorCode) {}
    @Override
    void onScanResult(int callbackType, ScanResult result) {
        result.getDevice().createBond();
        scanner.stopScan(this);
    }
    @Override
    public void onBatchScanResults(List<ScanResult> results) {}
};
scanner.startScan(scanFilters, scanSettings, scanCallback);

要点是我使用我的服务扫描设备,现在只需绑定到我看到的第一个设备。我所有的特征和描述符都使用 read/write_encrypted 权限(我不知道这是否重要)。我最终看到的是两台设备上的配对屏幕,但后来当我使用 connectGatt 方法时,我失败并出现以下错误:

E/bt_btif: bta_gattc_cache_load: can't open GATT cache file /data/misc/bluetooth/gatt_cache_76fe0b7bf8dd for reading, error: No such file or directory
E/bt_att: gatt_disc_cmpl_cback() - Unable to register for service changed indication

另一方面,如果我打开 BT 设置并从那里绑定设备,一切都很好! connectGatt 有效,我可以 read/write 随心所欲地获取数据。

createBond() 我做错了什么?

似乎调用 BluetoothDevice.createBond() 使用 BluetoothDevice.TRANSPORT_AUTO 来创建绑定。这不适用于所有 Android 平台并且很脆弱。相反,使用用于扫描设备的特定传输进行调用就可以了。

device.createBond(BluetoothDevice.TRANSPORT_LE)