我正在开发一个应用程序,当用户显示应用程序的某个片段时,我将在该应用程序中不断搜索范围内的可用信标。为了做到这一点,我创建了一个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);
}
}
我如何开始和停止runnable:
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();
答案 0 :(得分: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;
}