Altbeacon图书馆:无法找到信标

时间:2017-08-16 09:31:12

标签: android altbeacon beacon

我已经使用了教程和Android Beacon Library(示例代码)。 不幸的是,我的应用程序无法找到我的Beacons(Blueup)。 不知道我的beacon配置是否有错误。

我测试了所有不同的Beaconparser表达式,以及我的Beacons的ID。最后,我决定将Identifier.parse(" ...")更改为null,以查看所有Beacons。

因为我使用了Android 6+和SDK 23,所以我添加了ACCESS_COARSE_LOCATION和蓝牙权限。

这是我收到的消息:" MonitoringActivity:我刚刚看到/没有看到信标:0"。

这是我的代码:

package com.example.smi.beacon03;

import android.app.Activity;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

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 java.util.Collection;

public class BeaconMainActivity extends Activity implements BeaconConsumer {

protected static final String TAG = "MonitoringActivity";
protected static final String TAG2 = "RangingActivity";
public static final String ALTBEACON = "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
public static final String ALTBEACON2 = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
public static final String 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";
public static final String EDDYSTONE_UID = "s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19";
public static final String EDDYSTONE_URL = "s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v";
public static final String IBEACON = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";


private BeaconManager beaconManager;

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

    beaconManager = BeaconManager.getInstanceForApplication(this);

    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT));

    beaconManager.bind(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}

@Override
public void onBeaconServiceConnect() {

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

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
        }
    });

    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                Log.i(TAG2, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
            }
        }
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
    } catch (RemoteException e) {    }
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}
}

1 个答案:

答案 0 :(得分:0)

您必须在课程顶部添加已初始化的所有信标布局。 所以做这样的事情:

beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(ALTBEACON);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(ALTBEACON2);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(EDDYSTONE_TLM);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(EDDYSTONE_UID);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(EDDYSTONE_URL);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(IBEACON);