2016-04-16 39 views
1

我嘗試使用Android燈塔庫檢測iBeacons,但它不工作檢測iBeacon的Android Beacon庫失敗!不公平:(

從logcat的IST只是輸出:

I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file. I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-I9195_4.4.2_0046 I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts E/dalvikvm﹕ >>>>> Normal User E/dalvikvm﹕ >>>>> com.cyberland.felix.ibeaconexample [ userId:0 | appId:10176 ] D/dalvikvm﹕ Late-enabling CheckJNI I/PersonaManager﹕ getPersonaService() name persona_policy D/BeaconParser﹕ Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25 D/BeaconParser﹕ Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24 W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default E/MonitoringStatus﹕ Deserialization exception, message: $s

然後就是

D/BluetoothAdapter﹕ stopLeScan()

連連

...

我剛剛修改了官方網頁的例子:

public class MainActivity extends Activity implements BeaconConsumer { 
protected static final String TAG = "MainActivity"; 
private BeaconManager beaconManager; 
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1; 

private BluetoothAdapter mBluetoothAdapter; 



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

    /* 
    mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); 
    mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() { 
     @Override 
     public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 
      Log.d(TAG, "Scanned BLE device with mac: " + device.getAddress()); 
     } 
    }); 
    */ 
    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); 




} 


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

} 


@Override 
public void onBeaconServiceConnect() { 
    beaconManager.setRangeNotifier(new RangeNotifier() { 
     @Override 
     public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
      if (beacons.size() > 0) { 
       Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away."); 
      } 
     } 
    }); 

    try { 
     beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null)); 
    } catch (RemoteException e) { } 
} 

我希望你能幫助我!

此致 菲利克斯

+0

您的手機有哪些Android版本?如果你轉到設置 - >位置,你會看到什麼?位置是開還是關,以及它的模式是什麼 - 高精度,僅設備或節省電池? – davidgyoung

+0

手機有4.4.2(三星S4 Mini),位置準確度高。我可以從playstore找到一款應用程序。 – Cyberlander

回答

0

它看起來像問題可能與適當的權限不被授予您的設備上掃描。爲了測試這一理論,請將以下代碼添加到主要活動的onCreate方法中,並查看它返回的內容。下面顯示了我在Nexus 5X上看到的輸出示例。

 try { 
      PackageInfo info = getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS); 
      Log.d(TAG, "SDK "+Build.VERSION.SDK_INT+" App Permissions:"); 
      if (info.requestedPermissions != null) { 
       for (String p : info.requestedPermissions) { 
        int grantResult = this.checkPermission(p, android.os.Process.myPid(), android.os.Process.myUid()); 
        if (grantResult == PackageManager.PERMISSION_GRANTED) { 
         Log.d(TAG, p+" PERMISSION_GRANTED"); 
        } 
        else { 
         Log.d(TAG, p+" PERMISSION_DENIED: "+grantResult); 
        } 
       } 
      } 
     } catch (Exception e) { 
      Log.d(TAG, "Cannot get permissions due to error", e); 
     } 

輸出示例:

04-17 12:04:01.943 30624-30624/? D/MonitoringActivity: SDK 23 App Permissions: 
04-17 12:04:01.944 30624-30624/? D/MonitoringActivity: android.permission.INTERNET PERMISSION_GRANTED 
04-17 12:04:01.945 30624-30624/? D/MonitoringActivity: android.permission.BLUETOOTH PERMISSION_GRANTED 
04-17 12:04:01.945 30624-30624/? D/MonitoringActivity: android.permission.BLUETOOTH_ADMIN PERMISSION_GRANTED 
04-17 12:04:01.946 30624-30624/? D/MonitoringActivity: android.permission.RECEIVE_BOOT_COMPLETED PERMISSION_GRANTED 
04-17 12:04:01.946 30624-30624/? D/MonitoringActivity: android.permission.ACCESS_COARSE_LOCATION PERMISSION_GRANTED 

如果發現任何上述權限從設備上的輸出中缺少的,該解決方案可以是手動添加ACCESS_COARSE_LOCATION位置權限到AndroidManifest.xml。如果您看到PERMISSION_DENIED,則可能是另一個問題。在您的設備上開始信標掃描之前,您可以跳過權限檢查,這可以通過暫時將您的Android Beacon庫版本降級到2.7,但不會執行此檢查。

+0

非常感謝! ACCESS_COARSE_LOCATION是關鍵。 – Cyberlander

+0

你是說你的調試輸出中缺少權限?還是你說它說權限被拒絕?另外,你使用的是Android Studio還是Eclipse?對於後續行動感到抱歉,但我想確切地瞭解您遇到了哪些問題以及如何解決問題,以便我可以幫助其他人。 – davidgyoung

+0

權限丟失,我正在使用Android Studio。我與Eclipsed有同樣的問題。向清單添加ACCESS_COARSE_LOCATION權限解決了問題。 – Cyberlander

0

嗯,官方示例(機器人信標庫參考)產生相同的輸出。我無法解釋這一點。