我正在尝试在活动上下文之外使用 iBeacon 库来编写其有效的实现,但我缺少一些东西,因为我没有获得所需的功能。
该服务很可能似乎没有绑定到我新创建的课程......而且我不确定我在这里缺少什么......
这是我的自定义类:
public class BeaconUtils implements IBeaconConsumer, RangeNotifier, IBeaconDataNotifier {
private Context context;
protected static final String TAG = "BeaconUtils";
public BeaconUtils(Context context) {
this.context = context;
verifyBluetooth((Activity) context);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void verifyBluetooth(final Activity activity) {
try {
if (!IBeaconManager.getInstanceForApplication(activity).checkAvailability()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Bluetooth not enabled");
builder.setMessage("Please enable bluetooth in settings and restart this application.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
activity.finish();
//System.exit(0);
}
});
builder.show();
}
} catch (RuntimeException e) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Bluetooth LE not available");
builder.setMessage("Sorry, this device does not support Bluetooth LE.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
activity.finish();
//System.exit(0);
}
});
builder.show();
}
}
@Override
public void onIBeaconServiceConnect() {
Region region = new Region("MainActivityRanging", null, null, null);
try {
ZonizApplication.iBeaconManager.startMonitoringBeaconsInRegion(region);
ZonizApplication.iBeaconManager.setRangeNotifier(this);
ZonizApplication.iBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
ZonizApplication.iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
//createNotification();
//Log.i(TAG, "I am in the range of an IBEACON: "+region.getProximityUuid());
//SyncServiceHelper.getInst().trySyncOffers(region.getProximityUuid());
}
@Override
public void didExitRegion(Region region) {
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(0);
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing iBeacons: " + region.getProximityUuid());
createNotification();
}
});
}
@Override
public Context getApplicationContext() {
return this.context;
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
ZonizApplication.iBeaconManager.unBind(this);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
ZonizApplication.iBeaconManager.bind(this);
return true;
}
@Override
public void iBeaconDataUpdate(IBeacon iBeacon, IBeaconData iBeaconData, DataProviderException e) {
if (e != null) {
Log.d(TAG, "data fetch error:" + e);
}
if (iBeaconData != null) {
String displayString = iBeacon.getProximityUuid() + " " + iBeacon.getMajor() + " " + iBeacon.getMinor() + "\n" + "Welcome message:" + iBeaconData.get("welcomeMessage");
Log.d(TAG, displayString);
}
}
@Override
public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
for (IBeacon iBeacon : iBeacons) {
iBeacon.requestData(this);
String displayString = iBeacon.getProximityUuid() + " " + iBeacon.getMajor() + " " + iBeacon.getMinor() + "\n";
Log.d(TAG, displayString);
}
}
public void createNotification() {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Build notification
// Actions are just fake
//if (currentUIID != null && !currentUIID.isEmpty()) {
Notification noti = new Notification.Builder(context)
.setContentTitle("New beacon in range")
.setContentText("You are currently in the range of a new beacon.").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, noti);
//}
}
}
我在我的应用程序类中实例化信标管理器:
iBeaconManager = IBeaconManager.getInstanceForApplication(this);
我在onCreate()
andonDestroy()
方法中的活动中绑定了这个经理。
我错过了什么?
我在这样的活动中实例化我的自定义类:
private BeaconUtils beaconUtilities = new BeaconUtils(MainActivity.this);
装订部分:
beaconUtilities = new BeaconUtils(MainActivity.this);
ZonizApplication.iBeaconManager.bind(beaconUtilities);