2015-05-13 50 views
12

目标是读取蓝牙LE心率监视器的值。startLeScan替换为当前api

使用谷歌的样品,我得到

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?

+3

一年后,同样的样品仍不赞成,为什么他们不作更新?.. – user3290180

+0

好一点,当我在谷歌工作,我会继续更新的文档。我到了这里,因为我找不到如何:p –

回答

11

在Android棒棒糖中不推荐使用方法BluetoothAdapter.startLeScanBluetoothAdapter.stopLeScan。作为替代BluetoothLeScanner被引入并充当扫描控制器。

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

+0

我可以在Android 4.3中使用BluetoothLeScanner(使用Lollipop编译,但min SDK设置为18)? – Felix

+2

@Fei在低于API 21的操作系统版本上使用BluetoothLeScanner是不可能的。这个类在这些设备上不会出现在Android SDK中。尝试对它们执行BLE扫描将导致抛出ClassNotFoundException。 –

5

使用BluetoothAdapter.getBluetoothLeScanner()得到BluetoothLeScanner的一个实例。

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

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

+0

使用'import android.bluetooth.BluetoothAdapter;'我只接收'.getDefaultAdapter()'而不是'.getBluetoothLeScanner()'。因此我没有'startScan',但只提到了被弃用的方法。 –

+0

你有目标SDK设置为21吗?你是否有一个BluetoothAdapter的实例来获得BluetoothLeScanner? – JPS

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

9

谢谢大家的响应。总结这个问题的答案,我将添加我的最终代码片段。

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