gpt4 book ai didi

Android 蓝牙 LE 扫描如何检查设备是否超出范围?

转载 作者:行者123 更新时间:2023-11-30 05:06:08 29 4
gpt4 key购买 nike

我在使用 BluetoothLeScannerstartScan 方法时遇到了问题,找到了一个 BLE 设备,但是当我关闭 BLE 设备时,我的手机仍然显示此设备已打开!!

我尝试过使用:

private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("ScanCallback", String.format("onScanResult(int callbackType[%d], ScanResult result)", callbackType));
final BluetoothDevice btDevice = result.getDevice();
if (btDevice == null){
Log.e("ScanCallback", "Could not get bluetooth device");
return;
}

final String macAddress = btDevice.getAddress();
if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
// NOTE: I've never got here
final BluetoothDevice outOfRangeDevice = mBtDevices.get(macAddress);
...
} else {
...
}
}
...
};

伙计,我还没有找到如何检测 BLE 设备在其他资源中丢失的解决方案,例如(Android SDK 引用、论坛、stackoverflow 等)(:

任何帮助将不胜感激!!

最佳答案

在谷歌搜索和浏览 Android 文档的过程中,我找到了检测设备是否超出范围的方法。我想分享我的解决方案:

...
public void scanBLEDevices(final boolean enable) {
if(mLeScanner == null) {
Log.d(TAG, "Could not get LEScanner object");
throw new InternalError("Could not get LEScanner object");
}

if (enable) {
startLeScan();
} else {
stopLeScan(false);
}
}

private void startLeScan() {
Log.i(TAG, "startLeScan(BluetoothLeScanner mLeScanner)");
mScanning = true;
mInRangeBtDevices.clear();
if (mStartScanCallback != null) {
mHandler.removeCallbacks(mStartScanCallback);
}
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mLeScanner.startScan(mScanFilters, mScanSettings, mScanCallback);
}
mStopScanCallback = new Runnable() {
@Override
public void run() {
stopLeScan(true);
}
};
mHandler.postDelayed(mStopScanCallback, SCAN_PERIOD);
}

private void stopLeScan(final boolean isContinueAfterPause) {
Log.i(TAG, "stopLeScan(BluetoothLeScanner mLeScanner)");
mScanning = false;
if (mStopScanCallback != null) {
mHandler.removeCallbacks(mStopScanCallback);
}
removeOutOfRangeDevices();
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLeScanner.stopScan(mScanCallback);
}
if (isContinueAfterPause) {
mStartScanCallback = new Runnable() {
@Override
public void run() {
startLeScan();
}
};
mHandler.postDelayed(mStartScanCallback, SCAN_PAUSE);
}
}

private void removeOutOfRangeDevices() {
final Set<String> outOfRangeDevices = new HashSet<>();
for (String btAddress : mBtDevices.keySet()) {
if (!mInRangeBtDevices.contains(btAddress)) {
outOfRangeDevices.add(btAddress);
}
}
for (String btAddress : outOfRangeDevices) {
final BluetoothDevice outOfRangeDevice = mBtDevices.get(btAddress);
mBtDevicesRSSI.remove(btAddress);
mBtDevices.remove(btAddress);
}
}
...

解释:

  1. 如您所见,我在每个扫描周期添加了 mInRangeBtDevices 集合,它将保留在当前扫描期间找到的所有设备。
  2. 当我停止扫描时,我还会使用一个额外的助手集合 outOfRangeDevices
  3. 从以前的列表中删除不再可用的超出范围的设备

我认为这个示例会很有用,您将能够将它集成到您​​自己的代码中

关于Android 蓝牙 LE 扫描如何检查设备是否超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54562410/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com