0

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.

Edit

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?

Community
  • 1
  • 1
Sam
  • 4,302
  • 12
  • 40
  • 74

1 Answers1

0

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.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I got my head around point (a) now. I am still facing issues with app shutting down on clicking home button or using task switcher. The error is "Attempt to invoke virtual method 'android.bluetooth.le.BluetoothLeScanner android.bluetooth.BluetoothAdapter.getBluetoothLeScanner()' on a null object reference". I am not sure if this is only on the simulator. – Sam Apr 13 '15 at 01:33
  • Also, I restored the androidmanifest and changed delay in timedbeacon from 0 to 30, but even though monitoringactivity is getting invoked after 30 sec automatically when app is already opened, but doesn't when the app is in background. It can be due to the error I am getting above. – Sam Apr 13 '15 at 01:33
  • Try adding `beaconManager.setAndroidLScanningDisabled(true);` in the onCreate method of `BeaconReferenceApplication.java` right after the line `BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);`. This should solve the crash problem, which is caused by an incompatibility with using the BeaconSimulator when the Android 5 APIs are active. Meanwhile, I have opened an issue to fix this incompatibility here: https://github.com/AltBeacon/android-beacon-library/issues/165 – davidgyoung Apr 13 '15 at 03:07
  • This works. One question though - Will it work if I am targetting API < 21. Do I need to check the platform the app is running on and then add the condition or it does so automatically? – Sam Apr 13 '15 at 10:59
  • The library works fine with API 19+. If targeting devices with earlier Android versions, you need to check this version before making API calls. See here for details: http://altbeacon.github.io/android-beacon-library/backward-compatibility.html – davidgyoung Apr 13 '15 at 11:08
  • Can you please check the Edit in my original post. – Sam Apr 20 '15 at 19:53
  • Generally, it's better to post new questions in cases like this. For the sake of brevity, I will say the code is largely unnecessary -- you can safely make the call to disable Android L scanning without any checks! But if you do want to include code like that, you should know that Android L starts with API level 20, not 19. – davidgyoung Apr 20 '15 at 20:56
  • Sure, just added the API level as an additional check just to make sure there are no surprises. Thanks a lot again. – Sam Apr 20 '15 at 21:47