0

I am developing an app in which I will be searching for available beacons in range continuously, when the user displays a certain fragment of the app. In order to do this I created a Runnable in which i call a method which starts a scan. Scan results are send to the Handler using a Message. The problem I have is that I don't know how can I call a method which would stop the scan. My code:

private class BleScanRunnable implements Runnable, BleScanCallback {

        private final BleScanCallback mBleScanCallback = this;
        private BleScan mBleScan =
                BleScanFactory.getFactory(BleFragment.this.getActivity())
                        .createBleScan();

        @Override
        public void onScanResult(BluetoothDevice device, int rssi, byte[] scanRecord) {
            Message msg = mHandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putParcelable(ARG_BLUETOOTH_DEVICE, device);
            bundle.putInt(ARG_RSSI, rssi);
            bundle.putByteArray(ARG_SCAN_RECORD, scanRecord);
            msg.setData(bundle);
            mHandler.sendMessage(msg);
        }

        @Override
        public void run() {
            mBleScan.startBleScan(mBleScanCallback);
        }

    }

How I start and stop the runnable:

    private void startBleRunnable() {
        ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor();
        BleScanRunnable bleScanRunnable = new BleScanRunnable();
        mBleScanRunnableFuture = threadPoolExecutor.submit(bleScanRunnable);
    }

    private void stopBleRunnable() {
        mBleScanRunnableFuture.cancel(true);
    }

The question is how can I not only stop the Runnable, but also call mBleScan.stopBleScan() method in onPause() method of the BleFragment

Is it OK to add such method:

public void stopBleScan() {
    mBleScan.stopBleScan();
}

to the BleScanRunnable and call it from onPause() like this:

mBleScanRunnable.stopBleScan();
fragon
  • 3,391
  • 10
  • 39
  • 76

1 Answers1

0

I have scanned the beacon and stopped the scanning in a slightly different way.Please check my sample code here:

    public class MainActivity extends AppCompatActivity implements ProximityManager.ProximityListener{
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_patient_summary_info);
                ButterKnife.bind(this);

                setSupportActionBar(toolbar);
                KontaktSDK.initialize(KONTAKT_API_KEY)
                        .setDebugLoggingEnabled(BuildConfig.DEBUG)
                        .setLogLevelEnabled(LogLevel.DEBUG, false);// set true if debug log needed.

                mProximityManager = new KontaktProximityManager(this);
                mAppPreferences = AppUtil.getAppPreferences(this);
                mScannedBeaconsList = new ArrayList<>();
                mDotor = new Gson().fromJson(mAppPreferences.getString(Constants.SETTINGS_OBJ_DOCTOR, ""), Doctor.class);
                RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
                mRealm = Realm.getInstance(realmConfig);
                getPatientList();


            }

@Override
    protected void onStart() {
        super.onStart();
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkPermissionAndStart();
        }else {
            startBluetoothIfNotStarted();
            startScan();
        }
    }

        }

Here is my start scanning code:

private void startScan() {
        mProximityManager.initializeScan(getScanContext(), new OnServiceReadyListener() {
            @Override
            public void onServiceReady() {
                mProximityManager.attachListener(PatientSummaryInfoActivity.this);
            }

            @Override
            public void onConnectionFailure() {
                AppUtil.writeLog(TAG, "Beacon connection lost.");
            }
        });
    }

Here is my how I stopped scanning:

@Override
    protected void onStop() {
        super.onStop();
        mProximityManager.detachListener(this);
        mProximityManager.disconnect();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRealm.close();
        mProximityManager = null;
    }
Lips_coder
  • 686
  • 1
  • 5
  • 17