您的位置:首页 > 移动开发 > Unity3D

ios端ibeacon 集成到unity

2016-09-09 09:23 399 查看
背景:项目要实现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;
        [<
4000
span class="s5">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

到此全部结束,有错误的地方 欢迎纠正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS unity3d