确保在停止扫描前调用LeScanCallback的stopScan方法。以下是一个示例:
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// 处理扫描到的蓝牙设备
}
};
// 开始扫描设备
private void startScan() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startLeScan(mLeScanCallback);
}
// 停止扫描设备
private void stopScan() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.stopLeScan(mLeScanCallback);
}