How to get the AltBeacon library's BootstrapRe

2020-06-28 01:48发布

问题:

I'm using reference, http://altbeacon.github.io/android-beacon-library/samples.html . I also used How to detect Region Enter/Exit for multiple beacons using AltBeacon android-beacon-library?

I'm trying to detect iBeacons in the background using AltBeacon's Android-Beacon-Library. I included the following code snippet from my project. So far, I'm not detecting ibeacons in the background...any help is appreciated

I'm using the BeaconManager to

setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

However, I get no error and no beacon detection. When running app in debug on the Samsung Galaxy 4 device, app launches but does not detect active beacons. My beacons are Rad Beacons which are configured as iBeacons. The Rad Beacon app detects them and so does my other AltBeacon library app that runs in the foreground and detects my iBeacons. These apps are running on the Samsung Galaxy 4.

The app that I set-up for background beacon detection...does not detect the iBeacons.

Here's my code. Reference to Constants.java is just a file of constants for my app.

package com.myApp.BTleDemo;

import android.app.Application;
import android.content.Intent;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;



import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.Identifier;




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

    private BeaconManager beaconManager;
SharedPreferences prefs;
List<Region> regions;
    public void onCreate() {
        super.onCreate();
    Log.d(TAG, "App started up");


  beaconManager = BeaconManager.getInstanceForApplication(this);
  // Add AltBeacons Parser for iBeacon 
  beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));


    // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)

  Region region = new Region("com.myApp.BTleDemo.boostrapRegion", Identifier.parse(Constants.BT_UUID), 
  Identifier.fromInt(Constants.BT_MAJOR), Identifier.fromInt(Constants.BT_MINOR));

        regionBootstrap = new RegionBootstrap(this, region);


}


@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
    // Don't care
}

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

    // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
    // if you want the Activity to launch every single time beacons come into view, remove this call.  
    regionBootstrap.disable();
    Intent intent = new Intent(this, MainActivity.class);
    // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
    // created when a user launches the activity manually and it gets launched from here.
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
}

@Override
public void didExitRegion(Region arg0) {
    // Don't care
}  


/*
@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {

    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");     
        }
    }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {   }
}
}
*/
}

I'm not getting any ibeacon detection. None of the expected LogCat listings are posted. Am I missing some step?

回答1:

Edit your project.properties file and add the line:

manifestmerger.enabled=true

See full instructions here:

http://altbeacon.github.io/android-beacon-library/configure.html

The problem has nothing to do with the beacon type you are trying to recognize. Without the manifest merging enabled, you AndroidManifest.xml file doesn't have the service definition inherited from the library to start up the BeaconService, so no beacons of any kind are ever detected.

Using Eclipse, you can tell if manifest merging is working properly by doing a build, then looking at your generated manifest in bin/AndroidManifest.xml. This manifest should include the entries below.

If all else fails, you may copy these entries into your project manifest manually:

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

<!-- nest the entries below underneath your application tag -->

    <service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="beacon" android:name="org.altbeacon.beacon.service.BeaconService">
    </service>
    <service android:enabled="true" android:name="org.altbeacon.beacon.BeaconIntentProcessor">
    </service>
    <receiver android:name="org.altbeacon.beacon.startup.StartupBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
        </intent-filter>
    </receiver>


回答2:

If you aren't detecting beacons at all, make sure the library's BeaconService is running and that it notices bluetooth devices. To do this, enable debug logging by placing a line like below in the onCreate method of your application or activity:

mBeaconManager.setDebug(true);

Run your app while capturing the results of LogCat. You should look for lines like the ones below to tell you if the BeaconService is starting up successfully:

I/BeaconService( 7140): beaconService version 2.0-beta4 is starting up
I/BeaconService( 7140): binding
I/BeaconService( 7140): start ranging received
D/BeaconService( 7140): Currently ranging 1 regions.

If you see lines like this, then this means the service is starting up successfully. Then turn on your beacon and look for lines like below, which will tell you whether or not you are detecting bluetooth devices at all, and whether they are recognized as beacons:

D/BtGatt.GattService( 2006): onScanResult() - address=00:07:80:02:60:20, rssi=-65
D/BeaconService( 1772): got record
D/BeaconParser( 1772): This is a recognized beacon advertisement -- beac seen
D/BeaconService( 1772): beacon detected multiple times in scan cycle :id1: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6 id2: 1 id3: 3


D/BtGatt.GattService( 2006): onScanResult() - address=D4:CB:08:6F:DA:AD, rssi=-82
D/BeaconService( 7140): got record
D/BeaconParser( 7140): This is not a matching Beacon advertisement.  (Was expecting be ac.  The bytes I see are: 0201061aff0000001012f234454cf6d4a0fadf2f4911ba9ffa600010001b90000000000000000000000000000000000000000000000000000000000000000

The first snippet shows a successful decoding of a bluetooth device that is recognized as a beacon. The second snippet shows the detection of a bluetooth device that is NOT recognized as a beacon. If you don't see any snippets like either, then that indicates that the service is not running, or that it is not detecting bluetooth devices at all.

Once you get a LogCat capture like this, please add an excerpt of it to your question, which can help troubleshoot further.