1

I am run callbacks for changes in the state of my region with the altbeacon libary. Sample code from here ("Starting an App in the Background" section). But after trying everything I can come up with, still no luck. What am I missing?

All im getting in my App is:

  • RegionApp: App started up
  • RegionApp: Got a didDetermineStateForRegion call, isInRegion: false

Details:

  • Tried using the latest version of the libary org.altbeacon:android-beacon-library:2+ and an older one 'org.altbeacon:android-beacon-library:2.12.1'
  • 3 Kontakt.io Beacons with IBeacon protocol. (firmware v4)
  • Attempted different beaconLayout, im quite sure this one is right.
  • Permissions are granted by going to app settings. Like this
  • compileSdkVersion: 28

EDIT: With the ranging example code I can't see the beacons either. But with the Kontakt app I can. One of the beacons according to BeaconScope:

f7826da6-4fa2-4e98-8024-bc5b71e0893e
id2: 29737
id3: 24354
power: -77 dBm
distance: 0.9 meters
rssi -65dBm
average rssi: -76.5 dBm
packets: 78
packes/sec: 1.4
detection rate: 100%
stabilized: true (sometimes false)
sample period: 53.9 secs

Attempted on

  • Nokia 6.1 / Android 9
  • Samsung Note 9 / Android 9
  • Huawei GRA-L09 / Android 6

Code:

App.java

public class App extends Application implements BootstrapNotifier {
    private static final String TAG = "RegionApp";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager 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"));
        // Also tried this: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25;

        Region region = new Region("com.example.regionmonitor.boostrapRegion", null, null, null);
        // Also tried creating region with defining UUID, and changing uniqueId

        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        Boolean isInRegion = arg0 == 1;
        Log.d(TAG, "Got a didDetermineStateForRegion call, isInRegion: " + isInRegion.toString());
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
    }

    @Override
    public void didExitRegion(Region arg0) {
        Log.d(TAG, "Got a didExitRegion call");
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.regionmonitor"
    >

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:name="com.example.regionmonitor.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >

        <!-- Note:  the singleInstance below is important to keep two copies of your activity from getting launched on automatic startup -->
        <activity
            android:launchMode="singleInstance"
            android:name="com.example.regionmonitor.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

MainActivity.java Should be irrelevant but for the sake of sharing all code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
joeydebreuk
  • 376
  • 2
  • 8
  • Can you see your beacon with an off-the-shelf beacon locator tool like [BeaconScope](https://play.google.com/store/apps/details?id=com.davidgyoungtech.beaconscanner&hl=en_CA)? What does it say it sees? – davidgyoung Aug 14 '19 at 17:03
  • Yes I can see the beacons with my device using the Kontakt.io app. If I try ranging with the altbeacon sample code I don't see them. – joeydebreuk Aug 15 '19 at 09:44
  • Beaconscope: f7826da6-4fa2-4e98-8024-bc5b71e0893e id2: 29737 id3: 24354 power: -77 dBm Reception Statistics distance: 0.9 meters rssi -65dBm average rssi: -76.5 dBm packets: 78 packes/sec: 1.4 detection rate: 100% stabilized: true (sometimes false) sample period: 53.9 secs – joeydebreuk Aug 15 '19 at 13:06

2 Answers2

0

On Android 6+, it is no longer sufficient to put location permission in the manifest. You also have to request permission from the user dynamically. Until the user grants permission, beacon detections (and all bluetooth LE scanning) is blocked.

See here for instructions: https://altbeacon.github.io/android-beacon-library/requesting_permission.html

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I had tried that, but for the sake of simplicity I excluded it in this repo. Instead I set permissions by going to app settings. Shouldn't that be enough? I wen't trough my logs and I did not find anything related to permissions - I also assume that there would be some kind of warning there if this were the problem. See screenshot: https://imgur.com/GnQdG8z (I'll also include this info in the question as it should have been there in the first place) – joeydebreuk Aug 16 '19 at 14:35
  • Gave it a try, not setting permissions indeed logs an exception. Yet "didDetermineStateForRegion" is still called with arg1 being 0. – joeydebreuk Aug 16 '19 at 14:54
0

The posted code works for Kontakt beacons (and IBeacons), but you need to be patient. If you wait long enough the callbacks are called as expected, but it might take a while.

If you are testing on Nokia (like me) you might want to read this.

joeydebreuk
  • 376
  • 2
  • 8