i am trying to create and app to locate beacon and get its distance and i am using altbeacon library for that. by using the code i get details of nearby beacon but i didn't get the distance. here is my code and stacktrace. what is the problem in my code can anyone please help me to identify.
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>
and stacktrace
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: android.os.BinderProxy@1fc91e75 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 :(得分:1)
private double calculateDistance(int rssi){
int txPower = -59; //hard coded power value. Usually ranges between -59 to -65
if (rssi == 0) {
return -1.0;
}
double ratio = rssi * 1.0 / txPower;
if (ratio < 1.0) {
return Math.pow(ratio,10);
}else{
return (0.89976)*Math.pow(ratio,7.7095) + 0.111;
}
}
使用此代码来计算信标距离bcoz,大多数信标具有不同的TxPower。您必须校准信标才能获得TxPower
-59 TxPower,您将默认获得
答案 1 :(得分:0)
If I understand the question correctly, the issue is that no beacons are detected and didRangeBeaconsInRegion(...)
is never called.
Try changing: setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
To use a different layout expression, perhaps the one for iBeacon if that is what you are trying to detect. You can find a list of expressions for different beacon formats here:
答案 2 :(得分: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();
}
}
我的主要任务是获得信标设备的距离。