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
到此全部结束,有错误的地方 欢迎纠正。
/***********************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
到此全部结束,有错误的地方 欢迎纠正。
相关文章推荐
- unity, iOS集成微信
- 集成讯飞听写iOS sdk到unity遇到的问题:weak成员和strong成员
- Unity项目工程集成友盟分析统计SDK(支持iOS与Android平台)
- IOS 和 Android Unity游戏引擎的集成AdMob新版教程
- Unity集成内嵌网页插件-支持Android和IOS
- unity, 集成iOS广告sdk注意事项
- [置顶] Unity中集成ShareSDK(3.X)的功能-IOS平台
- [Unity2d系列教程] 004.Unity如何调用ios的方法(SDK集成相关)
- Unity项目工程集成Mob的社会化分享SDK之发布到iOS平台
- IOS版Unity的Google Analytics(分析)插件集成Admob管理库教程
- Unity开发IOS游戏的优化建议
- Unity与ASP.NET的集成(官方方法)
- WCF后续之旅(7):通过WCF Extension实现和Enterprise Library Unity Container的集成
- Unity的IOS界面风格插件Igui
- UnityForIOS
- [原创]WCF技术剖析之七:如何实现WCF与EnterLib PIAB、Unity之间的集成
- 如何在macox下面配置集成ios和android游戏教程
- WCF后续之旅(7):通过WCF Extension实现和Enterprise Library Unity Container的集成
- Unity开发IOS游戏的优化建议
- iOS 5.1测试版可让开发者有限集成Siri