Tim*_*Tim 22 android bluetooth-lowenergy android-bluetooth
我使用LeScanCallback(不能使用更新的扫描方法,因为我正在为api 18开发.不重要,因为android 5.0+ apis不提供此功能)以检测何时检测到附近的BLE设备:
private BluetoothAdapter.LeScanCallback bleCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
discoveredDevices.add(bluetoothDevice);
}
};
Run Code Online (Sandbox Code Playgroud)
我没有与设备配对或连接,因为这不是必需的,我只是想看看附近有哪些设备.
我正在尝试提供一项服务,每隔5分钟左右,就会调用一个网络服务器来更新当时附近的设备.
棘手的部分是Android设备将移动,所以现在附近的蓝牙设备可能不会在5分钟内.在那种情况下,我需要将其删除discoveredDevices.
理想情况下,我希望在蓝牙设备处于范围之前接收回叫,但现在不再.但是这个回调不存在.
(我知道android.bluetooth.device.action.ACL_CONNECTED和android.bluetooth.device.action.ACL_DISCONNECTED广播,但这些是你连接到蓝牙设备,我不想要.)
一个选项是每隔5分钟进行一次新的扫描,但是你无法判断所有附近的设备何时被发现,所以你必须进行定时扫描,例如扫描5秒然后将收集的数据发送到网络服务.
这听起来很脏并且有风险,因为您无法确定所有附近的设备是否在规定的时间内被发现,因此我非常希望避免这样做.
还有另一种方法吗?
编辑
某些设备会持续报告附近蓝牙设备的发现,即使它们之前已经被发现过.如果该功能是通用的,我可以解决我的问题,但这是特定于设备的.
例如,我的手机的蓝牙适配器只能发现附近的设备一次.我测试过的其他一些设备不断报告相同的附近设备,但不是所有设备都这样,所以我不能不依赖它.
Tim*_*Tim 15
这听起来很脏并且有风险,因为您无法确定所有附近的设备是否在规定的时间内被发现,因此我非常希望避免这样做.
这听起来像是一个合理的假设,但这是错误的.
蓝牙低功耗以特定方式工作,BLE设备有一些限制.例如,它们具有固定范围的可能广告频率,范围从20毫秒到10.24秒,步长为0.625毫秒.有关更多详细信息,请参见此处和此处.
这意味着在设备广播新广告包之前最多可能需要10.24秒.BLE设备通常(如果不是总是)为其所有者提供调整其广告频率的方式,因此频率当然可以变化.
如果您定期收集有关附近设备的数据(如您的设备),可以使用具有固定时间限制的扫描,将数据保存在某处,重新启动扫描,收集新数据,与旧数据进行比较 - >获取结果.
例如,如果在扫描1中找到设备但在扫描2中未找到设备,则可以断定设备在范围内,但现在不再存在.
反过来也是如此:如果在扫描4中找到设备但在扫描3中没有找到设备,则它是新发现的设备.
最后,如果在扫描5中找到设备,但在扫描6中未找到,但在扫描7中再次找到该设备,则重新发现它并且如果需要可以如此处理.
因为我在这里回答我自己的问题,所以我将添加用于实现此问题的代码.
我在后台服务中完成扫描,并使用BroadcastReceivers与应用程序的其他部分进行通信.Asset是我的自定义类,包含一些数据.DataManager是我的一个自定义类 - 你怎么猜它 - 管理数据.
public class BLEDiscoveryService extends Service {
// Broadcast identifiers.
public static final String EVENT_NEW_ASSET = "EVENT_NEW_ASSET ";
public static final String EVENT_LOST_ASSET = "EVENT_LOST_ASSET ";
private static Handler handler;
private static final int BLE_SCAN_TIMEOUT = 11000; // 11 seconds
// Lists to keep track of current and previous detected devices.
// Used to determine which are in range and which are not anymore.
private List<Asset> previouslyDiscoveredAssets;
private List<Asset> currentlyDiscoveredAssets;
private BluetoothAdapter bluetoothAdapter;
private BluetoothAdapter.LeScanCallback BLECallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
Asset asset = DataManager.getAssetForMACAddress(bluetoothDevice.getAddress());
handleDiscoveredAsset(asset);
}
};
@Override
public void onCreate() {
super.onCreate();
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = manager.getAdapter();
previouslyDiscoveredAssets = new ArrayList<>();
currentlyDiscoveredAssets = new ArrayList<>();
handler = new Handler();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Start scanning.
startBLEScan();
// After a period of time, stop the current scan and start a new one.
// This is used to detect when assets are not in range anymore.
handler.postDelayed(new Runnable() {
@Override
public void run() {
performRepeatingTask();
// Repeat.
handler.postDelayed(this, BLE_SCAN_TIMEOUT);
}
}, BLE_SCAN_TIMEOUT);
// Service is not restarted if it gets terminated.
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
handler.removeCallbacksAndMessages(null);
stopBLEScan();
super.onDestroy();
}
private void startBLEScan() {
bluetoothAdapter.startLeScan(BLECallback);
}
private void stopBLEScan() {
bluetoothAdapter.stopLeScan(BLECallback);
}
private void handleDiscoveredAsset(Asset asset) {
currentlyDiscoveredAssets.add(asset);
// Notify observers that we have a new asset discovered, but only if it was not
// discovered previously.
if (currentlyDiscoveredAssets.contains(asset) &&
!previouslyDiscoveredAssets.contains(asset)) {
notifyObserversOfNewAsset(asset);
}
}
private void performRepeatingTask() {
// Check if a previously discovered asset is not discovered this scan round,
// meaning it's not in range anymore.
for (Asset asset : previouslyDiscoveredAssets) {
if (!currentlyDiscoveredAssets.contains(asset)) {
notifyObserversOfLostAsset(asset);
}
}
// Update lists for a new round of scanning.
previouslyDiscoveredAssets.clear();
previouslyDiscoveredAssets.addAll(currentlyDiscoveredAssets);
currentlyDiscoveredAssets.clear();
// Reset the scan.
stopBLEScan();
startBLEScan();
}
private void notifyObserversOfNewAsset(Asset asset) {
Intent intent = new Intent();
intent.putExtra("macAddress", asset.MAC_address);
intent.setAction(EVENT_NEW_ASSET);
sendBroadcast(intent);
}
private void notifyObserversOfLostAsset(Asset asset) {
Intent intent = new Intent();
intent.putExtra("macAddress", asset.MAC_address);
intent.setAction(EVENT_LOST_ASSET);
sendBroadcast(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码并不完美,甚至可能是错误的,但它至少会给你一个如何实现它的想法或示例.