startLeScan replacement to current api

前端 未结 5 563
北海茫月
北海茫月 2020-12-14 01:18

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) {
             


        
5条回答
  •  不思量自难忘°
    2020-12-14 02:05

    Remember that the method:

    public BluetoothLeScanner getBluetoothLeScanner ()
    

    isn't static. If you do:

    BluetoothAdapter.getBluetoothLeScanner() 
    

    you will get an error, since getDefaultAdapter() is a static method, but getBluetoothLeScanner() isn't.

    You need an instance of a BluetoothAdapter. You can get one by doing:

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

    That way, you can try:

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

    More info here: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html

提交回复
热议问题