Beacons not detected in Android service

一笑奈何 提交于 2020-01-03 04:25:13

问题


I'm using the Altbeacon library (stable release 2.1.4) to detect beacons. If I do it in an Activity, I have no problems detecting them. However I can't get this to work from a service. Here's what I've got:

package com.ibeacontest.android;


import java.util.Collection;

import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;


public class TestBestzBeaconService extends Service implements BeaconConsumer
{

    private BeaconManager beaconManager;
    private final String BEACON_UUID = "11687109-915f-4136-a1f8-e60ff514f96d";
    private final int BEACON_MAJOR = 3;

    @Override
    public void onCreate() {
        super.onCreate();
        L.p("In TestBestzBeaconService onCreate()");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {
        L.p("in TestBestzBeaconService onStartCommand()");

        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.bind(this);

        //iBeacons ?
        BeaconParser bp0 = new BeaconParser();
        bp0.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
        beaconManager.getBeaconParsers().add(bp0);

        //Bluecats?
        BeaconParser bp1 = new BeaconParser();
        bp1.setBeaconLayout("m:2-3=0201,i:28-29,p:24-24");
        beaconManager.getBeaconParsers().add(bp1);

        return super.onStartCommand(intent, flags, startId);    
    }

    @Override
    public void onDestroy() {
        L.p("In TestBestzBeaconService onDestroy()");
        beaconManager.unbind(this);
    }


    @Override
    public void onBeaconServiceConnect() {

        L.p("In TestBestzBeaconService onBeaconServiceConnect()");

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
                L.p("In TestBestzBeaconService - anonymous didRangeBeaconsInRegion()");
            }
        });

        Region region = new Region("myregion", Identifier.parse(BEACON_UUID), Identifier.fromInt(BEACON_MAJOR), null); //

        try {
            beaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            L.p("In TestBestzBeaconService onBeaconServiceConnect(), REMOTEEXCEPTION!");
        }

    }

    private static class L
    {
        public static void p(String s) {
            Log.i("beacon", s);
        }
    }

}

I'm calling this from an Activity as so:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, TestBestzBeaconService.class));
    }

}

The log output I'm getting is:

03-19 09:56:40.233: I/beacon(25210): In TestBestzBeaconService onCreate()
03-19 09:56:40.233: I/beacon(25210): in TestBestzBeaconService onStartCommand()
03-19 09:56:40.566: I/beacon(25210): In TestBestzBeaconService onBeaconServiceConnect()

Parts added to the AndroidManifest:

<!-- Needed for AltBeacon SDK -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

and inside the Application tag:

<!-- Needed for AltBeacon SDK -->
<service android:name="org.altbeacon.beacon.service.BeaconService"/>
<service android:name="com.ibeacontest.android.TestBestzBeaconService" />

...but no signs of beacons or the didRangeBeaconsInRegion log output. Any pointers?


回答1:


As i know Monitoring only works in background while Ranging do not. Try firstly starting a Monitoring and then immediately when you enter Monitoring start Ranging.

Something like:

  //Set Monitoring
mBeaconManager.setMonitorNotifier(new

MonitorNotifier() {
    @Override
    public void didEnterRegion (Region region){
        Log.d("TEST", "ENTERED beacon region");
        //Start Raning as soon as you detect a beacon
        try {
            mBeaconManager.startRangingBeaconsInRegion(mRegion);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
});

//Set Ranging
mBeaconManager.setRangeNotifier(new

RangeNotifier() {
    @Override
    public void didRangeBeaconsInRegion ( final Collection<Beacon> beacons, Region region){
           if (beacons.size() > 0) {
                Log.i(TAG, p("In TestBestzBeaconService - anonymous    
            }
    }
});

try
{
    //Start Monitoring
    mBeaconManager.startMonitoringBeaconsInRegion(mRegion);
}

catch(RemoteException e)
{
    e.printStackTrace();
}

Don't forget to add beacon service to Manifest:

        <service
        android:name="org.altbeacon.beacon.service.BeaconService"
        android:enabled="true"
        android:exported="true"
        android:isolatedProcess="false"
        android:label="beacon"></service>
       <service
        android:name="org.altbeacon.beacon.BeaconIntentProcessor"
        android:enabled="true"></service>

note: In meantime check also lib project




回答2:


The posted code is in a library project and contained the correct inserts in the AndroidManifest file, however the actual app project was missing this.

I believe normally this would cause the app to crash, but in this case it just wouldn't show beacons.

Adding this made it work.



来源:https://stackoverflow.com/questions/29148007/beacons-not-detected-in-android-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!