[英]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.我正在开发一个移动应用程序,我正在执行以下操作以从我的传感器获取数据。
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_LATENCY
在
scanFilters
中,我设置了设备 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.你需要为此做一些修改。
mScanning
is the marker.mScanning
是标记。mScanning
value.mScanning
值。mScanning
is true or false.mScanning
是 true 还是 false。public static boolean isScanRunning(){
return mScanning;
}
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.