Android - 无法停止测距信标

Android - unable to stop ranging beacons

我正在尝试创建一个 android 应用程序,该应用程序可以在用户指定的区域内搜索信标。 问题是当我添加距离约束并返回 mainactivity 时,该应用程序还显示了之前的测距实例。

例如:

距离的默认值是10m,当我将距离改为5m时,它仍然显示相距10m的信标。如果我将距离值更改为 2m,则会显示 5m 测距实例和 10m 测距实例。

主要活动

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.settings) {
        try {
            beaconManager.stopRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
            beaconManager.unbind(this);
            Log.w(TAG,"Stopped ranging");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        startActivity(new Intent(MainActivity.this, Settings.class));

    }

    return true;
}

变量 距离 oncreate

SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
distance = sharedPref.getFloat("distance",10.0f);

测距函数

  @Override
public void onBeaconServiceConnect() {

    //beacon ranging


    beaconManager.addRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            if (beacons.size() > 0) {
                for (Beacon beacon : beacons) {
                    if(beacon.getDistance()<= distance) {
                        Log.i(TAG,beacon.getBluetoothName());
                        Log.i(TAG,String.valueOf(distance));                      
                    }

                }
            }

        }

    });


    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {    }
}

设置

Button button;
EditText distance;
Float distanceValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    setTitle("Settings");
    SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPref.edit();
    button = (Button) findViewById(R.id.button);
    distance = (EditText) findViewById(R.id.distanceValue);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(distance.getText().toString().isEmpty()){
                distance.requestFocus();
                distance.setError("Required");
            }
            else{
                distanceValue = Float.valueOf(distance.getText().toString());
                editor.putFloat("distance",distanceValue);
                editor.commit();
                startActivity(new Intent(Settings.this,MainActivity.class));

            }
        }
    });

日志

之前

06-28 14:54:21.724 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00037
06-28 14:54:21.724 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
06-28 14:54:21.732 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
06-28 14:54:21.732 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0

之后

06-28 14:56:37.778 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
06-28 14:56:37.778 13866-18103/com.example.beaconscanner I/MonitoringActivity: 5.0
06-28 14:56:37.788 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
06-28 14:56:37.788 13866-18103/com.example.beaconscanner I/MonitoringActivity: 5.0
06-28 14:56:37.801 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00037
06-28 14:56:37.801 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0

proxiteeMini_00037在10m范围内

proxiteeMini_00035在5m范围内

我怀疑您同时拥有多个 MainActivity 运行 实例。 MainActivity 的旧实例具有距离的旧值,而新实例具有新值。创建从 SettingsActivity 返回到 MainActivity 的意图将导致这种情况。您可以通过使用 this.finish();

退出 SettingsActivity 来解决此问题