Altbeacon-扫描阵列中的多个UUID

问题描述 投票:2回答:1

我正在为我的应用程序使用Altbeacon库来扫描信标。在有广告的情况下,我已经成功扫描并推送了通知。我的要求是我需要扫描一个UUID列表,但是文档中给出的示例仅使用一个UUID进行扫描。

到目前为止,我将BeaconConsumer放入BaseActivity中,以便它在启动后立即开始扫描(无背景扫描):

public class BaseActivity extends SlidingFragmentActivity implements BeaconConsumer {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...

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

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                Intent intent = new Intent(getApplicationContext(), BeaconNotificationService.class);
                intent.putExtra("uuid", region.getId1().toString());
                intent.putExtra("major", region.getId2().toString());
                intent.putExtra("minor", region.getId3().toString());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(intent);
            }

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

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

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("com.my.app.boostrapRegion",
                    Identifier.parse(Constants.BT_UUID),
                    Identifier.fromInt(Constants.BT_MAJOR),
                    Identifier.fromInt(Constants.BT_MINOR)));
        } catch (RemoteException e) { e.printStackTrace(); }
    }

}

在Constants类中,我出于测试目的定义了我的UUID:

public class Constants {

    public static final String BT_UUID = "bbbbbbbb-3443-8888-3443-bb323bbb0005";
    public static final int BT_MAJOR = 0;
    public static final int BT_MINOR = 0;

}

例如,我如何放置要由BeaconConsumer扫描的UUID数组?

谢谢

java android bluetooth-lowenergy altbeacon
1个回答
1
投票

您可以添加null来查找范围内的所有信标,而不是在区域中定义UUID。然后,您可以稍后过滤出您要查找的信标。在此示例中,为清晰起见,我保留了您的主要和次要ID不变。

try {
    beaconManager.startMonitoringBeaconsInRegion(new Region("com.my.app.boostrapRegion",
    null,
    Identifier.fromInt(Constants.BT_MAJOR),
    Identifier.fromInt(Constants.BT_MINOR)));
} catch (RemoteException e) { e.printStackTrace(); }
© www.soinside.com 2019 - 2024. All rights reserved.