2017-08-04 32 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(); 
     } 

    } 

我的主要任务是获得信标设备的距离。