2
我使用了altbeacon提供的參考碼,但無法檢測到任何iBeacons。以下是我的代碼:未通過altbeacon檢測到iBeacon android庫
在Manifest中包含以下權限。除此之外,我還啓用了位置服務。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
爲活動
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
onBeaconServiceConnecr試圖同時使用RangeNotifier和MoniterNotifier(在下面的代碼註釋),但都沒有工作onCreate方法。 RangeNotifier始終有一個大小爲零的集合,MoniterNotifier永遠不會被調用。
@Override
public void onBeaconServiceConnect() {
//BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
if(collection.size() > 0){
for (Beacon beacon : collection) {
Log.i("MainActivity", "I see a beacon that is about "+beacon.getDistance()+" meters away.");
}
}
}
});
/*beaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
}
});*/
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
感謝您的幫助,謝謝。