This code detects a beacon but i am not able to get an id or major or minor from it. I tried using beacon.id1() function but it always returns null. I am trying to implement this since 3 days but i am not able to figure it out i am new to beacon technology. I want to get an id from a beacon. This code detects a beacon but i am not able to get an id or major or minor from it. I tried using beacon.id1() function but it always returns null. I am trying to implement this since 3 days but i am not able to figure it out i am new to beacon technology. I want to get an id from a beacon.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.bind( this);
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
Intent eintent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
int intVal=1;
startActivityForResult(eintent, intVal);
} else {
// The toggle is disabled
BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
bAdapter.disable();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind((BeaconConsumer) this);
}
public void onBeaconServiceConnect() {
beaconManager.removeAllMonitorNotifiers();
beaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Context context = getApplicationContext();
CharSequence text = "Beacon Found";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Log.d(TAG, "I just saw a beacon for the first time!");
a=beacon.getServiceUuid();
Toast toastt = Toast.makeText(context,"" + a, duration);
toastt.show();
}
@Override
public void didExitRegion(org.altbeacon.beacon.Region region) {
Log.i(TAG, "I no longer see a beacon");
}
@Override
public void didDetermineStateForRegion(int i, org.altbeacon.beacon.Region region) {
//Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new org.altbeacon.beacon.Region("MY_UUID", null, null, null));
//beaconManager.startRangingBeaconsInRegion(new org.altbeacon.beacon.Region("myranging", null, null, null));
}
catch (RemoteException e) { }
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
Understand that using monitoring APIs, you only get a callback when any beacon (one or more) is in the vicinity that matches your Region
definition. The code shown defines a region setting all the identifiers to null. This is what is known as a wildcard Region because it matches any beacon.
When you get a callback to didEnterRegion
it passes a copy of the same Region
definition you used to start monitoring. And because you set all the identifiers to null (a wildcard definition) that is what you get when you inspect the identifiers of the Region
object passed to that method. These APIs are designed to tell you when any one of a group of beacons has appeared.
If you want to know the specific identifiers of the beacons that are visible, you simply need to use ranging APIs. Instead of:
beaconManager.startMonitoringBeaconsInRegion(new org.altbeacon.beacon.Region("MY_UUID", null, null, null));
Call:
beaconManager.startRangingBeaconsInRegion(new org.altbeacon.beacon.Region("MY_UUID", null, null, null));
And then set your callback notifier like this:
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Region region, Collection<Beacon> beacons) {
for (Beacon beacon: beacons} {
Log.d(TAG, "I see a beacon with ID1 of: "+beacon.getID1());
}
}
});