检查以下几个方面:
以下是一段检查设备是否支持BLE以及启动BLE扫描的示例代码:
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private BluetoothScanCallback mBluetoothScanCallback;
// 初始化蓝牙适配器及扫描回调
private void initBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
// 设备不支持蓝牙或未开启蓝牙功能
return;
}
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
mBluetoothScanCallback = new BluetoothScanCallback();
}
// 启动BLE扫描
private void startBluetoothScan() {
if (mBluetoothLeScanner == null || mBluetoothScanCallback == null) {
// 未初始化
return;
}
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) // 低功耗模式
// 可加入其他设置参数
.build();
List filters = new ArrayList<>();
// 可加入过滤条件
mBluetoothLeScanner.startScan(filters, settings, mBluetoothScanCallback);
}
// BLE扫描回调
private class BluetoothScanCallback extends ScanCallback {
@Override
public void onScanResult(int callbackType
下一篇:BLE扫描与处理程序的问题