简体   繁体   中英

Failing to monitor beacon regions with “altbeacon” libary android

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);
    }
}

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM