简体   繁体   English

Kontakt.io SDK:有没有办法在信标列表中进行硬编码,以便每当我需要搜索信标时,我可以随机抽取它们?

[英]Kontakt.io SDK: Is there a way to hard code in a list of beacons so that I can pull from them randomly whenever I need to search for a beacon?

I am programming an app that is intended to help children get up and active in the classroom by assigning questions to beacons. 我正在编写一个应用程序,旨在通过向信标分配问题来帮助孩子们在课堂上起床和活动。 The app works like this: 该应用程序的工作原理如下

  1. enter a gamecode from the api (similar to Kahoot) 从api输入游戏代码(类似于Kahoot)
  2. start scanning for beacons 开始扫描信标
  3. upon finding and being within a meter of a certain beacon, enter the question screen and ask a question that the user must answer. 找到并在某个信标的一米之内时,进入问题屏幕并询问用户必须回答的问题。
  4. repeat steps 2 and 3 until out of questions. 重复步骤2和3,直到出问题为止。

The problem in question comes from the 2nd step of the process. 有问题的问题来自该过程的第二步。 I am looking to get a list of all beacons I have (either hardcoded into the app, or gathering them on the fly) so that I can easily just pick a random beacon and have the user go find that beacon to answer the next question. 我希望获得我所拥有的所有信标的列表(硬编码到应用程序中,或者动态收集它们),以便我可以轻松地选择随机信标并让用户找到该信标来回答下一个问题。 When I try to hard code in the beacons, the distancing does not work (it just reads 0.0), and when I try to gather them while the app runs, the app will crash or hang whenever the beacons update. 当我尝试在信标中硬编码时,间隔不起作用(它只读取0.0),当我在应用程序运行时尝试收集它们时,应用程序将在信标更新时崩溃或挂起。 I am using the Kontakt Android SDK. 我正在使用Kontakt Android SDK。

I have tried putting for loops in onIBeaconsUpdated to gather from the list of updating beacons and just pick from those randomly, but it will constantly change the beacon that it is looking for, making it impossible for the user to complete. 我已经尝试在onIBeaconsUpdated放置for循环以从更新信标列表中收集并从那些随机选择,但它将不断更改它正在寻找的信标,使用户无法完成。

I have also tried making a list of beacons using the details from beacons on the Kontakt.io web panel, but I must be missing a step that links these generated beacons to the physical beacons or something because listOfHardcodedBeacons.get(0).getDistance() will always return 0.0 no matter which beacon you're looking at or how far you are from the beacon. 我还尝试使用Kontakt.io网络面板上信标的详细信息制作信标列表,但我必须错过将这些生成的信标链接到物理信标或其他东西的步骤,因为listOfHardcodedBeacons.get(0).getDistance()无论你正在寻找哪个信标,或者你离信标有多远,它都会返回0.0。

Please excuse my terrible code: 请原谅我可怕的代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_scanner);
        distanceTV = findViewById(R.id.distanceText);
        setupProximityManager();
        fillBeaconList(listOfHardcodedBeacons);
        randint = random.nextInt(listOfHardcodedBeacons.size());

        super.onCreate(savedInstanceState);
    }

    private void fillBeaconList(List<IBeaconDevice> beaconList) {

        //beacon #1
        BeaconDevice beacon1 = new BeaconDevice.Builder()
                .uniqueId("q0hg")
                .proximityUUID(UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"))
                .major(1)
                .minor(0)
                .address("D3:95:F8:1E:CB:6E")
                .build();

        //beacon #2
        BeaconDevice beacon2 = new BeaconDevice.Builder()
                .uniqueId("eHI1")
                .proximityUUID(UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"))
                .major(1)
                .minor(1)
                .address("CF:FE:16:60:0D:50")
                .build();

        //beacon #3
        BeaconDevice beacon3 = new BeaconDevice.Builder()
                .uniqueId("R9Bq")
                .proximityUUID(UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"))
                .major(1)
                .minor(2)
                .address("CF:81:E3:B6:D1:D0")
                .build();

        //beacon #4
        BeaconDevice beacon4 = new BeaconDevice.Builder()
                .uniqueId("zUiD")
                .proximityUUID(UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"))
                .major(55910)
                .minor(12620)
                .address("DC:65:69:47:93:26")
                .build();

        //beacon #5
        BeaconDevice beacon5 = new BeaconDevice.Builder()
                .uniqueId("QR18")
                .proximityUUID(UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"))
                .major(30785)
                .minor(10106)
                .address("D7:1F:B8:AF:85:B2")
                .build();

        //beacon #6
        BeaconDevice beacon6 = new BeaconDevice.Builder()
                .uniqueId("uhnG")
                .proximityUUID(UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"))
                .major(48261)
                .minor(35926)
                .address("CC:2C:82:59:93:C6")
                .build();

        beaconList.add(beacon1);
        beaconList.add(beacon2);
        beaconList.add(beacon3);
        beaconList.add(beacon4);
        beaconList.add(beacon5);
        beaconList.add(beacon6);
    }

    //TODO: figure out list of known beacons and do stuff
    private IBeaconListener createIBeaconListener() {

        return new IBeaconListener() {
            @Override
            public void onIBeaconDiscovered(IBeaconDevice ibeacon, IBeaconRegion region) {

                //Toast.makeText(getApplicationContext(), String.format("I found beacon %s! :D", ibeacon.getUniqueId()), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onIBeaconsUpdated(List<IBeaconDevice> ibeaconList, IBeaconRegion region) {

                /* This was my original code. It pulls the closest beacon. It isn't what I want because I don't want the user to keep pulling questions from the same beacon.

                String rounded = df.format(ibeaconList.get(0).getDistance());
                distanceTV.setText(String.format("Distance to %s: ", ibeaconList.get(0).getUniqueId()) + rounded);
                if(ibeaconList.get(0).getDistance() <= 2.7432) { //2.7432 = 9 feet
                    distanceTV.setText("Found it! :D");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            toQuestions();
                        }
                    }, 1000);
                    proximityManager.stopScanning();
                }
                */

            }

            @Override
            public void onIBeaconLost(IBeaconDevice ibeacon, IBeaconRegion region) {
                //Toast.makeText(getApplicationContext(), String.format("I lost beacon %s! D:", ibeacon.getUniqueId()), Toast.LENGTH_SHORT).show();
            }

        };
    }

    public void toQuestions() {

        Intent intent = new Intent(this, QuestionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
        finish();
    }

As of now, the code will only pull from the first beacon in ibeaconList and tell the user to go to that one, but that isn't ideal for the goal of the app. 截至目前,代码将仅从ibeaconList的第一个信标中ibeaconList并告诉用户转到那个,但这对于应用程序的目标来说并不理想。 As stated earlier, I want to pull from a random list of predefined beacons and just shuffle through the list randomly. 如前所述,我想从一个随机的预定义信标列表中拉出来,然后随机地在列表中随机播放。 I have written and deleted probably 4 different non functioning solutions to my problem and I'm just not sure what to do at this point. 我已经为我的问题编写并删除了4种不同的非功能解决方案,我只是不确定此时该怎么做。 Any help would be appreciated. 任何帮助,将不胜感激。 I did my best to make this as clear as possible but if you still need more information I will be happy to provide it. 我尽力使这一点尽可能清楚,但如果您仍需要更多信息,我将很乐意提供。

to anyone that may read this, I figured out the answer. 对于任何读过这篇文章的人,我都想出了答案。 What I did was this: 我做的是这样的:

IBeaconFilter filter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_scanner);
        distanceTV = findViewById(R.id.distanceText);
        fillBeaconList(listOfHardcodedBeacons);
        randint = random.nextInt(listOfHardcodedBeacons.size());
        filter = new IBeaconUniqueIdFilter(listOfHardcodedBeacons.get(randint).getUniqueId());

        setupProximityManager();

        super.onCreate(savedInstanceState);
    }

private void setupProximityManager(){
        proximityManager = ProximityManagerFactory.create(this);

        //configure proximity manager basic options
        proximityManager.configuration()
                //using ranging for continuous scanning or MONITORING for scanning with intervals
                .scanPeriod(ScanPeriod.RANGING)
                //using BALANCED for best performance/battery ratio
                .scanMode(ScanMode.LOW_LATENCY)
                //OnDeviceUpdate callback will be received with 1 second interval
                .deviceUpdateCallbackInterval(TimeUnit.MILLISECONDS.toMillis(20));

        //setting up iBeacon and Eddystone spaces listeners
        //proximityManager.setSpaceListener(createSpaceListener());

        //setting up iBeaconListener to only listen for random beacon (filter)
        proximityManager.filters().iBeaconFilter(filter);
        proximityManager.setIBeaconListener(createIBeaconListener());
        proximityManager.setEddystoneListener(new SimpleEddystoneListener() {
        });

    }

This let me keep my existing (commented) code in onIBeaconsUpdated without any messy lists. 这让我在onIBeaconsUpdated保留我现有的(注释)代码而没有任何杂乱的列表。 This solution takes a random int that is created on Activity start, and then uses that int as the index for the list of hardcoded beacons, which is then used as the uniqueID for the filter. 此解决方案采用在Activity start上创建的随机int,然后使用该int作为硬编码信标列表的索引,然后将其用作过滤器的唯一ID。 When the Proximity Manager starts scanning, it will only detect beacons of that uniqueID. 当Proximity Manager开始扫描时,它只会检测该唯一ID的信标。 It was a solution I haven't seen anywhere else, so I am posting here for posterity. 这是我在其他任何地方都没见过的解决方案,所以我在这里张贴后代。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kontakt.io Beacon示例的问题 - Problems with Kontakt.io Beacon example 如何从数组列表中随机抽取一个字符串并将其放入另一个列表中? - How can I randomly pull out a String from an array list and put it in another? 如何从解析中提取对象并将其显示在列表中,而在Android中没有它们的ID? - How can I pull objects from parse and display them in a list without their ID in android? 使用匿名引用,我可以从它们中提取数据吗? - Used anonymous references, can I pull data from them? 从文件中读取一个数组列表,然后将数组列表分成多个字符串,这样我就可以分别解析它们 - Reading in from a file into an arraylist then dividing array list into multiple strings so I can parse them individually 有没有办法可以从 Eclipse IDE 复制带有行号的代码,以便将其粘贴到 Word 文档中? - Is there a way I can copy code from Eclipse IDE with line numbers so I can paste it into a word document? 每当我登录时,我都想一直记录不同的消息。 那么可以在下面的代码中进行哪些更改 - I want to log different messages all the time whenever i log. So what changes can be made in the below code 如何从链表中删除随机选择的元素? - How can I remove a randomly chosen element from a linked list? 如何检查收到的信标是持续 2 分钟的信标? - How can I check that the received beacon is a beacon that last 2 minutes? 如何列出源代码中使用的 Android SDK 中的所有类和方法? - How can I list all classes and methods from Android SDK used in my source code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM