2017-04-26 27 views
0

Ì試圖在我的android應用程序中連接信標。但我似乎無法找到我的信標。我正在使用Ibeacons。我正在使用AltBeacon庫。 onBeaconServiceConnect啓動並執行didDetermineStateForRegion。但didEnterReion永遠不會被調用。這是我的代碼:無法找到altbeacon庫的燈塔

public class ListScenarios extends AppCompatActivity implements BeaconConsumer, MonitorNotifier { 

private static final String TAG = "ListScenarios"; 

private ListView listView; 
public String persoonID; 
public String adresID; 

public Array beaconArray; 
//private ArrayList<IBeacon> arrBeacons = new ArrayList<>(); 

private BeaconManager mBeaconManager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list_scenarios); 

    // Setup beaconmanager 
    mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext()); 
    // Detect iBeacon 
    mBeaconManager.getBeaconParsers().add(new BeaconParser() 
      .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); 
    mBeaconManager.bind(this); 



    //listview 
    listView = (ListView) findViewById(R.id.list_scenario); 

    //send request to load list 
    getScenarios(); 
    getBeacons(); 



} 


// MARK: - Bluetooth 


@Override 
public void onBeaconServiceConnect() { 
    System.out.println("We are in onBeaconServiceConnect"); 
    // Set the two identifiers below to null to detect any beacon regardless of identifiers 
    Identifier myBeaconNamespaceId = null; 
    Identifier myBeaconInstanceId = null; 
    Region region = new Region("my-beacon-region", myBeaconNamespaceId, myBeaconInstanceId, null); 
    mBeaconManager.addMonitorNotifier(this); 
    try { 
     mBeaconManager.startMonitoringBeaconsInRegion(region); 
    } catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 

public void didEnterRegion(Region region) { 
    System.out.println("We are in didEnterRegion"); 
    Log.d(TAG, "I detected a beacon in the region with namespace id " + region.getId1() + 
      " and instance id: " + region.getId2()); 
} 

public void didExitRegion(Region region) { 
    System.out.println("We are in didExitRegion"); 
} 

public void didDetermineStateForRegion(int state, Region region) { 
    System.out.println("We are in didDetermineStateForRegion"); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mBeaconManager.unbind(this); 
} 
+0

可以檢測到信標與基於定位的應用在這個相同的圖書館? https://play.google.com/store/apps/details?id=com.radiusnetworks.locate。你在'didDetermineStateForRegion'中得到了什麼'state'的值? – davidgyoung

+0

是的,我可以找到它與查找應用程序。它總是說0 –

回答

0

兩件事情來檢查:

  1. 確保您使用的是正確的BeaconParser表達。問題中顯示的表達式適用於AltBeacon:"m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25。如果您嘗試檢測iBeacon,則需要使用不同的表達式。本網站有便利參考:https://beaconlayout.wordpress.com/

  2. 如果您在Android 6+上運行您的應用並且定位到SDK 23或更高版本,則需要爲您的應用動態請求位置權限。如果你不這樣做,你會得到沒有檢測,你會看到這樣的日誌:04-22 22:35:20.152 5158 5254 E BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results您可以瞭解如何在這裏做到這一點:http://altbeacon.github.io/android-beacon-library/requesting_permission.html

+0

ACCESS_COARSE_LOCATION做了竅門,謝謝 –

+0

很高興聽到你解決它! – davidgyoung