我用的是小米note 4(Android 7.0)和Moto x play(Android 7.1.1)
我正在单独进行 BLE 扫描。 扫描时我收到扫描响应“扫描失败” 打开/关闭蓝牙不会影响扫描响应。 打开/关闭 Wifi 也不影响扫描响应。
(但在这种情况下,android 内置(从设置->蓝牙)蓝牙扫描工作正常)。
我也使用了 BLE 扫描仪应用程序,但该应用程序也未检测到 BLE 广告!
我尝试使用此功能开启/关闭飞行模式,我的设备能够正常扫描。
扫描功能:
mLeScanner.startScan(filters, scanSettings, mScanCallback);
扫描回调:
ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.e("TAG","onScanResult");
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Log.e("TAG","onScanFailed");
}
}
扫描设置:
scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
过滤器:
List<ScanFilter> filters = new ArrayList<>();
ScanFilter filter = new ScanFilter.Builder().setDeviceAddress("device address").build();
filters.add(filter);
信标扫描过滤器
ScanFilter.Builder builder = new ScanFilter.Builder();
builder.setManufacturerData(0x004c, new byte[]{});
有人知道为什么它只适用于切换飞行模式吗?
网络会影响BLE扫描吗?
最佳答案
错误代码 0x02 表示 SCAN_FAILED_APPLICATION_REGISTRATION_FAILED(无法启动扫描,因为无法注册应用)。这意味着,在移动到扫描之前,我们需要初始化蓝牙适配器
/**
* Initialize BluetoothAdapter
* Check the device has the hardware feature BLE
* Then enable the hardware,
*/
public boolean init(Context context) {
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
return mBluetoothAdapter != null && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
然后注册接收者
**
* Register GATT update receiver
*/
private void registerServiceReceiver() {
this.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
服务初始化方法也包含在答案中。服务创建是可选的。
/**
* Initialize Bluetooth service.
*/
public void initBLEService(Context context) {
try {
this.mContext = context;
if (mBLEService == null) {
Intent gattServiceIntent = new Intent(mContext, BLEService.class);
if (this.mContext != null) {
isBind = mContext.bindService(gattServiceIntent, mServiceConnection, mContext.BIND_AUTO_CREATE);
}
}
} catch (Exception e) {
AppLog.logError(TAG, e.getMessage());
}
}
希望您已经在下面给出的 list 中添加了权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
希望对您有所帮助。
关于android - BLE扫描失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185118/