微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我无法使用 Swift 检测到任何 iBeacon,而 Objective C 可以?

如何解决为什么我无法使用 Swift 检测到任何 iBeacon,而 Objective C 可以?

因此,我一直在努力检测办公室中可用的 iBeacon。我上网冲浪,总能找到一个解决方案,您需要知道 iBeacons 的 UUID 才能检测到它。但是,我在 github 上发现了这个(向这个人大喊大叫)。我在办公室运行了这个应用程序,它能够检测到 2 个可用的 iBeacons(其中 1 个具有动态 UUID),但它是用目标 C 编写的。

在目标 c 上,它怎么可能检测到周围的 iBeacons 而 Swift 不能?

我猜这是开始扫描 iBeacon 的 Objective C 代码(我之前从未学过 obj c):

AIBBeaconRegionAny *beaconRegionAny = [[AIBBeaconRegionAny alloc] initWithIdentifier:@"Any"];
[self.locationManager startRangingBeaconsInRegion:beaconRegionAny];

这是我在 Swift 中的代码

locationManager?.startRangingBeacons(satisfying: CLBeaconIdentityConstraint(uuid: UUID(uuidString: "Any")!,major: 9,minor: 2)).

好吧,它当然崩溃了,我也将其更改为 "11111111-1111-1111-1111-111111111111" 并打印了信标,但没有找到。

谁能解释一下我错过了什么?或者现在真的有可能吗?

提前致谢

更新:

还有 AIBBeaconRegionAny 可以检测任何信标。

文件AIBBeaconRegionAny.h

#import <Foundation/Foundation.h>

#import <CoreLocation/CoreLocation.h>

@interface AIBBeaconRegionAny : CLBeaconRegion

- (id)initWithIdentifier:(Nsstring *)identifier;

@end

文件AIBBeaconRegionAny.m

#import "AIBBeaconRegionAny.h"

// https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/CoreLocation.framework/CLRegion.h
struct ClientRegion {
    BOOL identifier[512];
    BOOL onBehalfOfIdentifier[512];
    int type;
    bool notifyOnEntry;
    bool notifyOnExit;
    bool conservativeEntry;
    union {
        struct {
            BOOL proximityUUID[512];
            unsigned short major;
            unsigned short minor;
            int deFinitionMask;
            bool notifyEntryStateOndisplay;
        } beaconAttributes;
        struct {
            struct {
                double latitude;
                double longitude;
            } center;
            double radius;
            double desiredAccuracy;
            int referenceFrame;
        } circularAttributes;
    } ;
};

@interface CLBeaconRegion (Hidden)

- (id)initWithIdentifier:(Nsstring *)identifier;
- (struct ClientRegion)clientRegion;

@end

@implementation AIBBeaconRegionAny

- (id)initWithIdentifier:(Nsstring *)identifier;
{
    return (self = [super initWithIdentifier:identifier]);
}

- (struct ClientRegion)clientRegion
{
    struct ClientRegion clientRegion = [super clientRegion];
    
    // deFinitionMask:
    //                  1 => uuid
    //                  3 => uuid + major
    //                  7 => uuid + major + minor
    
    clientRegion.beaconAttributes.deFinitionMask = ~0x07;
    
    return clientRegion;
}

@end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。