ios端ibeacon 集成到unity

背景:项目要实现unity3d调用ibeacon小范围内定位的功能,无奈直接集成在unity里的ibeacon发布ios版之后,只要ibeacon设备灯不亮就获取不到范围,多次调试未果,最终决定从ios端集成,再通过unity调用,亲测成功,简单记录一下。直接贴代码

/***********************oc端代码**********************/

//  ibeacon.mm

//  ibeacon

//

//  Created by 他三叔,走啊 on 16/8/23.

//  Copyright © 2016 他三叔,走啊. All rights reserved.

//


#import "ibeacon.h"

#import <CoreLocation/CoreLocation.h>

#import<CoreBluetooth/CoreBluetooth.h>

#import "Reachability.h"


#define MY_REGION_IDENTIFIER @"my region"

#define MY_UUID @"5E96BDB3-89B0-4999-85CB-E622CA8A84A8"

@interface ibeacon : NSObject<CLLocationManagerDelegate,CBPeripheralManagerDelegate>

{

    CLLocationManager *_locationManager;

    CLBeaconRegion *_region;

    NSString *bluetooth;

    NSString *isNetWork;

    NSString *device;

}

@property (nonatomic,strong) CBPeripheralManager *manager;


- (void) turnOnBeacon:(NSString *)uuid;



@end

ibeacon *beacon = NULL;


#if defined(__cplusplus)

extern "C"{

#endif

    void pushUUID()

    {


        if(beacon == NULL)

        {

            beacon = [[ibeacon alloc]init];

            [beacon turnOnBeacon:MY_UUID];

        }

        

    }

    

#if defined(__cplusplus)

}

#endif


@implementation ibeacon

#pragma mark - Beacons Methods

- (void) turnOnBeacon:(NSString *)uuid{

    device = [[UIDevice currentDevice] model];

    

    Reachability *r = [Reachability reachabilityWithHostName:@"www.3dddly.com"];

    switch ([r currentReachabilityStatus]) {

        case NotReachable:

            // 没有网络连接

            isNetWork = @"false";

            break;

        case ReachableViaWWAN:

            // 使用3G网络

            isNetWork = @"true";

            break;

        case ReachableViaWiFi:

            // 使用WiFi网络

            isNetWork = @"true";


            break;

    }

    self.manager=[[CBPeripheralManager alloc]initWithDelegate:self queue:nil];


    [self initLocationManager];

    [self initBeaconRegion:uuid];

    [self startBeaconRanging];

}

#pragma mark Init Beacons

- (void) initLocationManager{

    if (!_locationManager) {

        _locationManager = [[CLLocationManager alloc] init];

        _locationManager.delegate = self;

        [self checkLocationAccessForRanging];

    }

}



- (void) initBeaconRegion:(NSString *)uuid{

    if (_region)

        return;

    

    NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:uuid];

    _region = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:MY_REGION_IDENTIFIER];

    _region.notifyEntryStateOnDisplay = YES;

    

}


#pragma mark Beacons Ranging


- (void) startBeaconRanging{

    if (!_locationManager || !_region) {

        return;

    }

    if (_locationManager.rangedRegions.count > 0) {

        return;

    }

    

    [_locationManager startRangingBeaconsInRegion:_region];

    

}


- (void) stopBeaconRanging{

    if (!_locationManager || !_region) {

        return;

    }

    [_locationManager stopRangingBeaconsInRegion:_region];

}


//Location manager delegate method

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{

    if (beacons.count == 0) {

        NSLog(@"没有搜到设备信息");

    } else {

        //_detectedBeacons = beacons;

        NSLog(@"设备数量:%lu", (unsigned long)beacons.count);

        for (CLBeacon *beacon in beacons) {

          

            NSString *str = [self detailsStringForBeacon:beacon];

            

            UnitySendMessage("ARCamera", "GetResult",[str UTF8String]);   //向unity发送消息的方法

                       

        }

    }

}

//beacon的信息转换为NSString并返回

- (NSString *)detailsStringForBeacon:(CLBeacon *)beacon

{

    

    NSString *format = @"%@|%@|%li|%@|%@|%@";

    return [NSString stringWithFormat:format, beacon.major, beacon.minor, beacon.rssi,bluetooth,isNetWork,device];

    

}


#pragma mark - CBPeripheralManagerDelegate


- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral

{

    switch (peripheral.state) {

            

        case CBPeripheralManagerStatePoweredOn:

            

        {

            

            NSLog(@"蓝牙开启且可用");

            bluetooth = @"true";

            

        }

            

            break;

            

        default:

            

            NSLog(@"蓝牙不可用");

            bluetooth = @"false";


            break;

            

    }


}



- (void)checkLocationAccessForRanging {

    if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [_locationManager requestWhenInUseAuthorization];

    }

}

@end

到此 oc处理结束    下面是unity调用代码

/***********************unity代码**********************/


using System.Runtime.InteropServices; 


public class mytest : MonoBehaviour {

    [DllImport("__Internal")]

    private static extern void pushUUID();
    #endif

以上是做的引用和声明

接下来在你需要的地方 调用

        #if UNITY_IPHONE || UNITY_TVOS
        pushUUID();
        #else

到此全部结束,有错误的地方 欢迎纠正。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页