简体   繁体   English

如何知道android中BLE扫描的状态?

[英]How to know the status of BLE scan in android?

I am working on a mobile app where I am doing the following to get the data from my sensors.我正在开发一个移动应用程序,我正在执行以下操作以从我的传感器获取数据。

  1. Running a BLE scan in the foreground service.在前台服务中运行 BLE 扫描。
  2. Detect bluetooth on/off states and automatically starting and stopping the BLE scan.检测蓝牙开/关状态并自动启动和停止 BLE 扫描。
  3. Restart BLE scan every 3mins (I read somewhere that BLE scans automatically stops when run more than 15mins).每 3 分钟重新启动一次 BLE 扫描(我在某处读到,当运行超过 15 分钟时,BLE 扫描会自动停止)。 so just to be safe I am restarting it once every 3 mins.所以为了安全起见,我每 3 分钟重新启动一次。

I start/restart the scan using the following statement我使用以下语句开始/重新启动扫描

scanner.startScan(scanFilters,scanSettings,callback);

I stop the scan using the following statement我使用以下语句停止扫描

scanner.stopScan(callback);

In scanFilters , I set the name of my device ID and In scanSettings I set the scan mode to LOW_LATENCYscanFilters中,我设置了设备 ID 的名称,在scanSettings中,我将扫描模式设置为LOW_LATENCY

The app works fine most of the time.该应用程序大部分时间都运行良好。 But sometimes I get some complaints from my customers that eventhough bluetooth and location are on, there is no data update.但有时我会收到一些客户的抱怨,即使蓝牙和定位功能打开,也没有数据更新。 The sensors are working fine.传感器工作正常。 When I start a blueooth LE scan how to ensure that the scan is actually running or not?.当我开始蓝牙 LE 扫描时,如何确保扫描实际上正在运行? I there anywhere that I can get a status of the scan running or no.我可以在任何地方获得扫描运行或未运行的状态。

Since you are saying that this happens only for some customers there might be another source of your problem.既然您说这种情况只发生在某些客户身上,那么您的问题可能还有其他来源。

BLE needs the ACCESS_FINE_LOCATION permission to work for devices running an Android version lower than 8.0. BLE 需要ACCESS_FINE_LOCATION权限才能在运行低于 8.0 的 Android 版本的设备上工作。 Higher version can use the Device Companion Pairing .更高版本可以使用Device Companion Pairing You might have added the Permission to your apps manifest file like this:您可能已将权限添加到您的应用程序清单文件中,如下所示:

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

But it is safer to also check for this permission at runtime and request it if not granted.但是在运行时检查此权限并在未授予时请求它会更安全。 Have a look at this page for more informations about how to request permissios from the user at runtime.查看 页面以获取有关如何在运行时向用户请求权限的更多信息。

You need little modification for this.你需要为此做一些修改。

  • From the below code, we find mScanning is the marker.从下面的代码中,我们发现mScanning是标记。
  • When start and stop the scan, we are changing mScanning value.当开始和停止扫描时,我们正在改变mScanning值。
  • Now we declare a static get method to check whether mScanning is true or false.现在我们声明一个 static get 方法来检查mScanning是 true 还是 false。
public static boolean isScanRunning(){
   return mScanning;
}
  • Based on the value you can understand, Is Scan running or not.根据您可以理解的值,扫描是否正在运行。
private BluetoothLeScanner bluetoothLeScanner =
        BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
private boolean static mScanning = false;
private Handler handler = new Handler();

// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

private void scanLeDevice() {
    if (!mScanning) {
        // Stops scanning after a pre-defined scan period.
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                bluetoothLeScanner.stopScan(leScanCallback);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        bluetoothLeScanner.startScan(leScanCallback);
    } else {
        mScanning = false;
        bluetoothLeScanner.stopScan(leScanCallback);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM