java - 蓝牙扫描仪在屏幕关闭时停止扫描或减少扫描频率

标签 java android service bluetooth-lowenergy foreground-service

我创建了简单的前台服务来扫描 BLE 设备。它应该一直扫描设备。我创建了自己的 BLE 设备,每 0.5 秒传输一帧。我有两台调试设备,一台使用 Android 9,另一台使用 Android 6。

首先,我注意到我没有获得所有 BLE 帧。我应该每 10 秒获得大约 20 帧,但是:

  • 搭载 Android 9 的设备大约有 6-7 帧,
  • 搭载 Android 6 的设备大约获得 15-17 帧,但设备会以一定的间隔进行扫描(扫描 10 秒,然后停止 5-7 秒)。

其次,我注意到当设备屏幕关闭时,它的扫描效果会更糟(或根本停止扫描):

  • 搭载 Android 9 的设备在屏幕关闭 1 分钟后停止扫描 BLE 设备,

  • 运行 Android 6 的设备在关闭屏幕后大约会出现 6 帧。

服务代码:

public class DeviceScan extends Service {

    BluetoothLeScanner btScanner;
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    List<ScanFilter> filterList = new ArrayList<>();
    {
        filterList.add(new ScanFilter.Builder().setDeviceName("MYBEACON").build());
    }

    private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
                Log.e("Adress: ",result.getDevice().getAddress());
                Log.e("RSSI: ", " rssi: " + result.getRssi());
        }
    };

    public void startScanning(){
        btScanner = mBluetoothAdapter.getBluetoothLeScanner();
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                btScanner.startScan(filterList, new ScanSettings.Builder()
                        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                        .setReportDelay(0) //when I use different value than 0, stop scanning
                        .build(),leScanCallback);
            }
        });
    }

    public void stopScanning() {
        btScanner = mBluetoothAdapter.getBluetoothLeScanner();
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                btScanner.stopScan(leScanCallback);
            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        createNotificationChannel();
        Intent notificationIntent = new Intent(this, AlbumActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText("Scanning")
                .setPriority(Notification.PRIORITY_MAX)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);


        startScanning();
        return START_STICKY;
    }

    private void createNotificationChannel()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
}

最佳答案

尝试下面的代码,它对我有用。 您需要添加空的ScanFilter并设置ScanSettings.SCAN_MODE_LOW_LATENCY

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ScanFilter.Builder builder = new ScanFilter.Builder();
                builder.setManufacturerData(0x004c, new byte[]{});// apply iBeacon Filter
                filters.add(builder.build());
            }

            ScanSettings.Builder scanSettingBuilder = new ScanSettings.Builder();
            scanSettingBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);

关于java - 蓝牙扫描仪在屏幕关闭时停止扫描或减少扫描频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59102975/

相关文章:

java - 如何使用 JProgressBar 测量任务进度并更新它

java - 如何使用java 8流将列表中的值相乘

java - ApplicationClass 类NotFoundException

android - 手机关机时会调用onDestroy吗?

android - 方法编译错误以检查服务是否正在运行

java - 共享列表上的多线程

java - 在 SonarQube 分析过程中分析另一个文件(自定义规则)

android - 为什么layout-large-v11资源不适用于Android 4.0 WVGA800手机

在android中使用相同的元素名称解析Android xml?

Android:唤醒锁和处理方向变化