VladMorzhanov
9/23/2017 - 2:40 PM

Android scan ble devices functions with stop argument.

Android scan ble devices functions with stop argument.

private void scanLeDevice(final boolean enable, String mac, long timeout) {
        BluetoothManager mBLEManager = (BluetoothManager) MevicsApplication
                .getAppContext()
                .getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter mBLEAdapter = mBLEManager.getAdapter();
        BluetoothAdapter.LeScanCallback mLeScanCallback =
                new BluetoothAdapter.LeScanCallback() {
                    @Override
                    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                        if (device.getAddress().equals(mac)) {
                            deviceFound = true;
                            mBLEAdapter.stopLeScan(this);
                            continueFirmware(mac);
                        }
                    }
                };
        if (enable) {
            mBLEAdapter.startLeScan(mLeScanCallback);
            mHandler.postDelayed(() -> {
                mBLEAdapter.stopLeScan(mLeScanCallback);
                if (!deviceFound) {
                    EventBus.getDefault().post(new DFUProgressViewChangeEvent(false));
                    enableViews();
                    SettingsModel mSettingsModel = SettingsModel.getInstance(context);
                    mSettingsModel.setUpdateFirmware(true);
                    MevicsToast.makeText(this, getString(R.string.device_not_found_m),
                            Toast.LENGTH_LONG).show();
                }
            }, timeout);
        } else {
            mBLEAdapter.stopLeScan(mLeScanCallback);
        }
    }