Android BLE SCAN_MODE_LOW_POWER扫描异常问题求助
蓝牙低功耗扫描异常问题
我查阅资料得知SCAN_MODE_LOW_POWER的扫描逻辑为扫描0.5秒、暂停4.5秒(扫描窗口=0.5秒,扫描间隔=5秒)。但我的程序出现以下问题:
- 扫描窗口间隔不稳定,在8-12秒之间波动,有时甚至达20秒;
- 在Android 10及以上版本中,程序前台运行时
SCAN_MODE_LOW_POWER无法正常工作,扫描速度变得极快。
注:我的广播模式设置为low latency mode(广播间隔=100ms)。
扫描代码
public class Service_Scan extends Service { @RequiresApi(api = Build.VERSION_CODES.O) public Service_Scan() { Log.e(TAG,"Service_Scan start"); startScanning(); stopScanningButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopScanning(); stopSelf(); } }); startScanningButton.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.O) public void onClick(View v) { Log.e(TAG,"start scanning"); startScanningButton.setVisibility(View.INVISIBLE); stopScanningButton.setVisibility(View.VISIBLE); startScanning(); } }); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @RequiresApi(api = Build.VERSION_CODES.O) public void startScanning() { byte[] data_all = new byte[ManufacturerData_size]; System.arraycopy(id_byte, 0, data_all, 1, id_byte.length); byte[] data_mask = new byte[] {0x00,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; ScanFilter Mau_filter_extended = new ScanFilter.Builder().setManufacturerData(0xffff,test.getBytes()).build(); ScanFilter Mau_filter_legacy = new ScanFilter.Builder().setManufacturerData(0xffff,data_all,data_mask).build(); ScanFilter name_filter = new ScanFilter.Builder().setDeviceName("1").build(); ArrayList<ScanFilter> filters = new ArrayList<>(); filters.add(Mau_filter_extended); filters.add(Mau_filter_legacy); filters.add(name_filter); ScanSettings settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) .build(); mBluetoothLeScanner.startScan(filters, settings, leScanCallback); } public void stopScanning() { Log.e(TAG,"stopping scanning"); peripheralTextView.append("Stopped Scanning"); startScanningButton.setVisibility(View.VISIBLE); stopScanningButton.setVisibility(View.INVISIBLE); AsyncTask.execute(new Runnable() { @Override public void run() { mBluetoothLeScanner.stopScan(leScanCallback); } }); } }
权限配置
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
内容的提问来源于stack exchange,提问作者Tse
相关产品推荐
相关产品推荐

