Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

android - startLeScan replacement to current api

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);
    }
}

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...