利用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. maya卸载不干净

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  2. jquery dataTable 自定义 Button及按钮事件

    参考网址:http://stackoverflow.com/questions/18134913/jquery-datatabletabletool-custom-buttons-calling-ev ...

  3. Oozie安装部署

    不多说,直接上干货! 首先,大家先去看我这篇博客.对于Oozie的安装有一个全新的认识. Oozie安装的说明 我这里呢,本篇博文定位于手动来安装Oozie,同时避免Apache版本的繁琐编译安装,直 ...

  4. Elasticsearch简单运算

    求平均数 { "query": { "bool": { "must": [ { "term": { "stor ...

  5. JavaFX--第3天窗口布局

    1.windows之间的交互 2.关闭程序 3.布局镶嵌 1.windows之间的交互 我们要实现“确定”.“取消”之类的功能:就像我们平时使用Word的时候要关闭会提示要不要保存的信息. 步骤如下: ...

  6. 斗鱼扩展--notifications提示(十二)

    来说下 桌面通知 Notification,HTML5支持 Web Notifications 的实例,但是要经过用户允许,  chrome://settings/content/notificati ...

  7. 内存分配详解 malloc, new, HeapAlloc, VirtualAlloc,GlobalAlloc

    很多地方都会使用内存,内存使用过程中操作不当就容易崩溃,无法运行程序,上网Google学习一下,了解整理下他们之间的区别以及使用 ,获益匪浅 0x01 各自的定义和理解 (1)先看GlobalAllo ...

  8. Redis入门--(二)Jedis的入门

    Jedis相应的jar包 编写一段程序来测试一下 1.新建一个Java的项目 2.引入jedis开发包 3.将包添加到构建路径中 4.创建一个测试类 5.创建一个Jedis的单实例的测试

  9. Redis入门--(二)Redis的概述

    1.Redis的由来 创始人觉得Mysql不好用,就自己写了: 国内使用Redis的网站有新浪微博,知乎: 国外GitHub: VMWare也支持redis的开发 2.Redis的概述 官方提供的测试 ...

  10. Could not find or load main class Hello

    在 linux 下写了一个非常简单的 Hello World 程序,编译正常,运行报错:Error: Could not find or load main class Hello 这是由于 CLAS ...