利用GameKit框架实现ios设备的蓝牙通讯,导入框架:#import <GameKit/GameKit.h>  , 注意: 此框架只能用于ios设置间蓝牙通讯

  如今苹果开放了接口来实现不仅限于在苹果设备间的蓝牙通讯,需要导入框架:#import <CoreBluetooth/CoreBluetooth.h>

  首先,利用GameKit框架实现ios设备蓝牙通讯

  基本属性和方法:

  属性

  • 是否可见:visible
  • 蓝牙连接类型:connectionTypesMask

  方法:

  • 显示蓝牙控制器:- (void)show;
  • 隐藏蓝牙控制器:- (void)dismiss;
  • 蓝牙连接成功会调用:- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
  • 取消蓝牙连接:- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
  • 接收到数据会调用:- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
  • 发送数据给特定的对端蓝牙设备:self.session sendData:<#(NSData *)#> toPeers:<#(NSArray *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
  • 发送数据给所有蓝牙设备:self.session sendDataToAllPeers:<#(NSData *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>

  实现:

 #import "WYSViewController.h"
#import <GameKit/GameKit.h> @interface WYSViewController () <GKPeerPickerControllerDelegate> // 会话
@property (nonatomic,strong) GKSession *session; @end @implementation WYSViewController - (void)viewDidLoad
{
[super viewDidLoad]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ // 创建蓝牙控制器
GKPeerPickerController *peerPk = [[GKPeerPickerController alloc] init]; // 代理
peerPk.delegate = self; // 显示蓝牙控制器
[peerPk show];
} #pragma mark - 蓝牙代理方法
// 蓝牙连接成功会调用
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
// 保存回话
self.session = session; // 设置接收者
[self.session setDataReceiveHandler:self withContext:nil]; // 关闭控制器
[picker dismiss];
} // 取消连接调用
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{ } // 接收到数据会调用
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
NSLog(@"%@",data);
} - (IBAction)sendData
{
// 发送数据去某些蓝牙设备
// NSData * :要发送的数据
// NSArray * : 蓝牙设备
// GKSendDataMode:枚举模式
// self.session sendData:<#(NSData *)#> toPeers:<#(NSArray *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#> // 发送数据去所有蓝牙设备
// self.session sendDataToAllPeers:<#(NSData *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
}

  

  利用CoreBluetooth实现蓝牙通讯:

  基本的思路:创建中心设备-->扫描外设-->连接外设-->扫描外设的服务和特征-->利用特征和外设进行数据交互-->断开连接

  基本方法和属性:

  属性:

  • 中心管理设备类:CBCentralManager
  • 中心管理设备代理:id<CBCentralManagerDelegate> delegate
  • 中心管理设备状态:CBCentralManagerState state
  • 外设类:CBPeripheral
  • 服务类:CBService
  • 特征类:CBCharacteristic

  方法:

  • 扫描外设:- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;
  • 停止扫描:- (void)stopScan;
  • 连接外设:- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
  • 外设连接成功调用:- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
  • 外设连接失败调用:- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
  • 扫描到服务就会调用:- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
  • 扫描到特征就会调用:- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

  具体实现:

 #import "WYSViewController.h"
#import <CoreBluetooth/CoreBluetooth.h> @interface WYSViewController () <CBCentralManagerDelegate,CBPeripheralDelegate> // 中心设备
@property (nonatomic,strong) CBCentralManager *cbMgr; // 全部的外部设备
@property (nonatomic,strong) NSMutableArray *Peripherals; @end @implementation WYSViewController // 懒加载
- (NSMutableArray *)Peripherals
{
if (!_Peripherals){ _Peripherals = [NSMutableArray array];
} return _Peripherals;
} - (void)viewDidLoad
{
[super viewDidLoad]; // 创建中心设备
CBCentralManager *cbMgr = [[CBCentralManager alloc] init];
self.cbMgr = cbMgr; // 代理
cbMgr.delegate = self; // 扫描外设
[cbMgr scanForPeripheralsWithServices:nil options:nil];
} #pragma mark - CBCentralManager代理方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// 扫描到得外设
if (![self.Peripherals containsObject:peripheral]){ // 外设代理
peripheral.delegate = self; [self.Peripherals addObject:peripheral];
}
} // 连接外设
- (IBAction)peripheralConnectSuccess
{
for (CBPeripheral *peripheral in self.Peripherals) { // 连接外设
[self.cbMgr connectPeripheral:peripheral options:nil];
}
} // 外设连接成功会调用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
// 扫描服务
[peripheral discoverServices:nil];
} // 外设连接失败 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{ } #pragma mark - Peripheral代理
// 扫描到服务就会调用
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSArray *services = peripheral.services;
for (CBService *service in services) { // 某服务存在
if ([service.UUID.UUIDString isEqualToString:@"xxx"]){ [peripheral discoverCharacteristics:nil forService:service];
}
}
} // 扫描到特征就会调用
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
NSArray *characters = service.characteristics; for (CBCharacteristic *character in characters) { if ([character.UUID.UUIDString isEqualToString:@"xxx"]){ // 进行设置
}
}
}

iOS 蓝牙(GameKit CoreBluetooth)的更多相关文章

  1. iOS蓝牙开发CoreBluetooth快速入门

    在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...

  2. iOS蓝牙4.0开发

    文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...

  3. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  4. https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程

    The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...

  5. iOS蓝牙BLE4.0通信功能

    概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...

  6. ios 蓝牙相关

      ios蓝牙开发项目实战 -(附小米手环实例)   前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮 ...

  7. iOS蓝牙BLE开发

    蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...

  8. iOS 蓝牙开发资料记录

    一.蓝牙基础认识:   1.iOS蓝牙开发:  iOS蓝牙开发:蓝牙连接和数据读写   iOS蓝牙后台运行  iOS关于app连接已配对设备的问题(ancs协议的锅)          iOS蓝牙空中 ...

  9. iOS蓝牙APP常驻后台

    iOS蓝牙类APP常驻后台的实现方法,经过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为: 1.设置plist,蓝牙权限 2.到target-capabilities-backgr ...

随机推荐

  1. qt安装

    在以下网页选择一个国内的下载地址即可 http://download.qt.io/official_releases/qt/5.7/5.7.0/qt-opensource-linux-x64-5.7. ...

  2. JAVA高并发秒杀API项目的学习笔记

    一步一步的搭建JAVA WEB项目,采用Maven构建,基于MYBatis+Spring+Spring MVC+Bootstrap技术的秒杀项目学习的视频:http://www.imooc.com/l ...

  3. Collections练习之按照字符串长度进行排序

    不多说,直接上干货! 代码需求 想从 [abcde, cba, aa, zzz, cba, nbaa] 变成 [aa, cba, cba, zzz, nbaa, abcde] CollectionsD ...

  4. linux下追查线上问题常用命令

    (1)查占用cpu最多的进程方法一:核心指令:ps实际命令:ps H -eo pid,pcpu | sort -nk2 | tail执行效果如下:[work@test01 ~]$ ps H -eo p ...

  5. Spring Boot使用mongo的GridFS模块

    1. GridFS简介 GridFS是Mongo的一个子模块,使用GridFS可以基于MongoDB来持久存储文件.并且支持分布式应用(文件分布存储和读取).作为MongoDB中二进制数据存储在数据库 ...

  6. Jersey实现文件上传下载

    一 文件上传 使用ajaxFileUpload进行文件上传的前端处理.在ajaxFileupload.js中,针对服务端返回的类型增加text判断, //ajax文件上传 function ajaxF ...

  7. Geek to Live: Set up your personal Wikipedia

    http://lifehacker.com/163707/geek-to-live--set-up-your-personal-wikipedia Filed to: Wikipedia Captur ...

  8. MVC之ViewData.Model

    在MVC中前台Razor视图呈现数据的方式不止一种.举个简单的Demo,我们要把用户信息呈现给人民. 一.ViewData.Model的使用,先简单写一下Razor @model   User---- ...

  9. maven课程 项目管理利器-maven 3-5 maven生命周期和插件 4星

    本节重点: maven插件的使用 本节主要内容: 1 maven生命周期 2 maven插件的使用 3 零散知识点 1 maven生命周期  maven生命周期主要有三个: a clean 清理项目 ...

  10. 学习路线 roadmap

    我的学习路线为HTML > CSS > Javsscript:Javascript是前端一切学习的基础.HTML和css一起学习. JavaScript基础: Js基础教程.js内置对象常 ...