代码之家  ›  专栏  ›  技术社区  ›  Sunny

Altbeacon库:找不到信标

  •  0
  • Sunny  · 技术社区  · 7 年前

    我已经使用了教程和Android Beacon库(示例代码)。 不知道我的BEAConformation是否有错误。

    我用信标的ID测试了所有不同的信标解析器表达式。最后,我决定更改标识符。解析(“…”)为空,以查看所有信标。

    因为我使用了Android 6+和SDK 23,所以我添加了ACCESS\u rough\u位置和蓝牙权限。

    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 回复  |  直到 7 年前
        1
  •  0
  •   Hadi shamqoli    7 年前

    您必须添加在类顶部初始化的所有信标布局。

    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);