Java 信标和涡流检测问题

Java 信标和涡流检测问题,java,android-studio,beacon,Java,Android Studio,Beacon,问题在于检测IBeacon和Eddystone帧,当我有Eddystone时,我必须使用特定的函数,IBeacon也是如此。 我在Android Studio中使用Java,因此向我提供IBeacon结果的函数是GetManufactureSpecificData(),而Eddystone的函数是getServiceData()。 问题是为什么会有不同的功能产生结果?如何检查接收到的帧是Eddystone还是IBeacon 代码片段 public void onBatchScanResu

问题在于检测IBeacon和Eddystone帧,当我有Eddystone时,我必须使用特定的函数,IBeacon也是如此。 我在Android Studio中使用Java,因此向我提供IBeacon结果的函数是GetManufactureSpecificData(),而Eddystone的函数是getServiceData()。 问题是为什么会有不同的功能产生结果?如何检查接收到的帧是Eddystone还是IBeacon

代码片段

    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
        beaconManager.clean();
        Log.i(TAG, "On batch called, size of result is : " + results.size());

        for (int index = 0; index < results.size(); ++index) {
            try {
                ScanRecord mScanRecord = results.get(index).getScanRecord();
                Map<ParcelUuid, byte[]> myMap = mScanRecord.getServiceData();
                int mRsi = results.get(index).getRssi();
                String url = "";
                byte[] txPower = new byte[1];
                byte[] nameSpaceId = new byte[10];
                byte[] instanceId = new byte[6];
                float distance;
                
                for (Map.Entry<ParcelUuid, byte[]> eddystoneFrame : myMap.entrySet()) {
                    // for eddystone URL
                    if (eddystoneFrame.getValue()[0] == 16) {
                        url += urlSchemePrefix[eddystoneFrame.getValue()[2]];

                        for (int i = 3; i < eddystoneFrame.getValue().length - 1; i++) {
                            url += (char) eddystoneFrame.getValue()[i];
                        }

                        url += topLevelDomain[eddystoneFrame.getValue()[eddystoneFrame.getValue().length - 1]];
                        txPower[0] = eddystoneFrame.getValue()[1];
                        distance = (float) Utils.calculateDistance((int) txPower[0], (double) mRsi);
                        
                        try {
                            beaconManager.addEddyBeacon(Utils.bytesToHex(nameSpaceId), Utils.bytesToHex(instanceId), distance);
                        } catch (Exception e) {
                            Log.e(TAG, e.toString());
                        }

                    }
                    else if (eddystoneFrame.getValue()[0] == 0) {
                        // For Eddystone UID
                        System.arraycopy(eddystoneFrame.getValue(), 2, nameSpaceId, 0, nameSpaceId.length);
                        System.arraycopy(eddystoneFrame.getValue(), 12, instanceId, 0, instanceId.length);
                        System.arraycopy(eddystoneFrame.getValue(), 1, txPower, 0, txPower.length);

                        distance = (float) Utils.calculateDistance((int) txPower[0], (double) mRsi);

                        /**  beaconList.add(new String[]{"Name Space ID : " + Utils.bytesToHex(nameSpaceId)+ "\n" + "Instance ID :" + Utils.bytesToHex(instanceId),
                         String.format("%.2f", distance),
                         "eddystoneuid"});**/
                        try {
                            beaconManager.addEddyBeacon(Utils.bytesToHex(nameSpaceId), Utils.bytesToHex(instanceId), distance);
                        } catch (Exception e) {
                            Log.e(TAG, e.toString());
                        }

                    }
                    Log.i(TAG, "The size of the frame is: " + eddystoneFrame.getValue().length);
                }


            } catch (Exception e) {
                Log.e("Error123456789", e.toString());
                break;
            }
        }
public void onBatchScanResults(列出结果){
super.onBatchScanResults(结果);
beaconManager.clean();
Log.i(标记,“在调用批处理时,结果的大小为:“+results.size()”);
对于(int index=0;index
蓝牙SIG标准机构定义了两种蓝牙LE广告:

  • 关贸总协定服务广告-这些广告旨在宣传关贸总协定服务,可能有服务数据字节,旨在告诉您有关广告服务的信息
  • 制造商广告-这些广告设计用于蓝牙设备制造商希望使用的任何目的。它们始终包含0-24字节的制造商数据
  • 当苹果设计iBeacon格式时,它选择使用制造商广告。因此,每当你检测到它们时,你必须获得制造商数据来解码内容

    当谷歌设计Eddystone时,它选择不使用制造商广告,而是使用GATT服务广告。之所以选择这种格式,是因为它希望这种格式在iOS设备上运行良好,而iOS通常拒绝让第三方应用程序在后台检测制造商广告(iBeacon除外)

    由于谷歌的这一设计决定,在尝试解码Eddystone广告时,您必须检查是否存在服务数据

    如果要检测这两种信标类型,请使用如下逻辑:

  • 是否有附加的服务数据?如果没有,请转至步骤3
  • 服务数据是否可以解码为Eddystone?如果可以,我们就完成了
  • 该广告是否为制造商广告?如果否,请转至步骤5
  • 是否可以在iOS上解码制造商数据,如果是,我们就完成了
  • 这则广告既不是艾迪斯通也不是伊贝康

  • 抱歉,我不明白这个问题。如何比较类型?
    instanceof
    getScanRecord提供的结果,如何检查帧类型?否则是IBeacon帧还是Eddystone帧?结果是一个列表。列表不是帧。是的,我知道,但结果可以通过使用所述函数之一为我提供帧负载。为了让问题更清楚,onBatchResult中接收的results参数是一个result对象列表,每个对象都有关于帧的信息,因此使用这些函数可以在不知道检测到哪种信标的情况下提供有效负载。