无法从 BLE nordic thingy:52 检索 RSSI 值

Unable to retrieve RSSI values from BLE nordic thingy:52

我通过 Android thingylib (github) 的 Nordic 库设法连接到 thingy52 并在回调侦听器 ThingyListener 中接收有关设备(例如 onGravityVectorChangedEventonAccelerometerValueChangedEventonGyroscopeValueChangedEvent 等...)。

我不能做的(而且我找不到怎么做)是如何获取有关与设备连接的信息,确切地说是 RSSI 值。

使用 BluetoothLeScannerCompat,在 onBatchScanResults 扫描回调中,我检测到 thingy52 蓝牙设备及其 RSSI 值,但是如果我设置高扫描频率,例如 .setReportDelay(10), 它几乎总是检测不到设备。

我是如何进行扫描的:

private void startBLEScan() {
        // set scan
        ScanSettings scanSettings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .setReportDelay(10) // set frequency
                .setUseHardwareBatchingIfSupported(false)
                .build();

        // filter scan by uuid
        List<no.nordicsemi.android.support.v18.scanner.ScanFilter> filters = new ArrayList<>();
        filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(ThingyUtils.THINGY_BASE_UUID)).build());

        // start scan, this will trigger the scanCallback
        BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
        scanner.startScan(filters, scanSettings, scanCallback);
    }

    private final ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onBatchScanResults(@NonNull List<ScanResult> results) {
            super.onBatchScanResults(results);

            for(ScanResult result : results) {
                result.getRssi(); // read RSSI values
            }
        }
    };

我需要以10Hz100Hz的频率获取nordic thingy52的RSSI值,我无法从ThingyListener事件中获取。

我该怎么做? 提前谢谢你。

我的 ThingyListener 示例:

  private ThingyListener mThingyListener = new ThingyListener() {


        @Override
        public void onGravityVectorChangedEvent(BluetoothDevice bluetoothDevice, float x, float y, float z) {
            // Here I got the data from device... but no RSSI value

    };

最后我是这样解决的:

我不使用 ScannerCompact,但 BluetoothGATT:

当我 link 北欧 ThingyLibSdkManager 我也 link 它变成 GATT:

ThingyListenerHelper.registerThingyListener(getApplicationContext(), mThingyListener, device);
mBluetoothGatt = device.connectGatt(getApplicationContext(), true, gattCallback);

这样,在 mThingylistener 中,我现在可以请求 rssi:

     @Override
        public void onGyroscopeValueChangedEvent(BluetoothDevice bluetoothDevice, float x, float y, float z) {

            collectedData.put("onGyroscopeValueChangedEvent_x", x + "");
            collectedData.put("onGyroscopeValueChangedEvent_y", y + "");
            collectedData.put("onGyroscopeValueChangedEvent_z", z + "");

            // request rssi from gatt
            mBluetoothGatt.readRemoteRssi();
        }

现在,以异步方式,您将收到内部回调 mBluetoothGatt:

   @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
// do some stuff
}