Android-AltBeacon 库集成 java class
Android-AltBeacon-library intergration in a java class
我正在尝试将 altbeacon 功能集成到 android studio 的 java class 中,但由于 getActivity 而出现错误。我想在 onahter 活动中从这个 class 创建一个对象..
知道它是如何工作的吗?
当我在 protected void onCreate(Bundle savedInstanceState)
.
下的 activity 中添加 altbeacon class 时,效果非常好
public class detectRoom implements BeaconConsumer {
private List <IBeaconSensor> beaconList = new ArrayList <IBeaconSensor> ();
private BeaconManager beaconManager;
public detectRoom() {
name="detectRoom";
}
private String detectRoomName(String raum) {
return raum;
}
public void detectRoomMet () {
for (int i = 0;i< beaconList.size() ;i++){
if(beaconList.get(i).getName().equals("45")) { // 6 = Minor of Ibeacon
detectRoomName("Room3");
}
if(beaconList.get(i).getName().equals("55")) {
detectRoomName("Room2");
}
if(beaconList.get(i).getName().equals("85")) {
detectRoomName("Room1");
}
else {
detectRoomName("UnknowRoom");
}
}
}
@Override
public void onBeaconServiceConnect() {
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
this.beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
beaconList.clear();
for(Iterator<Beacon> iterator = beacons.iterator(); iterator.hasNext();) {
beaconList.add(new IBeaconSensor (iterator.next().getId3().toString()));
}
}
}
});
try {
this.beaconManager.startRangingBeaconsInRegion(new Region("MyRegionId", null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public Context getApplicationContext() {
return null;
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
this.beaconManager.unbind(this);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
public void bindBeacon() {
beaconManager.bind(this);
}
public void unBindBeacon() {
beaconManager.unbind(this);
}
}
制作扩展 BeaconConsumer 的 POJO 时,您必须做两件事:
- 将对 Android 上下文的引用传递给 POJO。
- 将方法 bindService、unbindService、getApplicationContext 链接到上面的 Context。
像这样:
public class Pojo extends BeaconConsumer() {
private Context mContext;
public Pojo(Context context) {
mContext = context;
}
@Override
public Context getApplicationContext() {
return mContext.getApplicationContext();
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
mContext.unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return mContext.bindService(intent, serviceConnection, i);
}
...
}
我正在尝试将 altbeacon 功能集成到 android studio 的 java class 中,但由于 getActivity 而出现错误。我想在 onahter 活动中从这个 class 创建一个对象..
知道它是如何工作的吗?
当我在 protected void onCreate(Bundle savedInstanceState)
.
public class detectRoom implements BeaconConsumer {
private List <IBeaconSensor> beaconList = new ArrayList <IBeaconSensor> ();
private BeaconManager beaconManager;
public detectRoom() {
name="detectRoom";
}
private String detectRoomName(String raum) {
return raum;
}
public void detectRoomMet () {
for (int i = 0;i< beaconList.size() ;i++){
if(beaconList.get(i).getName().equals("45")) { // 6 = Minor of Ibeacon
detectRoomName("Room3");
}
if(beaconList.get(i).getName().equals("55")) {
detectRoomName("Room2");
}
if(beaconList.get(i).getName().equals("85")) {
detectRoomName("Room1");
}
else {
detectRoomName("UnknowRoom");
}
}
}
@Override
public void onBeaconServiceConnect() {
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
this.beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
beaconList.clear();
for(Iterator<Beacon> iterator = beacons.iterator(); iterator.hasNext();) {
beaconList.add(new IBeaconSensor (iterator.next().getId3().toString()));
}
}
}
});
try {
this.beaconManager.startRangingBeaconsInRegion(new Region("MyRegionId", null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public Context getApplicationContext() {
return null;
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
this.beaconManager.unbind(this);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
public void bindBeacon() {
beaconManager.bind(this);
}
public void unBindBeacon() {
beaconManager.unbind(this);
}
}
制作扩展 BeaconConsumer 的 POJO 时,您必须做两件事:
- 将对 Android 上下文的引用传递给 POJO。
- 将方法 bindService、unbindService、getApplicationContext 链接到上面的 Context。
像这样:
public class Pojo extends BeaconConsumer() {
private Context mContext;
public Pojo(Context context) {
mContext = context;
}
@Override
public Context getApplicationContext() {
return mContext.getApplicationContext();
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
mContext.unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return mContext.bindService(intent, serviceConnection, i);
}
...
}