当几个iBeacon发射器,startRangingBeaconsInRegion功能只获得一个UUID

时间:2016-08-29 03:12:00

标签: android ibeacon-android altbeacon

当我使用startRangingBeaconsInRegion(new Region(“myRangingUniqueId”,null,null,null);)来区域中的iBeacon信号时,我的代码只能获得一个iBeacon信号,即使我设置了两个或三个iBeacon发送器在房间里。 在我的代码中有任何错误吗?或者在stopRangingBeaconsInRegion之前,测距只能获得一个iBeacon信号?

我的代码:

private OnClickListener clickListener = new OnClickListener() {
    @Override
    public void onClick(View v){
        if (v.equals(ibeaconRecv)){
            TransmitTh transmitTh = new TransmitTh();
            ReceiveTh receiveTh = new ReceiveTh();
            NewReceiveTh newReceiveTh = new NewReceiveTh();

            transmitTh.start();
            //receiveTh.start();
            newReceiveTh.start();
        }
    }
};




class NewReceiveTh extends Thread{

    @Override
    public void run() {
        int waitFlagTimes = WAIT_REV_FLAG_CHANGE_TIMES;
        Identifier taponUUIDIden = Identifier.parse(TaponUUIDStr);
        //Region doorBeaconRegion = new Region("TaponiBeaconDoor", taponUUIDIden, null, null);
        Region doorBeaconRegion = new Region("TaponiBeaconDoor", null, null, null);

        matchFlag = WAIT_FOUND_CORRECT_TRASNMIT;
        //start receive iBeacon signal
        Log.d("BEA","start to receive iBeacon signal");
        recTimes = TIMES_RECEV_NOT_FOUND;       
        try {
            beaconManager.startRangingBeaconsInRegion(doorBeaconRegion);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        while(recTimes > 0){
            recTimes--;
            if(matchFlag == FOUND_CORRECT_TRASNMIT){
                Log.i("BEA","Get the Door iBeacon signal and generate the output. it's a success end!");
                addValue = ((major + minor) > 0xFFFE)?(0xFFFE) : (major + minor);
                minusValue = (major > minor)?(major - minor):(minor - major);
                transmittFlag = true;
                break;
            }
            else if(matchFlag == NOT_FOUND_CORRECT_TRASNMIT){
                Log.i("BEA","Not get correct iBeacon signal, try again!");
                matchFlag = WAIT_FOUND_CORRECT_TRASNMIT;
            }
            else{
                try {
                    Thread.sleep(TIME_INTERVAL_NOF_FOUND);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                       
            }
        }
        Log.d("BEA","time is over!");
        try {
            beaconManager.stopRangingBeaconsInRegion(doorBeaconRegion);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }
}   


@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
                Region arg1) {
            boolean patternFound = false;
            if (beacons.size() > 0) {
                patternFound = true;
                double distance = beacons.iterator().next().getDistance();
                Identifier beaconUUID = beacons.iterator().next().getId1();
                Identifier beaconMajor = beacons.iterator().next().getId2();
                Identifier beaconMinor = beacons.iterator().next().getId3();
                major = beaconMajor.toInt();
                minor =beaconMinor.toInt(); 

                Log.d("BEA", "The first beacon I see is "+beacons.iterator().next().toString()+".");
                Log.d("BEA", "The first beacon I see is about "+distance+" meters away.");  

                if(patternFound){
                    if(distance >= MAX_DISTANCE_PERMIT_OPEN){
                        patternFound = false;
                    }                       
                }

                if(patternFound){
                    if(!(beaconUUID.toString().equalsIgnoreCase("5461706F-6E69-4265-6163-6F6E446F6F72"))){
                        patternFound = false;
                    }
                }

                if(patternFound){
                    if((major == 1) || (minor == 1)){
                        patternFound = false;
                    }
                }

                if(!patternFound){
                    Log.i("BEA", "The incorrect parameter is:"+beaconUUID.toString()+" & "+major+" & "+minor);  
                    matchFlag = NOT_FOUND_CORRECT_TRASNMIT;
                }
                else{
                    matchFlag = FOUND_CORRECT_TRASNMIT;
                }
            }               
        }
    });
}   


class TransmitTh extends Thread{

    @Override
    public void run() {
        int waitFlagTimes = WAIT_TRN_FLAG_CHANGE_TIMES;
        while(!transmittFlag && (waitFlagTimes > 0)){
            waitFlagTimes--;
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(transmittFlag){
            transmittFlag = false;
            Log.d("BEA","transmit flag is set, new transmit should be send!");
            //start transfer new iBeacon signal
            Beacon newBeacon = new Beacon.Builder()
                    .setId1("FDA50693-A4E2-4FB1-AFCF-C6EB07647826")
                    .setId2(""+addValue)
                    .setId3(""+minusValue)
                    .setManufacturer(0x004c)
                    .setTxPower(-59)        //0xC5
                    .setDataFields(Arrays.asList(new Long[] {0l}))
                    .build();
            BeaconParser newBeaconParser = new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
            final BeaconTransmitter newBeaconTransmitter = new BeaconTransmitter(getApplicationContext(), newBeaconParser); 
            newBeaconTransmitter.startAdvertising(newBeacon, new AdvertiseCallback() {

                @Override
                public void onStartFailure(int errorCode) {
                    Log.e("BEA", "Advertisement start failed with code: "+errorCode);
                }

                @Override
                public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                    Log.i("BEA", "New Advertisement start succeeded. Major:"+addValue+", Minor:"+minusValue);
                    try {
                        Thread.sleep(TRANSMIT_MAINTAIN_TIME);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    newBeaconTransmitter.stopAdvertising();
                    Log.i("BEA", "New Advertisement stop succeeded.");  
                }
            });
        }
        else{
            Log.d("BEA","after waiting, no transmit flag is set!");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

代码必须迭代所有发现的信标,而不是只看第一个。为此,请更改:

if (beacons.size() > 0) {
            patternFound = true;
            double distance = beacons.iterator().next().getDistance();
            Identifier beaconUUID = beacons.iterator().next().getId1();
            Identifier beaconMajor = beacons.iterator().next().getId2();
            Identifier beaconMinor = beacons.iterator().next().getId3();

要:

for (Beacon beacon : beacons) {
            patternFound = true;
            double distance = beacon.getDistance();
            Identifier beaconUUID = beacon.getId1();
            Identifier beaconMajor = beacon.getId2();
            Identifier beaconMinor = beacon.getId3();