startLeScan 替换当前 api [英] startLeScan replacement to current api

查看:15
本文介绍了startLeScan 替换当前 api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是读取蓝牙 LE 心率监测器的值.

Goal is to read the values of a bluetooth LE heart rate monitor.

使用谷歌的样本,我得到

Using google's sample, I get

private void scanLeDevice(final boolean enable) {
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}

导致 mBluetoothAdapter.stopLeScan 显示为已弃用.Startscan 不是 mBluetoothAdapter 的方法.

which causes mBluetoothAdapter.stopLeScan to be shown as deprecated. Startscan is no method of mBluetoothAdapter though.

如何更改它以使其与当前 API 一起使用?

How to change this for it to work with the current API?

推荐答案

两种方法BluetoothAdapter.startLeScanBluetoothAdapter.stopLeScan 在 Android Lollipop 中已弃用.作为替代,引入了 BluetoothLeScanner 并充当扫描控制器.

Both methodsBluetoothAdapter.startLeScan and BluetoothAdapter.stopLeScan were deprecated in Android Lollipop. As a replacement BluetoothLeScanner were introduced and acting as a scan controller.

如果您开发基于 BLE 的应用程序,您应该通过 BluetoothAdapter(Android 4.3 和 Android 4.4)或 BluetoothLeScanner 控制扫描.Android Lollipop 中引入的 API 在电池功耗方面提供了更多功能.

If you develop BLE-based application you should control either scan via the BluetoothAdapter (Android 4.3 and Android 4.4) or the BluetoothLeScanner. The API introduced in Android Lollipop offers much greater features in terms of battery power consumption.

这篇关于startLeScan 替换当前 api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆