2017-10-04 20 views
2

altbeacon에서 제공 한 참조 코드를 사용했지만 iBeacons를 감지하지 못했습니다. 다음은 내 코드입니다.altbeacon android 라이브러리에서 iBeacons를 찾을 수 없음

매니페스트에 다음 권한이 포함됩니다. 이 외에도 위치 서비스도 사용할 수있게되었습니다. 활동

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를 모두 사용하여 시도했지만 모두 작동하지 않았다위한

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 

에서 onCreate 방법. RangeNotifier는 항상 크기가 0 인 콜렉션을 가지며 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) { } 
} 

감사합니다. 감사합니다.

답변

1

the documentation 상태이므로 api-23 이상을 타겟팅하고 Android 6 이상에서 실행하는 경우 런타임에 위치 권한을 요청해야합니다.

사용자가 이미 위치 permision 부여 된 경우 onCreate()에서 BeaconManager를 초기화

그래서, 전용 : 필요한 경우 checkLocationPermission() 방법은 사용자에게 메시지를 표시합니다

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); 
    } 

, 그리고 사용자가 위치 권한을 허용하는 경우, 당신이 할 수있는 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; 
     } 

    } 
}