I’ve been able to update the source code to use the 1.0.3 APIs, but although the application compiles and installs, it just doesn’t work (i.e. onEnteredRegion never fires). Yet it works fine using the 0.9.4 SDK.
I wrote a little introduction for using monitoring technology in a foreground activity:
create an instance of the BeaconManager class,
set a listener for monitoring to this instance,
setup the region you want to monitor,
call the startMonitoring method every time the activity comes to the foreground,
stop the monitoring action when the activity goes to the background,
disconnect from the service when the activity is killed.
[details=Example (Java code):]
public class MyMonitoringActivity {
private BeaconManager mBeaconManager;
private BeaconRegion mBeaconRegion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.
mBeaconManager = new BeaconManager(this);
// 2.
mBeaconManager.setMonitoringListener(new BeaconManager.BeaconMonitoringListener {
@Override
public void onEnteredRegion(BeaconRegion region, List<Beacon> list) {
// Handle the event here.
}
@Override
public void onExitedRegion(BeaconRegion region) {
// Handle the event here.
}
});
// 3.
mBeaconRegion = new BeaconRegion(
mIdentifier,
mUuid,
mMajor,
mMinor);
}
@Override
protected void onResume() {
super.onResume();
// 4.
SystemRequirementsChecker.checkWithDefaultDialogs(this);
mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
mBeaconManager.startMonitoring(mBeaconRegion);
}
});
}
@Override
protected void onPause() {
// 5.
mBeaconManager.stopMonitoring(mBeaconRegion);
}
@Override
protected void onDestroy() {
// 6.
mBeaconManager.disconnect();
super.onStop();
}
}
```[/details]----------
[details=Tips:]
* the instance of `BeaconManager` cannot be a **local variable** because you need it in the different methods of your activity.
* you can choose to override only **one of the two** callback methods `onEnteredRegion()` or `onExitedRegion()`,
* you can use the **`Log`** class in the overrided callback methods to verify the event quickly: `Log.i("MyMonitoringActivity", "Region entered!");`
* in the example code, `myIdentifier` is a `String` class instance, `myUuid` a **`UUID`** class instance and `mMajor` and `mMinor` are integers.
* you can easily transform a `String` to a `UUID` with the **`UUID.fromString()`** method.
* The _monitoring_ (like the _ranging_) feature can work on **different regions**:
`Region(myIdentifier, myUuid, myMajor, myMinor)` to range the -unique- beacon with defined UUID/major/minor,
`Region(myIdentifier, myuuid, myMajor, null)` to range all the beacons with defined UUID/major,
`Region(myIdentifier, myuuid, null, null)` to range all the beacons with the defined UUID,
`Region(myIdentifier, null, null, null)` to range all the beacons.
* do not forget to check the **permissions** with the `SystemRequirementsChecker.checkWithDefaultDialogs()` method.
* the manager has to be connected to the Estimote **service** with the `BeaconManager.connect()` method; the first time it will connect, and the other times it will start monitoring directly.
* in this example, we stop the _monitoring_ task on the onPause() method, but it can work in **background** (not the _ranging_!).
* don't forget to **disconnect** the manager from the service when you kill the activity.[/details]----------
Hope it works, please notice me if I did something wrong ;)