具有rssi的Android BLE扫描蓝牙设备

时间:2018-08-22 21:36:26

标签: android android-bluetooth rssi android-ble

我一直想找出Android上的BLE。

为了测试,我有3个按钮,分别是startScan,stopScan和showHimself

这两个设备都必须能够扫描并自我显示,这是我使用的:

  • startScan:

    private void scan() {
        if (bluetoothLeScanner != null) {
            bluetoothLeScanner.startScan(scanCallback);
        }
    }
    
    ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.d("BLE", String.format("Rssi value = %s\n", result.getRssi()));
            super.onScanResult(callbackType, result);
        }
    
        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            Log.d("BLE", "onBatchScanResults result size = " + results.size());
            super.onBatchScanResults(results);
        }
    
        @Override
        public void onScanFailed(int errorCode) {
            Log.d("BLE", "onScanFailed");
            super.onScanFailed(errorCode);
        }
    };
    
  • stopScan:

    if (bluetoothLeScanner != null) {
        bluetoothLeScanner.stopScan(scanCallback);
    }
    
  • showHimself:

    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
    startActivity(discoverableIntent);
    

问题是:我无法获得一致的结果。有时我开始扫描,然后在第二台设备上打电话给showHimself,得到即时结果。有时onScanResult根本没有结果...

有任何线索吗?

0 个答案:

没有答案