2

我目前正在开发 Android 信标应用程序。我只想列出检测到的信标。当用户点击按钮时,ListView应该会出现检测到的信标列表。这可能是一个小问题,但它给我带来了真正的麻烦。以下是我的测距和监控通知代码:

@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    ArrayList<Beacon> allRangedBeacons = (ArrayList<Beacon>) beacons;
                    ArrayList<Beacon> newRangedBeacons = new ArrayList<Beacon>();
                    ArrayList<Beacon> cloneArraylistIBeacon = (ArrayList<Beacon>) arraylistIBeacon.clone();

                    int index = 0;
                    for (Beacon presentBeacons : cloneArraylistIBeacon) {
                        boolean beaconPresent = false;
                        for (Beacon eachRangedBeacon : allRangedBeacons) {

                            if (presentBeacons.equals(eachRangedBeacon)) {
                                arraylistIBeacon.remove(index);
                                arraylistIBeacon.add(index, eachRangedBeacon);
                                // Toast.makeText(MainActivity.this,"U detected a beacon",
                                // Toast.LENGTH_LONG).show();
                                beaconPresent = true;
                                break;
                            }

                        }

                        index++;
                    }

                    for (Beacon eachRangedBeacon : allRangedBeacons) {
                        boolean beaconPresent = false;
                        for (Beacon presentBeacons : cloneArraylistIBeacon) {
                            if (eachRangedBeacon.equals(presentBeacons)) {
                                beaconPresent = true;

                                break;
                            }
                        }
                        if (!beaconPresent) {
                            newRangedBeacons.add(eachRangedBeacon);
                        }
                    }

                    // arraylistIBeacon.remove(nonRangedBeacons);
                    arraylistIBeacon.addAll(newRangedBeacons);
                    info.notifyDataSetChanged();

                }
            });
        }
    });


    beaconManager.setMonitorNotifier(new MonitorNotifier() {

        @Override
        public void didExitRegion(Region region) {

            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);                 
            Toast.makeText(getApplicationContext(), "u just went out of the region - Monitor mode", Toast.LENGTH_LONG).show();

            }

        @Override
        public void didEnterRegion(Region region) {

            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); 
            Toast.makeText(getApplicationContext(), "u just enterd the region - Monitor mode", Toast.LENGTH_LONG).show();

        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "u just enterd the didDetermineStateForRegion - Monitor mode", Toast.LENGTH_LONG).show();
        }
    });

}

任何类型的帮助将不胜感激。

4

0 回答 0