Java ALT信标测距功能不工作

Java ALT信标测距功能不工作,java,android,android-studio,altbeacon,android-ibeacon,Java,Android,Android Studio,Altbeacon,Android Ibeacon,我正在尝试构建一个android应用程序,它可以对附近的信标进行测距,并获取相关信息,如UUID和与信标的距离。我现在面临的问题是测距功能根本无法检测到一个信标。我非常确定这个功能是正确的,因为我下载了另一个类似的演示项目,可以在同一台设备上很好地扫描信标 应用程序现在在使用beaconManager.StartRangBeaconsisnRegion(新区域(“MyRangUniqueId”,null,null,null))函数后没有任何反应,调试器显示线程卡在DidRangeBeaconsi

我正在尝试构建一个android应用程序,它可以对附近的信标进行测距,并获取相关信息,如UUID和与信标的距离。我现在面临的问题是测距功能根本无法检测到一个信标。我非常确定这个功能是正确的,因为我下载了另一个类似的演示项目,可以在同一台设备上很好地扫描信标

应用程序现在在使用beaconManager.StartRangBeaconsisnRegion(新区域(“MyRangUniqueId”,null,null,null))函数后没有任何反应,调试器显示线程卡在DidRangeBeaconsisnRegion函数中,且信标大小始终为0

我的代码有问题吗?还是因为我的设置或配置不正确

代码:


当您无法使用检测时,需要检查以下几点:

  • 确保您的信标正在传输AltBeacon格式。如果没有,您只需要注册一个表达式,使其检测Eddystone或专有信标类型,如iBeacon

  • 如果您不确定播发的格式,请尝试使用,这将告诉您它检测到的格式

  • 如果无法检测的设备具有Android 6.0+,请确保您已获得位置权限,如前所述


在我将目标sdk更改为21并添加了位置权限后,它终于起作用了。你救了我的周末,非常感谢!
package com.example.ma.contextualawarenessapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.app.Activity;
import android.os.RemoteException;
import android.widget.TextView;

import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

public class GeofencingActivity extends AppCompatActivity implements     BeaconConsumer {
protected static final String TAG = "GeofencingActivity";
private BeaconManager beaconManager;

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

    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(this);

}
@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
@Override
protected void onPause() {
    super.onPause();
    if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(true);
}
@Override
protected void onResume() {
    super.onResume();
    if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            for (Beacon beacon : beacons) {
                logToDisplay(getCurrentTimeStamp() + " | Beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away.");
            }
        /*if (beacons.size() > 0) {
            EditText editText = (EditText)GeofencingActivity.this
                    .findViewById(R.id.geofencingText);
            Beacon firstBeacon = beacons.iterator().next();
            logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away.");            }
    */}

    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {   }
}
private void logToDisplay(final String line) {
    runOnUiThread(new Runnable() {
        public void run() {
            TextView editText = (TextView)GeofencingActivity.this
                    .findViewById(R.id.geofencingText);
            editText.append(line+"\n");
        }
    });
}

private static String getCurrentTimeStamp() {
    Locale locale = new Locale("es", "ES");
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS", locale);
    Date now = new Date();
    return sdf.format(now);
}
}
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.example.ma.contextualawarenessapplication"
    minSdkVersion 18
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'org.altbeacon:android-beacon-library:2+@aar'
}

buildscript {
repositories {
    jcenter()
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0-alpha5'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}