Android 服务中未检测到信标
Beacons not detected in Android service
我正在使用 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);
}
}
}
我是这样从 Activity 调用它的:
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
日志输出。有什么指点吗?
据我所知,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>
note: In meantime check also lib project
发布的代码在一个库项目中,并包含在 AndroidManifest
文件中的正确插入,但是实际的应用程序项目缺少这个。
我认为通常这会导致应用程序崩溃,但在这种情况下它不会显示信标。
添加这个使其工作。
我正在使用 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);
}
}
}
我是这样从 Activity 调用它的:
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
日志输出。有什么指点吗?
据我所知,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>
note: In meantime check also
lib project
发布的代码在一个库项目中,并包含在 AndroidManifest
文件中的正确插入,但是实际的应用程序项目缺少这个。
我认为通常这会导致应用程序崩溃,但在这种情况下它不会显示信标。
添加这个使其工作。