Android 6.0没有看到beacon

时间:2016-12-09 23:06:59

标签: android ibeacon beacon

我有android 6.0手机。这个应用程序没有看到灯塔。 I am getting this error

I / BluetoothLeScanner: startRegisteration: mLeScanClients = {org.altbeacon.beacon.service.scanner.CycledLeScannerForLollipop $ 4 @ 5445b45 = android.bluetooth.le.BluetoothLeScanner $ BleScanCallbackWrapper @ d9ce5fa}.

你能帮助我吗?

public class MainActivity extends AppCompatActivity implements BeaconConsumer {
private BeaconManager beaconManager;
private Beacon nearestBeacon;

private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
private static final String TAG = "Beacon";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    beaconManager=BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(20));
    beaconManager.setBackgroundBetweenScanPeriod(TimeUnit.SECONDS.toMillis(10));
    beaconManager.bind(this);



    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
    if(this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
        final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("This app needs location acces");

        builder.setMessage("Please grant location..");
        builder.setPositiveButton(android.R.string.ok,null);
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
           requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_COARSE_LOCATION);
            }
        });
        builder.show();

    }
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_COARSE_LOCATION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "coarse location permission granted");
            } else {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Functionality limited");
                builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                    }

                });
                builder.show();
            }
            return;
        }
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}

@Override
    public void onBeaconServiceConnect() {
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
            if(collection.size()>0){
                Log.d(TAG,collection.toString());
                List beaconList=new ArrayList(collection);

            }
        }
    });
    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region("myRangingUniqueID",null,null,null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }

}
public void getNearestBeacon(List beaconList) {
    for(int i=0; i<beaconList.size(); i++)
    {
        Beacon tempBeacon=((Beacon) beaconList.get(i));
        if(tempBeacon.getId3() != nearestBeacon.getId3())
        {
            if(tempBeacon.getDistance() < nearestBeacon.getDistance())
            {
                nearestBeacon=tempBeacon;
            }
        }
    }
    // t1.setText(nearestBeacon.getId2().toString());
    Log.d(TAG,"Beacon'ın Majoru: " + nearestBeacon.getId2());


}
}
  

我的申请代码。

1 个答案:

答案 0 :(得分:0)

我不确定您的应用程序出了什么问题,但对于在Android 6上扫描,重要的是:

  • 蓝牙已开启
  • 位置在
  • 授予位置许可

您在扫描iBeacons吗?在这种情况下,您可以尝试使用此库:https://github.com/inthepocket/ibeacon-scanner-android,看看您是否有更多运气。