2017-08-04 37 views
1

我正在嘗試創建和應用程序來定位信標並獲得它的距離,我正在使用altbeacon庫。通過使用代碼我得到附近的燈塔的細節,但我沒有得到的距離。這裏是我的代碼和堆棧跟蹤。我的代碼中有什麼問題,任何人都可以幫助我確定。應用程序使用altbeacon不顯示距離

MainActivity.java

import java.util.ArrayList; 
import java.util.Collection; 

import org.altbeacon.beacon.Beacon; 
import org.altbeacon.beacon.BeaconConsumer; 
import org.altbeacon.beacon.BeaconManager; 
import org.altbeacon.beacon.BeaconParser; 
import org.altbeacon.beacon.Identifier; 
import org.altbeacon.beacon.MonitorNotifier; 
import org.altbeacon.beacon.RangeNotifier; 
import org.altbeacon.beacon.Region; 
import org.altbeacon.beacon.startup.BootstrapNotifier; 

import android.Manifest; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothManager; 
import android.bluetooth.le.BluetoothLeScanner; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.RemoteException; 
import android.provider.Settings; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements BeaconConsumer{ 


    public static final String TAG = "BeaconsEverywhere"; 
    private BeaconManager beaconManager; 


    @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.bind(this); 
    } 


    @Override 
    public void onBeaconServiceConnect() { 

     final Region region = new Region("myBeaons", null, null, null); 

     beaconManager.addRangeNotifier(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.");   
       } 
      } 
     }); 

     beaconManager.setRangeNotifier(new RangeNotifier() { 
      @Override 
      public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region arg1) { 

       for(Beacon oneBeacon : beacons) { 
        Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3()); 
       } 
      } 
     }); 

     try { 
      beaconManager.startMonitoringBeaconsInRegion(region); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
     try { 
      beaconManager.startRangingBeaconsInRegion(region); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 

    } 





} 

的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.beaconexample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="21" 
     android:targetSdkVersion="21" /> 


    <uses-permission android:name="ANDROID.PERMISSION.INTERNET"/> 
    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 


    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="org.altbeacon.beacon.startup.StartupBroadcastReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> 
       <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name="org.altbeacon.beacon.service.BeaconService" 
      android:enabled="true" 
      android:exported="false" 
      android:isolatedProcess="false" 
      android:label="beacon" /> 
     <service 
      android:name="org.altbeacon.beacon.BeaconIntentProcessor" 
      android:enabled="true" 
      android:exported="false" /> 


    </application> 

</manifest> 

和堆棧跟蹤

08-04 06:33:32.344: V/Monotype(10057): SetAppTypeFace- try to flip, app = com.example.beaconexample 
08-04 06:33:32.348: V/Monotype(10057):  Typeface getFontPathFlipFont - systemFont = default#default 
08-04 06:33:32.354: V/Monotype(10057): SetAppTypeFace- try to flip, app = com.example.beaconexample 
08-04 06:33:32.354: V/Monotype(10057):  Typeface getFontPathFlipFont - systemFont = default#default 
08-04 06:33:32.396: I/BeaconManager(10057): BeaconManager started up on pid 10057 named 'com.example.beaconexample' for application package 'com.example.beaconexample'. isMainProcess=true 
08-04 06:33:32.399: D/BeaconParser(10057): Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25 
08-04 06:33:32.408: D/OpenGLRenderer(10057): Render dirty regions requested: true 
08-04 06:33:32.413: D/Atlas(10057): Validating map... 
08-04 06:33:32.421: I/CycledLeScanner(10057): This Android 5.0. We are using new scanning APIs 
08-04 06:33:32.434: I/BeaconService(10057): beaconService version 2.11 is starting up on the main process 
08-04 06:33:32.437: W/ModelSpecificDistanceCalculator(10057): App has no android.permission.INTERNET permission. Cannot check for distance model updates 
08-04 06:33:32.459: W/ModelSpecificDistanceCalculator(10057): Cannot find match for this device. Using default 
08-04 06:33:32.459: W/ModelSpecificDistanceCalculator(10057): Cannot find match for this device. Using default 
08-04 06:33:32.463: I/BeaconService(10057): binding 
08-04 06:33:32.472: I/Adreno-EGL(10057): <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3.C1.05.00.02.087.140_msm8916_64_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C1.05.00.02.087.140__release_AU (I6eddbfa548) 
08-04 06:33:32.472: I/Adreno-EGL(10057): OpenGL ES Shader Compiler Version: E031.25.03.02 
08-04 06:33:32.472: I/Adreno-EGL(10057): Build Date: 09/17/15 Thu 
08-04 06:33:32.472: I/Adreno-EGL(10057): Local Branch: 
08-04 06:33:32.472: I/Adreno-EGL(10057): Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C1.05.00.02.087.140 
08-04 06:33:32.472: I/Adreno-EGL(10057): Local Patches: NONE 
08-04 06:33:32.472: I/Adreno-EGL(10057): Reconstruct Branch: NOTHING 
08-04 06:33:32.475: I/OpenGLRenderer(10057): Initialized EGL, version 1.4 
08-04 06:33:32.487: D/OpenGLRenderer(10057): Enabling debug mode 0 
08-04 06:33:32.489: I/qdutils(10057): PartialUpdate status: Disabled 
08-04 06:33:32.489: I/qdutils(10057): Left Align: 0 
08-04 06:33:32.489: I/qdutils(10057): Width Align: 0 
08-04 06:33:32.489: I/qdutils(10057): Top Align: 0 
08-04 06:33:32.489: I/qdutils(10057): Height Align: 0 
08-04 06:33:32.489: I/qdutils(10057): Min ROI Width: 0 
08-04 06:33:32.489: I/qdutils(10057): Min ROI Height: 0 
08-04 06:33:32.489: I/qdutils(10057): Needs ROI Merge: 0 
08-04 06:33:32.489: I/qdutils(10057): Dynamic Fps: Disabled 
08-04 06:33:32.489: I/qdutils(10057): Min Panel fps: 0 
08-04 06:33:32.489: I/qdutils(10057): Max Panel fps: 0 
08-04 06:33:32.534: I/BeaconService(10057): start ranging received 
08-04 06:33:32.564: D/BluetoothLeScanner(10057): onClientRegistered() - status=0 clientIf=8 
08-04 06:33:32.564: I/Timeline(10057): Timeline: Activity_idle id: [email protected] time:12888793 
08-04 06:33:32.695: D/BluetoothLeScanner(10057): onScanResult() - ScanResult{mDevice=00:A0:50:B2:95:AD, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=[0000feaa-0000-1000-8000-00805f9b34fb], mManufacturerSpecificData={}, mServiceData={0000feaa-0000-1000-8000-00805f9b34fb=[0, -14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, mTxPowerLevel=-2147483648, mDeviceName=TCZ], mRssi=-76, mTimestampNanos=47409899203234} 
08-04 06:33:33.340: D/BluetoothLeScanner(10057): onScanResult() - ScanResult{mDevice=00:A0:50:B2:94:9B, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=[0000feaa-0000-1000-8000-00805f9b34fb], mManufacturerSpecificData={}, mServiceData={0000feaa-0000-1000-8000-00805f9b34fb=[0, -14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, mTxPowerLevel=-2147483648, mDeviceName=TCZ], mRssi=-84, mTimestampNanos=47410545673859} 
+0

你說「我沒有得到距離」。這是什麼意思?它爲距離打印什麼? – davidgyoung

+0

我的意思是說沒有函數setRangeNotifier和addRangeNotifier調用,並且不打印以下日誌 Log.d(TAG,「distance:」+ oneBeacon.getDistance()+「id:」+ oneBeacon.getId1()+「/ 「+ oneBeacon.getId2()+」/「+ oneBeacon.getId3()); (標籤,「我看到的第一個信標是關於」+ beacons.iterator()。next()。getDistance()+「米遠」); – user2427254

回答

0

如果我理解正確的問題,問題是,檢測和didRangeBeaconsInRegion(...)沒有信標永遠不會被調用。

嘗試更改: setBeaconLayout(「m:2-3 = beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25」) );

要使用不同的佈局表達式,可能是iBeacon的佈局表達式,如果這是您正在嘗試檢測的佈局表達式。你可以從這裏找到不同的信標格式表達式的列表:

https://beaconlayout.wordpress.com

+0

感謝您的回覆,我可以更改beaconLayout和代碼工作正常。 – user2427254

0

嗨大衛我的代碼檢測到跟隨在logcat的

08-04 06:33:32.695: D/BluetoothLeScanner(10057): onScanResult() - ScanResult{mDevice=00:A0:50:B2:95:AD, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=[0000feaa-0000-1000-8000-00805f9b34fb], mManufacturerSpecificData={}, mServiceData={0000feaa-0000-1000-8000-00805f9b34fb=[0, -14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, mTxPowerLevel=-2147483648, mDeviceName=TCZ], mRssi=-76, mTimestampNanos=47409899203234} 
08-04 06:33:33.340: D/BluetoothLeScanner(10057): onScanResult() - ScanResult{mDevice=00:A0:50:B2:94:9B, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=[0000feaa-0000-1000-8000-00805f9b34fb], mManufacturerSpecificData={}, mServiceData={0000feaa-0000-1000-8000-00805f9b34fb=[0, -14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, mTxPowerLevel=-2147483648, mDeviceName=TCZ], mRssi=-84, mTimestampNanos=47410545673859} 

所有細節信標和表演,但下面的代碼無法正常工作

@Override 
    public void onBeaconServiceConnect() { 

     final Region region = new Region("myBeaons", null, null, null); 

     beaconManager.addRangeNotifier(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.");   
       } 
      } 
     }); 

     beaconManager.setRangeNotifier(new RangeNotifier() { 
      @Override 
      public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region arg1) { 

       for(Beacon oneBeacon : beacons) { 
        Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3()); 
       } 
      } 
     }); 

     try { 
      beaconManager.startMonitoringBeaconsInRegion(region); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
     try { 
      beaconManager.startRangingBeaconsInRegion(region); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 

    } 

我的主要任務是獲得信標設備的距離。