Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 592
Vitalis Hommel Avatar asked May 13 '15 18:05

Vitalis Hommel


2 Answers

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.

like image 126
dawid gdanski Avatar answered Sep 23 '22 22:09

dawid gdanski


Thank you all for your responses. To Summarize the answer to this question I'll add my final code snippet.

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);     } } 
like image 20
Pedro Varela Avatar answered Sep 26 '22 22:09

Pedro Varela