startLeScan替换为当前api [英] startLeScan replacement to current api
问题描述
目标是读取蓝牙LE心率监测器的值.
Goal is to read the values of a bluetooth LE heart rate monitor.
使用Google的示例,我得到
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.stopLeScan .作为替代,引入了 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屋!