4

我正在尝试从后台的 iBeacon 获取主要和次要值。现在我能够检测到我何时进入一个区域,但我不知道信标正在宣传哪个主要或次要。有没有办法得到这个?或者我应该使用谷歌提供的普通蓝牙 api 来获取原始数据并捕获所需的字节?一旦我使用 AltBeacon 和蓝牙标准库,我什么都得不到。

我使用http://altbeacon.github.io/android-beacon-library/samples.html中提供的示例代码来获取背景区域检测并实现接口 BluetoothAdapter.LeScanCallback 以获取要获取的原始数据主要和次要字节,但是当我添加所有我需要得到的应用程序甚至没有进入 didEnterRegion 方法时。

问候,

伊万

4

2 回答 2

4

为了读取信标标识符,只需使用测距 API:

beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));           
beaconManager.setRangeNotifier(new RangeNotifier() {
    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            Log.i(TAG, "The first beacon I see has minor id "+beacons.iterator().next().getId3());  
        }
    }
});

您必须在进入该地区或建立服务连接后拨打上述电话。

于 2014-11-10T13:11:52.280 回答
1

固定的!!!是的,这是可能的。我在这里发布示例代码:我希望有人觉得这很有用

问候。

public class MyRegionApp extends Application implements BootstrapNotifier, BluetoothAdapter.LeScanCallback {
    private final String DebugTag="HOLAREGION";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private BeaconManager mBeaconManager;
    private static final String UUID = "01122334-4556-6778-899a-abbccddeeff0";
    private long time=0;
    private BluetoothAdapter mBluetoothAdapter;
    private String Minor;
    private String Major;
    private String UUID1;
    private BluetoothManager bluemanager;

    @Override
    public void onCreate() {
        super.onCreate();
        time=System.currentTimeMillis();
        Log.d(DebugTag, "App started up");
        bluemanager= (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluemanager.getAdapter();
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        //BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
        mBeaconManager.setBackgroundScanPeriod(1100l);

        mBeaconManager.setBackgroundBetweenScanPeriod(1100l);
        Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);

        //Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);
        //Region region = new Region("allbeacons", null , null, null);
        backgroundPowerSaver = new BackgroundPowerSaver(this);

        regionBootstrap = new RegionBootstrap(this, region);
        BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));



    }




    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // TODO Auto-generated method stub
        Log.d(DebugTag, "Got a didDetermineStateForRegion call with region:"+arg1.toString() );
    }

    @Override
    public void didEnterRegion(Region arg0) {
        // TODO Auto-generated method stub
        time=System.currentTimeMillis()-time;

        Log.d(DebugTag, "Got a didEnterRegion call region:"+arg0.getUniqueId());
        Log.d(DebugTag, "Got a didEnterRegion call region TIME: "+time);



        mBluetoothAdapter.startLeScan(this);

        //Descomentar la siguiente línea si se desea que únicamente se lance la actividad únicamente la primera vez que la beacon es vista
        //regionBootstrap.disable();

        Toast.makeText(getApplicationContext(), "ENTRA EN LA REGION!!!",
                   Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);

    }

    @Override
    public void didExitRegion(Region arg0) {
        // TODO Auto-generated method stub
        Log.d(DebugTag, "Got a didExitRegion call");
        Log.d(DebugTag, "Got a didExitRegion call with region:"+arg0.getUniqueId() );
    }



    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        // TODO Auto-generated method stub
        Log.d(DebugTag, "Entra en onLeScan");
        Major="";
        Minor="";
        UUID1="";

        for (int i = 0; i<scanRecord.length; i++){
            if(i>8 && i<25)
                UUID1 += String.format("%02x", scanRecord[i]);
            else if(i>24 && i<27)
                Major += String.format("%02x", scanRecord[i]);
            else if(i>26 && i<29)
                Minor += String.format("%02x", scanRecord[i]);



        }

        stop();
        Log.d(DebugTag, "Got a didExitRegion call with MAJOR:"+Major+" MINOR: "+Minor+" and UUID: "+UUID1  );
    }

    public void stop(){

        mBluetoothAdapter.stopLeScan(this);
        Log.d(DebugTag+": scanLeDevice->"," REGION Stopped");
        Log.d(DebugTag, "Got a didExitRegion call with MAJOR:"+Major+" MINOR: "+Minor );

    }



}

在 davidgyoung 的建议下,我得到了我想要的东西,如下所示。也许我犯了错误,但它对我有用,我想获得带有 iBeacon 信息的日志:

@Override
    public void onCreate() {
        super.onCreate();
        time=System.currentTimeMillis();
        Log.d(DebugTag, "App started up");
        //bluemanager= (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        //mBluetoothAdapter = bluemanager.getAdapter();
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBeaconManager.setBackgroundScanPeriod(8000l);

        mBeaconManager.setBackgroundBetweenScanPeriod(1100l);
        Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);

        //Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);
        //Region region = new Region("allbeacons", null , null, null);
        backgroundPowerSaver = new BackgroundPowerSaver(this);

        regionBootstrap = new RegionBootstrap(this, region);




        BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));



    }




    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // TODO Auto-generated method stub
        Log.d(DebugTag, "Got a didDetermineStateForRegion call with region:"+arg1.toString() );
    }

    @Override
    public void didEnterRegion(Region arg0) {
        // TODO Auto-generated method stub
        time=System.currentTimeMillis()-time;

        Log.d(DebugTag, "Got a didEnterRegion call region:"+arg0.getUniqueId());
        Log.d(DebugTag, "Got a didEnterRegion call region TIME: "+time);


        if(enabler==true)
        {
             try {
                    mBeaconManager.startRangingBeaconsInRegion(arg0);
                    enabler=false;
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
        }






         mBeaconManager.setRangeNotifier(new RangeNotifier() {

                @Override
                public void didRangeBeaconsInRegion(Collection<Beacon> arg0,
                        Region arg1) {
                    // TODO Auto-generated method stub
                    if (arg0.size() > 0) {
                        Log.i(DebugTag, "The first beacon I see has UUID: "+arg0.iterator().next().getId1()+" major id:"+arg0.iterator().next().getId2()+"  minor id: "+arg0.iterator().next().getId3());  
                    }
                    try {
                        mBeaconManager.stopRangingBeaconsInRegion(arg1);
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });


    }

    @Override
    public void didExitRegion(Region arg0) {
        // TODO Auto-generated method stub
        Log.d(DebugTag, "Got a didExitRegion call");
        Log.d(DebugTag, "Got a didExitRegion call with region:"+arg0.getUniqueId() );
        enabler=true;
    }


}
于 2014-11-10T12:03:48.167 回答