我正在将altbeacon库更新到最新的2.9.1,但是当我为它设置范围时,我没有得到任何beacon,这是使用android 6.0.1。
public class BeaconService extends IntentService implements BeaconConsumer {
private BeaconManager mBeaconManager;
private static ArrayList<Beacon> beaconsList=new ArrayList<Beacon>();
private Region region=new Region("rid", null, null, null);
private static final String LOGTAG = "BeaconService";
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public BeaconService() {
super(Constants.BEACON_SERVICE);
}
@Override
public void onBeaconServiceConnect() {
try {
mBeaconManager.startRangingBeaconsInRegion(region);
mBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Intent localIntent =new Intent(Constants.BEACON_ACTION);
beaconsList.clear();
beaconsList.addAll(beacons);
Collections.sort(beaconsList,new Comparator<Beacon>() {
@Override
public int compare(Beacon lhs, Beacon rhs) {
return Double.compare(lhs.getDistance(), rhs.getDistance());
}
});
localIntent.putParcelableArrayListExtra(Constants.BEACON_LIST,beaconsList);
LocalBroadcastManager.getInstance(BeaconService.this).sendBroadcast(localIntent);
}
});
} catch (RemoteException e) {
Log.e(LOGTAG,"Error BeaconService",e);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
mBeaconManager.bind(this);
}
@Override
public void onDestroy() {
super.onDestroy();
mBeaconManager.unbind(this);
}
/**
* Stop Scanning
*/
public void stopRanging(){
try {
mBeaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.e(LOGTAG,"Error BeaconService - StopRanging",e);
}
}
/**
* Start Scanning
*/
public void startRanging(){
try {
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.e(LOGTAG,"Error BeaconService - StartRanging",e);
}
}
@Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
}
@Override
protected void onHandleIntent(Intent intent) {
}
public class LocalBinder extends Binder {
public BeaconService getService() {
return BeaconService.this;
}
}
我确实尝试改变监测和相同的结果,我也确实尝试添加更多的布局,但我没有得到任何信标在列表
看起来您需要在运行时添加权限,我确实通过这样做来修复它
@Override
public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
Log.i(LOGTAG, "initialize");
context = webView.getContext();
beaconServiceIntent = new Intent(context, BeaconService.class);
context.bindService(beaconServiceIntent, serviceBeaconConnection, Service.BIND_AUTO_CREATE);
BeaconReceiver beaconReciever = new BeaconReceiver();
IntentFilter intentFilter = new IntentFilter(Constants.BEACON_ACTION);
LocalBroadcastManager.getInstance(context).registerReceiver(beaconReciever, intentFilter);
mainActiviy = (Activity) context;
checkPermission();
}
@TargetApi(Build.VERSION_CODES.M)
private void checkPermission() {
if(this.mainActiviy.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
AlertDialog.Builder builder = new AlertDialog.Builder(((Activity)context));
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mainActiviy.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}