Android:AltBeacon 未检测到信标

标签 android altbeacon beacon

我正在使用此处的 AltBeacon 库开发一个简单的信标接近应用程序 https://altbeacon.github.io/android-beacon-library/samples.html

我正在试验上述网站上提供的示例代码,但是,每次我运行该应用程序时,它只会转到 didDetermineStateForRegion() 方法。如果它检测到信标,它将转到 didEnterRegion() 方法。

我不确定自己做错了什么,也无法在其他问题中找到答案。

上述网站上提供的引用应用程序也存在同样的问题,但定位应用程序(基于 AltBeacon)会立即检测到我的信标。

我的 beacon 设置为 152ms 传输间隔和最大传输功率。

这是我的代码:

主要 Activity

public class MainActivity extends AppCompatActivity implements BeaconConsumer {
    protected final String TAG = "Beacons Monitoring";
    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:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
        beaconManager.bind(this);
    }

    public void onDestroy(){
        super.onDestroy();
        beaconManager.unbind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.addMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                Log.e(TAG,"I just saw a beacon for the first time");
            }

            @Override
            public void didExitRegion(Region region) {
                Log.e(TAG, "I lost my beacons :( ");
            }
            @Override
            public void didDetermineStateForRegion(int i, Region region) {
                Log.e(TAG, "I just switched from seeing/not seeing a beacon. STATE: " + i);
            }
        });
        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));

        } catch (RemoteException e){
            Log.e(TAG, "EXCEPTION!!! :'( ");
        }
    }
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.michal.beacons2">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

有人知道我的代码有什么问题吗?

最佳答案

我设法解决了这个问题。

首先,根据您使用的信标类型设置信标布局很重要。这些是可用的布局:

ALTBEACON       m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
EDDYSTONE TLM   x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15
EDDYSTONE UID   s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19
EDDYSTONE URL   s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v
IBEACON         m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24

来源:https://beaconlayout.wordpress.com/

可以使用以下代码设置布局:

beaconManager.getBeaconParsers().add(new 
BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

其次,android 需要位置权限才能检测信标。我通过检查授予和拒绝权限时发生的情况对此进行了测试,但 Android Beacon Library 网站上也提到了这一要求。我建议按照有关获取用户许可的 Google Android 开发人员培训的说明进行操作: https://developer.android.com/training/permissions/index.html

此外,一旦我设法解决这个问题,我就测试了 davidgyoung 提供的代码,它确实按照描述的方式工作,所以我建议在开发阶段将它添加到您的代码中。

所有这些实际上在 Android Beacon Library 网站上都有清楚的解释,但我不知何故混淆了一切。我希望这个答案对以后的人有所帮助。

关于Android:AltBeacon 未检测到信标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42168250/

相关文章:

Java格式化字符串以删除变量前面的 "0"

安装新版本后运行的Android信标库

安卓开发: Bluetooth Low Emission Advertisement is "Not Supported" on compatible devices

ios - 我可以在不指定 "region"的情况下快速扫描信标吗?

android - 字典作为 SignalR Android 客户端中的查询字符串参数

android - InAppBilling 不可靠

android - AndEngine在多个设备上加载TMX map

java - 即使有前台服务,测距和监控也会在一段时间后停止

java - URIBeacon 的 BeaconLayout 是什么?

java - 借助Android上的SDK,如何改变iBeacon的参数值(UUID、Major、Minor、TxPower)