我使用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
日志输出的迹象。有什么指针吗?
答案 0 :(得分:0)
据我所知Monitoring
仅适用于后台,Ranging
则不适用。首先尝试启动Monitoring
,然后在您输入Monitoring
开始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 service
添加到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>
注意:同时请检查
lib project
答案 1 :(得分:0)
发布的代码位于库项目中,并在AndroidManifest
文件中包含正确的插入内容,但实际的应用程序项目缺少此内容。
我认为这通常会导致应用程序崩溃,但在这种情况下它不会显示信标。
添加此功能使其正常工作。