BLE蓝牙开发不太清楚的可以看下这篇文章
一、先来看下扫描蓝牙的代码
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
//扫描蓝牙
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
if (scanner != null) {
scanner.startScan(scanCallback);
}
} else {
bluetoothAdapter.startLeScan(leScanCallback);
}
-
由于编译器提示~~startLeScan()~~是过时的API,所以写成如上代码
-
然后每次扫描发现,扫描到自己的设备总是的那么慢。
二、突发想到GitHub上有个FastBle
开源项目,特意安装Demo试试扫描看会不会很快。结果就是每次都是很快的扫描到了设备这让我很是纳闷;就直接看他的源代码了,发现他用的扫描API就是startLeScan()
。
- 也就是扫描代码不使用
BluetoothLeScanner
API
bluetoothAdapter.startLeScan(leScanCallback);