AltBeacon扫描背景中的所有信标

时间:2016-10-29 11:38:46

标签: android altbeacon

我正在使用Altbeacon开发一个应用程序,我对测距信标有问题。事实上,我想要所有信标的范围。所以我必须创建一个这样的区域

Region region = new Region("backgroundRegion",
        null, null, null);

但问题是我无法找到是否添加了新的信标。事实上,我想扫描信标,当我走到不到1米的距离时,我会向我的活动发送通知。 这是我的Application课程

public class App extends Application implements      BootstrapNotifier,RangeNotifier,BeaconConsumer {

    private static final String TAG = "BeaconReferenceApp";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private boolean haveDetectedBeaconsSinceBoot = false;
    private bootomsheetcontent main=null;
    private BeaconManager mBeaconManager;
    RangeBeaconsInRegion ranged;
    public void onCreate() {
        super.onCreate();


        TypefaceUtil.setDefaultFont(this, "DEFAULT", "fonts/ir.ttf");
        TypefaceUtil.setDefaultFont(this, "MONOSPACE", "fonts/ir.ttf");
        TypefaceUtil.setDefaultFont(this, "SERIF", "fonts/ir.ttf");
        TypefaceUtil.setDefaultFont(this, "SANS_SERIF", "fonts/ir.ttf");




        mBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
        mBeaconManager.getBeaconParsers().clear();
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15"));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v"));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("s:0-1=fed8,m:2-2=00,p:3-3:-41,i:4-21v"));
        Log.d(TAG, "setting up background monitoring for beacons and power saving");
        // wake up the app when a beacon is seen
        Region region = new Region("backgroundRegion",
                null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);

        // simply constructing this class and holding a reference to it in your custom Application
        // class will automatically cause the BeaconLibrary to save battery whenever the application
        // is not visible.  This reduces bluetooth power usage by about 60%
        backgroundPowerSaver = new BackgroundPowerSaver(this);
        mBeaconManager.bind(this);

    }

    public void setHandlerListener(RangeBeaconsInRegion listener)
    {
        ranged=listener;
    }
    @Override
    public void didEnterRegion(Region region) {
        // In this example, this class sends a notification to the user whenever a Beacon
        // matching a Region (defined above) are first seen.
        sendNotification();
        try {
            mBeaconManager.startRangingBeaconsInRegion(region);
        }
        catch (RemoteException e) {
            Log.d(TAG, "Can't start ranging"+e.getMessage());
        }

        if (!haveDetectedBeaconsSinceBoot) {
            Log.d(TAG, "auto launching MainActivity");

            // The very first time since boot that we detect an beacon, we launch the
            // MainActivity
            Intent intent = new Intent(this, bootomsheetcontent.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Important:  make sure to add android:launchMode="singleInstance" in the manifest
            // to keep multiple copies of this activity from getting created if the user has
            // already manually launched the app.
            this.startActivity(intent);
            haveDetectedBeaconsSinceBoot = true;
        } else {
            if (main != null) {
                // If the Monitoring Activity is visible, we log info about the beacons we have
                // seen on its display
                Log.d(TAG, "I see a beacon again" );
            } else {
                // If we have already seen beacons before, but the monitoring activity is not in
                // the foreground, we send a notification to the user on subsequent detections.
                Log.d(TAG, "Sending notification.");
                sendNotification();
            }
        }
    }

    @Override
    public void didExitRegion(Region region) {
        beaconsSeen.clear();
        try {
            mBeaconManager.stopRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void didDetermineStateForRegion(int i, Region region) {

    }

    List<Beacon> AvailableBeacons=new ArrayList<Beacon>() ;
    private HashSet<Beacon> beaconsSeen = new HashSet<Beacon>();
    Region region;
    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

        if(ranged!=null){
            ranged.RangeBeaconsInRegion(beacons,region,beaconsSeen);

        }
        if (beacons.size() > 0) {
            for (Beacon beacon : beacons) {

                if (!beaconsSeen.contains(beacon)) {
                    beaconsSeen.add(beacon);
                }

            }

            if(ranged!=null){
                ranged.RangeBeaconsInRegion(beacons,region,beaconsSeen);

            }
        }
    }


    private void sendNotification() {

        Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.b1);
        Bitmap bigicon = BitmapFactory.decodeResource(getResources(), R.mipmap.b1);
        // BigPictureStyle
        NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();
        s.bigPicture(bigicon);


        // Base notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_menu_send);
        builder.setContentTitle(getString(R.string.apptitlenotification));
        builder.setContentText(getString(R.string.nearbynotificatio));
        builder.setTicker(getString(R.string.apptitlenotification));
        builder.setWhen(System.currentTimeMillis());
      //  b.setDeleteIntent(getDismissNotificationsPendingIntent(quantity));
        builder.setLargeIcon(bigicon);
        builder.setStyle(s);
        //Vibration
        builder.setVibrate(new long[] { 100, 100, 100, 100, 100 });

        //LED
        builder.setLights(Color.BLUE, 3000, 3000);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(alarmSound);
       // builder.setSound(Notification.DEFAULT_SOUND);
        builder.build();
//        NotificationCompat.Builder builder =
//                new NotificationCompat.Builder(this)
//                        .setContentTitle(getString(R.string.apptitlenotification))
//                        .setContentText(getString(R.string.nearbynotificatio))
//                        .setSmallIcon(R.drawable.ic_menu_send)
////                        .setLargeIcon(bigicon)
////                        .setStyle(s)
//                        .build();
      //  notif.flags |= Notification.FLAG_AUTO_CANCEL;;

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(new Intent(this, bootomsheetcontent.class));
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }

    @Override
    public void onBeaconServiceConnect() {
        mBeaconManager.addRangeNotifier(this);
    }
}

上述代码仅在我打开信标一次并且不再工作时才有效。似乎didRangeBeaconsInRegion只针对每个区域发射一次,那么我怎样才能在背景中连续发射信标呢?感谢

这就是我在我的活动中获得回调的方式

 App myApplication = (App) getApplication();
    myApplication.ranged = this;

implimentedRangeBeaconsInRegion,这也是处理RangeBeaconsInRegion的函数

 @Override
public void RangeBeaconsInRegion(Collection<Beacon> beacons, Region region, HashSet<Beacon> beaconsSeen) {
    Toast.makeText(this.mContext,"test",Toast.LENGTH_LONG).show();
}

0 个答案:

没有答案