altbeacon android库未检测到iBeacons

altbeacon android库未检测到iBeacons,android,ibeacon,ibeacon-android,altbeacon,android-ibeacon,Android,Ibeacon,Ibeacon Android,Altbeacon,Android Ibeacon,我使用了altbeacon提供的参考代码,但无法检测到任何iBeacon。以下是我的代码: 在清单中包含以下权限。除此之外,我还启用了定位服务 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android

我使用了altbeacon提供的参考代码,但无法检测到任何iBeacon。以下是我的代码:

在清单中包含以下权限。除此之外,我还启用了定位服务

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
OnBeaConserviceConnerc尝试同时使用RangeNotifier和MonitorNotifier(在下面的代码中进行了注释),但两者都不起作用。RangeNotifier始终具有大小为零的集合,并且从不调用MonitorNotifier

@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) {   }
}
@覆盖
beacerviceconnect()上的公共无效{
//BeaconManager BeaconManager=BeaconManager.getInstanceForApplication(此应用程序);
beaconManager.addRangeNotifier(新的RangeNotifier(){
@凌驾
公共无效DidRangeBeanConsignRegion(集合集合,区域){
if(collection.size()>0){
用于(信标:集合){
Log.i(“MainActivity”,“我看到一个信标大约在“+beacon.getDistance()+”米之外”);
}
}
}
});
/*beaconManager.addMonitorNotifier(新的MonitorNotifier(){
@凌驾
公共区域(区域){
Log.i(标记“我第一次看到一个灯塔!”);
}
@凌驾
公共区域(区域){
i(标记“我不再看到灯塔”);
}
@凌驾
公共无效不确定地区(国际州、地区){
Log.i(标记“我刚从看到/没有看到信标切换到:“+state”);
}
});*/
试一试{
beaconManager.startrangbeaconregion(新区域(“MyRangUniqueId”,null,null,null));
}捕获(远程异常e){}
}
感谢您的帮助,谢谢。

如前所述,如果目标是api-23或更高版本,并且运行在Android 6或更高版本上,您需要在运行时请求位置权限

因此,如果用户已授予位置权限,则仅在
onCreate()
中初始化BeaconManager:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);
    if (checkLocationPermission()) {
        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);
    }
checkLocationPermission()
方法将在需要时提示用户,如果用户接受位置权限,则可以初始化BeaconManager:

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            new AlertDialog.Builder(this)
                    .setTitle(R.string.title_location_permission)
                    .setMessage(R.string.text_location_permission)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //Prompt the user once explanation has been shown
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                    MY_PERMISSIONS_REQUEST_LOCATION);
                        }
                    })
                    .create()
                    .show();


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    //Set up the BeaconManager
                    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);
                }

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

            }
            return;
        }

    }
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            new AlertDialog.Builder(this)
                    .setTitle(R.string.title_location_permission)
                    .setMessage(R.string.text_location_permission)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //Prompt the user once explanation has been shown
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                    MY_PERMISSIONS_REQUEST_LOCATION);
                        }
                    })
                    .create()
                    .show();


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    //Set up the BeaconManager
                    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);
                }

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

            }
            return;
        }

    }
}