首页 文章

是否可以使用AltBeacon从属于具体区域的iBeacon获取Major和Minor值?

提问于
浏览
4

我试图从背景中的iBeacon获取Major和Minor值 . 现在我能够检测到我何时进入某个地区,但我不知道该灯塔正在宣传哪个主要或次要 . 有没有办法得到这个?或者我应该使用Google提供的普通蓝牙API来获取原始数据并捕获所需的字节数?一旦我使用AltBeacon和蓝牙标准库我没有得到任何东西 .

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

问候,

伊万

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

    进入该区域后或在 Build 服务连接后,您必须调用上述内容 .

  • 0

    固定!!!是的,它是可行的 . 我在这里发布示例代码:我希望有人发现这很有用

    问候 .

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

相关问题