-3

My code Snippet: I have tried altbeacon gradle. I have declared Region as per below code.

I have wrote this line in onCreate method of Application class. Region region = new Region("all beacon", null, null, null);

I can get Beacon's major,minor and uuid in "didRangeBeaconsInRegion".

Problem : I didn't identify beacon in didEnterRegion() and didExitRegion() method as getting null value of major,minor and uuid.

How can i solve this issue?

user2231294
  • 153
  • 1
  • 1
  • 10

1 Answers1

0

Nothing is wrong, this is how beacon monitoring works. When you monitor, you get notified when any beacon matching a wildcard pattern defined by your Region is detected. This notification tells you that a matching beacon was detected, and provides a reference to the Region object being monitored.

In the example shown, the defined Region has null for each of the three beacon identifiers, meaning it will match any beacon. It therefore provides only a single callback when any beacon is detected and does not tell you the identifiers of what matched.

You have two choices for getting access to the identifiers:

  1. If you only care about a couple of beacons, you can monitor for multiple explicit regions like: Region region1 = new Region("first beacon", "2F234454-CF6D-4A0F- ADF2-F4911BA9FFA6", "1", " 2"); This will give you a Region object in the callback with all identifiers set.

  2. Use ranging APIs which give you a callback every second with a list of all beacons with identifiers matching the defined region.

Below is a full Activity implementation for option 1. When you run this, you will see the following in LogCat:

D/MonitoringActivity(31644): starting monitoring
D/MonitoringActivity(31644): Entered region: null null null
D/MonitoringActivity(31644): Entered region: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6 1 1

Note that the first region entry will all null identifiers is sent because the activity is also monitoring an all null region.

public class MonitoringActivity extends Activity implements BeaconConsumer, MonitorNotifier {
    protected static final String TAG = "MonitoringActivity";
    private BeaconManager mBeaconManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitoring);
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBeaconManager.bind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        try {
            mBeaconManager.setMonitorNotifier(this);
            mBeaconManager.startMonitoringBeaconsInRegion(new Region("all beacons region", null, null, null));
            mBeaconManager.startMonitoringBeaconsInRegion(new Region("region 1", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), Identifier.parse("1"), Identifier.parse("1")));
            mBeaconManager.startMonitoringBeaconsInRegion(new Region("region 2", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), Identifier.parse("1"), Identifier.parse("2")));
            Log.d(TAG, "starting monitoring");
        } catch (RemoteException e) {
            Log.e(TAG, "can't monitor", e);
        }
    }

    @Override
    public void didEnterRegion(Region region) {
        Log.d(TAG, "Entered region: "+region.getId1()+" "+region.getId2()+" "+region.getId3());
    }

    @Override
    public void didExitRegion(Region region) {
        Log.d(TAG, "Exited region: "+region.getId1()+" "+region.getId2()+" "+region.getId3());
    }

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

    }
}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • The first approach,u told its hardcoded or for predefined beacons. I have used ranging api and its method didRangeBeaconsInRegion method. I have set identifiers in didRangeBeaconsInRegion method But still get null value in didEnterRegion and didExitRegion method. – user2231294 Jun 23 '15 at 12:59
  • As you told that we have to define region for all list i have first and then i will get that information(uuid,major,minor) etc. is it right ? – user2231294 Jun 23 '15 at 13:48
  • Can you please tell me that how can i add value in List ? – user2231294 Jun 23 '15 at 14:26
  • Can you please show your code and explain exactly where you are unexpectedly getting the null identifiers? – davidgyoung Jun 23 '15 at 15:26
  • Sorry, I really need to see a bigger section of code including the callback. Can you please add this to the bottom of your question? – davidgyoung Jun 23 '15 at 15:39
  • I have added region in OnCreate() method not in callback. – user2231294 Jun 23 '15 at 15:43
  • Apologies, but I do not understand what the code is doing. If I am going to be able to help, I need you to show me a larger block of code. I need to see both the code that sets up the regions **AND** the callback code that is giving you null values. If I can't see this, I'm afraid I can't help. – davidgyoung Jun 23 '15 at 16:05
  • Now i want exactly like this: You have answered in this post http://stackoverflow.com/questions/25223385/how-to-detect-region-enter-exit-for-multiple-beacons-using-altbeacon-android-bea?rq=1 – user2231294 Jun 23 '15 at 16:15
  • Since I can't see your full code, I have augmented my answer to show what does work. Try running that and you should be able to reproduce the same results. – davidgyoung Jun 23 '15 at 17:57
  • Ok thank u so much. I got result.I want to know how can add multiple region (I mean how can add multiple identifier? ) as i have list of beacons. – user2231294 Jun 24 '15 at 06:01