0

I want to analysis the heart rate of a heart rate monitor. For that I want to save the last used device and compare it to the found devices. Because it takes a while to find devices, mDevice remains null. What do I have to do to update mDevice properly?

private ArrayList<BluetoothDevice> mDeviceList;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private BluetoothDevice mDevice;

private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

    @Override
protected void onStart() {
    super.onStart();
    // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
    // fire an intent to display a dialog asking the user to grant permission to enable it.
    if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }


    // Initializes list view adapter.
    mDeviceList = new ArrayList<BluetoothDevice>();
    scanLeDevice(true);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final String adress = prefs.getString(getString(R.string.device_address), "");

    for(BluetoothDevice b : mDeviceList){
        if(b.getAddress().equals(adress)){
            mDevice = b;
        }
    }
    if(mDevice != null)
        Log.e(TAG, mDevice.getAddress());

}

taken from the google manual:

    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);
                invalidateOptionsMenu();
            }
        }, SCAN_PERIOD);
        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
    invalidateOptionsMenu();
}

// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(!mDeviceList.contains(device)){
                            mDeviceList.add(device);
                        }
                    }
                });
            }
        };

I hope these are enough information. If something is missing, feel free to ask

Frauke
  • 17
  • 4

2 Answers2

0

The scan is a background activity your trying to look at the results straight after you start it rather than waiting for it to finish. You may want to put your checking code into the onLeScan call-back directly and stopping the scan as soon as you see the device you want.

You could also try not doing the scan all together if you already have the details for the device just try going straight to connect. The details of if you need to scan before you connect are not at all clear from the documentation so you need to be prepared to experiment a bit as it's still all far too flaky.

Ifor
  • 2,825
  • 1
  • 22
  • 25
0

Just move your code where you try to find the last device (everything in onStart() after SharedPreferences prefs...) to after you have already found devices, for example at the end of your runnable (after invalidateOptionsMenu();)

sorianiv
  • 4,845
  • 1
  • 23
  • 27