如何在 android 中使用服务实现 BeaconConsumer
How to implement BeaconConsumer with service in android
我将 BeaconConsumer 与 Activity 一起使用,它工作正常。当我用服务实现 BeaconConsumer 时,它不起作用。
SimpleService extends Service implements BeaconConsumer {
protected static final String TAG = "MyBeaconService";
String UUID = "EXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
private BeaconManager mBeaconManager;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBeaconManager.setDebug(true);
mBeaconManager.bind(this);
Log.e("", "onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart called");
mBeaconManager
.getBeaconParsers()
.add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
mBeaconManager.bind(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand called");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroyed ...");
mBeaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
Log.e("", "onBeaconServiceConnect");
mBeaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
try {
Log.e(TAG, "didEnterRegion");
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didExitRegion(Region region) {
try {
Log.e(TAG, "didExitRegion");
mBeaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
});
mBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
Region region) {
for (Beacon oneBeacon : beacons) {
Log.e("", "setRangeNotifier");
Log.e("", "ID 1" + oneBeacon.getId1());
Log.e("", "ID 2" + oneBeacon.getId2());
Log.e("", "ID 3" + oneBeacon.getId3());
Log.e(TAG, "distance: " + oneBeacon.getDistance());
Log.e("",
"oneBeacon.getBluetoothAddress() :- "
+ oneBeacon.getBluetoothAddress());
}
}
});
try {
mBeaconManager.startMonitoringBeaconsInRegion(new Region(
"myBeaons", Identifier.parse(UUID), null, null));
mBeaconManager.startRangingBeaconsInRegion(new Region("myBeaons",
Identifier.parse(UUID), null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
这行不通。它没有提供信标的详细信息,而是在 logcat 中显示 -
Logcat留言
04-09 11:07:40.053: D/BeaconParser(8765): This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 0201061aff4c000215e2c56db5dffb48d2b060d0f5a71096e000000000c5020a000816f0ff640000000011094d696e69426561636f6e5f30303933370000
04-09 11:07:40.648: D/BluetoothAdapter(8765): onScanResult() - Device=F2:75:FA:D7:6E:19 RSSI=-45
那么,我如何将 BeaconConsumer class 与服务一起使用,以便在应用程序关闭或未关闭时获得信标的所有详细信息。请帮忙
尝试在 onCreate
方法中添加 setBeaconLayout
行。 根据您启动服务的方式,可能不会调用 onStart
方法。 onCreate
方法总是被调用。
您可能还希望在这些方法中添加一些日志记录,以便您在故障排除期间充分了解服务的启动顺序。
我将 BeaconConsumer 与 Activity 一起使用,它工作正常。当我用服务实现 BeaconConsumer 时,它不起作用。
SimpleService extends Service implements BeaconConsumer {
protected static final String TAG = "MyBeaconService";
String UUID = "EXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
private BeaconManager mBeaconManager;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBeaconManager.setDebug(true);
mBeaconManager.bind(this);
Log.e("", "onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart called");
mBeaconManager
.getBeaconParsers()
.add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
mBeaconManager.bind(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand called");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroyed ...");
mBeaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
Log.e("", "onBeaconServiceConnect");
mBeaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
try {
Log.e(TAG, "didEnterRegion");
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didExitRegion(Region region) {
try {
Log.e(TAG, "didExitRegion");
mBeaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
});
mBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
Region region) {
for (Beacon oneBeacon : beacons) {
Log.e("", "setRangeNotifier");
Log.e("", "ID 1" + oneBeacon.getId1());
Log.e("", "ID 2" + oneBeacon.getId2());
Log.e("", "ID 3" + oneBeacon.getId3());
Log.e(TAG, "distance: " + oneBeacon.getDistance());
Log.e("",
"oneBeacon.getBluetoothAddress() :- "
+ oneBeacon.getBluetoothAddress());
}
}
});
try {
mBeaconManager.startMonitoringBeaconsInRegion(new Region(
"myBeaons", Identifier.parse(UUID), null, null));
mBeaconManager.startRangingBeaconsInRegion(new Region("myBeaons",
Identifier.parse(UUID), null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
这行不通。它没有提供信标的详细信息,而是在 logcat 中显示 - Logcat留言
04-09 11:07:40.053: D/BeaconParser(8765): This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 0201061aff4c000215e2c56db5dffb48d2b060d0f5a71096e000000000c5020a000816f0ff640000000011094d696e69426561636f6e5f30303933370000 04-09 11:07:40.648: D/BluetoothAdapter(8765): onScanResult() - Device=F2:75:FA:D7:6E:19 RSSI=-45
那么,我如何将 BeaconConsumer class 与服务一起使用,以便在应用程序关闭或未关闭时获得信标的所有详细信息。请帮忙
尝试在 onCreate
方法中添加 setBeaconLayout
行。 根据您启动服务的方式,可能不会调用 onStart
方法。 onCreate
方法总是被调用。
您可能还希望在这些方法中添加一些日志记录,以便您在故障排除期间充分了解服务的启动顺序。