0

我正在开发一个应用程序,当用户显示应用程序的某个片段时,我将在其中连续搜索范围内的可用信标。为了做到这一点,我创建了一个Runnable在其中调用开始扫描的方法。扫描结果Handler使用Message. 我遇到的问题是我不知道如何调用可以停止扫描的方法。我的代码:

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

    }

我如何启动和停止可运行文件:

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

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

问题是我怎样才能不仅停止Runnable,而且在方法中调用mBleScan.stopBleScan()方法onPause()BleFragment

添加这样的方法是否可以:

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

BleScanRunnable并这样调用它onPause()

mBleScanRunnable.stopBleScan();
4

1 回答 1

0

我已经扫描了信标并以稍微不同的方式停止了扫描。请在此处查看我的示例代码:

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

        }

这是我的开始扫描代码:

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

这是我停止扫描的方式:

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRealm.close();
        mProximityManager = null;
    }
于 2016-04-19T12:29:37.443 回答