It is the first time I work with iBeacon. So can you tell me how to detect it(give me some code example). Thanks very much. It is very important to me.
1 Answer
The open source Android iBeacon Library will allow you to do this.
Here is a basic code sample:
public class MonitoringActivity extends Activity implements IBeaconConsumer {
protected static final String TAG = "RangingActivity";
private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
iBeaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
iBeaconManager.unBind(this);
}
@Override
public void onIBeaconServiceConnect() {
iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an iBeacon for the firt time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an iBeacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing iBeacons: "+state);
}
});
try {
iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) { }
}
}
Full disclosure: I am Chief Engineer for Radius Networks and author of the library.
-
Hi @davidgyoung I write a class not extend activity as your example. But it can not run onIBeaconServiceConnect(). Can you tell me the reason. I overwrite some menthod of IBeaconConsumer as: Commented Apr 20, 2014 at 6:12
-
Hi @davidgyoung can you show me how to write a class implements IBeaconConsumer without activity Thanks very much. Here is my code app.box.com/s/340hm5hp257n464lsg8f. I don not know why onIBeaconServiceConnect() does not run. Thanks you Commented Apr 20, 2014 at 14:00
-
In the code you shared, I do not see any call to iBeaconManager.bind(...) which is necessary to get a callback to onIBeaconServiceConnect(). If you need more help, please post a new question that includes your code inline, that way everybody else can see it, too and be able to offer their answers. Commented Apr 21, 2014 at 3:55
-
Thanks you. I got over this problem. Thanks for your help! Commented Apr 21, 2014 at 8:23
-
Hello sir I made a similar code as it is above by onIBeaconServiceConnect() is not called in my activity but i am able to run the demo app without any problem. What i would have missed? any suggestions? Commented May 13, 2014 at 6:38