2

我创建了一个应用程序,当超过三个信标被测距时,它会定位信标并计算位置。

问题是,当我想显示位置时,必须启动一个新的 Intent,所以主要活动不在前台。大约 5 秒后,信标的测距停止,我的位置也在计算,因为信标的距离不再改变。

是否有可能继续测距信标?我试图在我的主要活动中启动一个异步任务,但它不起作用,也许有一个错误,我不知道。

这是我的异步任务代码和 OnBeaconServiceConnect():

public class Monitor_Screen extends Activity implements BeaconConsumer,
    SensorEventListener {

private class asyncThread extends AsyncTask<Activity, Void, BeaconManager> {


    @Override
    protected BeaconManager doInBackground(Activity... arg0) {
        // TODO Auto-generated method stub

        Thread.currentThread().setName("BeaconManagerThread");

        Application myApplication = new Application();
        // BeaconManager
        myBeaconManager = BeaconManager.getInstanceForApplication(myApplication);
        myBeaconManager
                .getBeaconParsers()
                .add(new BeaconParser()
                        .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        myBeaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1));
        myBeaconManager.bind(Monitor_Screen.this);



        // Region
        myRegion = new Region("all beacons", null, null, null); 
        startService(Monitor_Screen.this.getIntent());
        return myBeaconManager;
    }

}

@Override
public void onBeaconServiceConnect() {
    // // range beacons on connect

    try {
        myBeaconManager.startRangingBeaconsInRegion(myRegion);
    } catch (RemoteException e) {
        Toast.makeText(getApplicationContext(), e.getMessage(),
                Toast.LENGTH_LONG).show();
    }
    myBeaconManager.setRangeNotifier(new RangeNotifier() {

        public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
                Region myRegion) {

            if (beacons.size() > 0) {
                Iterator<Beacon> myIterator = beacons.iterator();
                while (myIterator.hasNext()) {
                    Beacon tempBeacon = myIterator.next();
                    MyBeacon myBeacon = new MyBeacon(tempBeacon.getId1(),
                            tempBeacon.getId2(), tempBeacon.getId3(),
                            tempBeacon.getBluetoothAddress(), tempBeacon
                                    .getRssi(), tempBeacon.getDistance(),
                            tempBeacon.getTxPower(), System
                                    .currentTimeMillis(),
                            new Position(0, 0));
                    boolean isIn = false;
                    for (MyBeacon blub : myVector) {
                        if (blub.getBTAdress().equals(
                                myBeacon.getBTAdress())) {
                            isIn = true;
                            blub.distance = myBeacon.getDistance();
                        }
                    }
                    if (!isIn)
                        myVector.add(myBeacon);
                }
            }

            logBeaconData();
            for (int i = 0; i < myVector.size(); i++) {
                if (System.currentTimeMillis()
                        - myVector.get(i).getTimeStamp() > 10000) {
                    myVector.remove(i);
                }
            }
        }
    });

    try {
        myBeaconManager.startMonitoringBeaconsInRegion(myRegion);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    myBeaconManager.setMonitorNotifier(new MonitorNotifier() {

        @Override
        public void didExitRegion(Region arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void didEnterRegion(Region arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void didDetermineStateForRegion(int arg0, Region arg1) {
            // TODO Auto-generated method stub

        }
    });

}
4

1 回答 1

0

让信标测距在后台继续进行的最简单方法是在自定义类中设置信标测距android.app.Application,并让它将回调转发给您Activity,以便仅当它Activity在前台时更新显示。

您可以在此处的Android Beacon 库的参考应用程序中看到执行此操作的示例:https ://github.com/AltBeacon/android-beacon-library-reference

android.app.Application下面是设置它的自定义类的摘录。请注意,测距设置一次,并将在应用程序的整个生命周期中持续。测距回调被传递给mRangingActivity它是否存在,因此它可以进行任何必要的 UI 处理。

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier {
...

    public void onCreate() {
        mAllBeaconsRegion = new Region("all beacons", null, null, null);
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBackgroundPowerSaver = new BackgroundPowerSaver(this);     
        mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
        if (mRangingActivity != null) {
            mRangingActivity.didRangeBeaconsInRegion(arg0, arg1);
        }

    }

    @Override
    public void didEnterRegion(Region arg0) {
        try {
            Log.d(TAG, "entered region.  starting ranging");
            mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
            mBeaconManager.setRangeNotifier(this);
        } catch (RemoteException e) {
            Log.e(TAG, "Cannot start ranging");
        }
    }

...
}
于 2014-10-29T20:12:12.213 回答