I am using Android Studio (IntelliJ) IDE for building an android app which uses AltBeacon to detect beacons. I am trying out this sample and trying to understand the basics behind it. I am running the sample of android simulator (ADT 1.1.0 and gradle 2.2.1). When I turn on the TimedBeaconSimulator
, I can see them after clicking on Start Ranging
. There are some things I noticed which are confusing a bit -
a) It always shows Id3:2
and Id3:3
, and never 1 and 4, though all of them are added to beacons list.
b) When the app is closed, it crashes, not sure why.
c) As I understand the MonitoringActivity
is there to detect beacons in background. But it is not happening. Is it not built for detecting simulated beacons (which are part of sample)? I tried adding these lines in AndroidManifest.xml
(reference), but got below error on gradle build -
Error:(35, 41) Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute service#org.altbeacon.beacon.service.BeaconService@exported value=(true) from AndroidManifest.xml:35:41
is also present at org.altbeacon:android-beacon-library:2.1.4:27:13 value=(false)
Suggestion: add 'tools:replace="android:exported"' to <service> element at AndroidManifest.xml:35:9 to override
Please help with this. I am very new to beacons and trying to grasp these concepts.
David, To get around the issue of app crashing due to interference with Android L BLE scanning, I have added this check in my code -
public boolean IsBLESupportedOnDevice(Context context) {
if (Build.VERSION.SDK_INT >= 19 && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
Log.d(TAG, "BLE is supported, so need to disable L scanning");
return true;
}
return false;
}
If it returns true, I am adding below line in the code -
beaconManager.setAndroidLScanningDisabled(true);
Can you verify if the API level and rest of the condition looks good?
A) The reason you don't see all the beacons is because the sample code only shows the first beacon in the ranging callback. If you want to see them all, change the code in the ranging callback to look like this:
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon: beacons) {
EditText editText = (EditText)RangingActivity.this
.findViewById(R.id.rangingText);
logToDisplay("I see a beacon "+beacon.toString()+" about "+beacon.getDistance()+" meters away."); }
}
B) I'm not sure what you mean by "closing the app" causing it to crash. You can hit the home button to test its operation in the background. If you use the task switcher to kill the app, then yes, that will cause it to stop running. Automatic restarts of beacon detection after the app is killed with the task switcher are very difficult to test in the emulator.
C) You don't need to add the entries to the AndroidManifest.xml
you describe. The fact that you saw any beacon detections using the TimedBeaconSimulator indicates that your manifest file is set up properly. Please remove these manually added entries. (They are only needed if automatic manifest merging fails for some reason.)
Yes, you can use a BeaconSimulator
to detect beacons in the background, but the example TimedBeaconSimulator may not be the best way to try this, because it detects beacons right away. To make this work better, change this code:
0, 10, TimeUnit.SECONDS
to:
30, 10, TimeUnit.SECONDS
Which will make it take 30 seconds to detect the first beacon. Then, launch the app in the simulator and hit the home button to put it to the background. Within 30 seconds you should see the app pop up on beacon detection.