gpt4 book ai didi

android - API 21 中弃用了 startLeScan

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:57 25 4
gpt4 key购买 nike

我正在开发一个使用低功耗蓝牙的安卓应用程序。我使用类 BluetoothAdapter 但现在在 API-21 中类是 BluetoothLeScanner 我的旧代码正在实现接口(interface) BluetoothAdapter.LeScanCallback 我现在必须实现什么接口(interface)?或者我可以做其他解决方案吗?stopLeScanstartLeScan 这两个方法已被弃用,这就是为什么我想使用类 BluetoothLeScanner 它有方法开始和停止。

这是我类(class)的代码:

public class BleDevicesScanner implements Runnable, BluetoothAdapter.LeScanCallback {
private static final String TAG = BleDevicesScanner.class.getSimpleName();

private static final long DEFAULT_SCAN_PERIOD = 500L;
public static final long PERIOD_SCAN_ONCE = -1;

private final BluetoothAdapter bluetoothAdapter;
private final Handler mainThreadHandler = new Handler(Looper.getMainLooper());
private final LeScansPoster leScansPoster;

private long scanPeriod = DEFAULT_SCAN_PERIOD;
private Thread scanThread;
private volatile boolean isScanning = false;

public BleDevicesScanner(BluetoothAdapter adapter, BluetoothAdapter.LeScanCallback callback) {
if (adapter == null)
throw new IllegalArgumentException("Adapter should not be null");

bluetoothAdapter = adapter;

leScansPoster = new LeScansPoster(callback);
}

public synchronized void setScanPeriod(long scanPeriod) {
this.scanPeriod = scanPeriod < 0 ? PERIOD_SCAN_ONCE : scanPeriod;
}

public boolean isScanning() {
return scanThread != null && scanThread.isAlive();
}

public synchronized void start() {
if (isScanning())
return;

if (scanThread != null) {
scanThread.interrupt();
}
scanThread = new Thread(this);
scanThread.setName(TAG);
scanThread.start();
}

public synchronized void stop() {
isScanning = false;
if (scanThread != null) {
scanThread.interrupt();
scanThread = null;
}
bluetoothAdapter.stopLeScan(this);
}

@Override
public void run() {
try {
isScanning = true;
do {
synchronized (this) {
bluetoothAdapter.startLeScan(this);
}

if (scanPeriod > 0)
Thread.sleep(scanPeriod);

synchronized (this) {
bluetoothAdapter.stopLeScan(this);
}
} while (isScanning && scanPeriod > 0);
} catch (InterruptedException ignore) {
} finally {
synchronized (this) {
bluetoothAdapter.stopLeScan(this);
}
}
}

@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
synchronized (leScansPoster) {
leScansPoster.set(device, rssi, scanRecord);
mainThreadHandler.post(leScansPoster);
}
}

private static class LeScansPoster implements Runnable {
private final BluetoothAdapter.LeScanCallback leScanCallback;

private BluetoothDevice device;
private int rssi;
private byte[] scanRecord;

private LeScansPoster(BluetoothAdapter.LeScanCallback leScanCallback) {
this.leScanCallback = leScanCallback;
}

public void set(BluetoothDevice device, int rssi, byte[] scanRecord) {
this.device = device;
this.rssi = rssi;
this.scanRecord = scanRecord;
}

@Override
public void run() {
leScanCallback.onLeScan(device, rssi, scanRecord);
}
}
}

最佳答案

Android documenation描述了如何使用新的 BluetoothAdapter startScan() 方法:

public void startScan (List<ScanFilter> filters, ScanSettings settings, ScanCallback callback)

您可以为 API 级别 21 及更高级别创建一个 BleDevicesScannerV21 类,它使用 ScanCallback 而不是 BluetoothAdapter.LeScanCallback

关于android - API 21 中弃用了 startLeScan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318391/

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