0

我正在使用 Altbeacon 库(稳定版 2.1.4)来检测信标。如果我在 Activity 中执行此操作,则检测它们没有问题。但是,我无法通过服务来实现这一点。这是我所拥有的:

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

}

我从一个活动中这样调用它:

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

}

我得到的日志输出是:

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()

零件添加到AndroidManifest

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

并在Application标签内:

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

...但没有信标或didRangeBeaconsInRegion日志输出的迹象。任何指针?

4

2 回答 2

0

发布的代码在一个库项目中,并在AndroidManifest文件中包含正确的插入,但是实际的应用程序项目缺少这个。

我相信通常这会导致应用程序崩溃,但在这种情况下,它不会显示信标。

添加它使其工作。

于 2015-03-19T18:45:03.467 回答
0

据我所知Monitoring,只在后台工作,而Ranging不是。首先尝试启动 a Monitoring,然后在输入Monitoringstart时立即尝试Ranging

就像是:

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

不要忘记添加beacon serviceManifest

        <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>

注意:同时检查lib project

于 2015-03-19T15:30:53.290 回答