startLeScan替换为当前的api

时间:2015-05-13 18:53:14

标签: android bluetooth bluetooth-lowenergy

目标是阅读蓝牙LE心率监测器的值。

使用谷歌的示例,我得到了

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的方法。

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

5 个答案:

答案 0 :(得分:18)

Android Lollipop中不推荐使用BluetoothAdapter.startLeScanBluetoothAdapter.stopLeScan两种方法。随着替换BluetoothLeScanner被引入并充当扫描控制器。

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

答案 1 :(得分:14)

谢谢大家的回复。为了总结这个问题的答案,我将添加我的最终代码段。

private BluetoothAdapter mBluetoothAdapter;


private ScanCallback mLeScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
}


private void scanLeDevice(final boolean enable) {

    final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;

                bluetoothLeScanner.stopScan(mLeScanCallback);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        bluetoothLeScanner.startScan(mLeScanCallback);
    } else {
        mScanning = false;
        bluetoothLeScanner.stopScan(mLeScanCallback);
    }
}

答案 2 :(得分:7)

使用BluetoothAdapter.getBluetoothLeScanner()获取BluetoothLeScanner的实例。

然后,您可以使用startScanstopScan方法开始或停止扫描,这与弃用版本非常相似。

区别是你可以通过scanfilters和设置。 ScanCallback有关于找到的设备的更多信息。过滤器允许您根据名称,macaddress,服务UUID等过滤扫描结果。扫描设置允许您控制扫描功率。

答案 3 :(得分:5)

请记住方法:

public BluetoothLeScanner getBluetoothLeScanner ()

不是静态的。如果你这样做:

BluetoothAdapter.getBluetoothLeScanner() 

您将收到错误,因为getDefaultAdapter()是静态方法,但getBluetoothLeScanner()不是。

您需要BluetoothAdapter的实例。你可以这样做:

(BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE).getAdapter()

这样,您可以尝试:

Context mContext = getBaseContext();
BluetoothAdapter mAdapter = ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
BluetoothLeScanner mLeScanner = mAdapter.getBluetoothLeScanner();

mLeScanner.startScan(...);

此处有更多信息:http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html

答案 4 :(得分:3)

避免警告。只需在调用函数之前检查API版本。您可以使用代码

private BluetoothAdapter bluetoothAdapter;
private BluetoothAdapter.LeScanCallback leScanCallback;
private BluetoothAdapter.LeScanCallback leScanCallback;
private ScanCallback scanCallback;
private ScanSettings scanSetting;

// Check before call the function
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            bluetoothAdapter.getBluetoothLeScanner().startScan(filterList, scanSetting, scanCallback);
        } else {
            bluetoothAdapter.startLeScan(leScanCallback);
        }