利用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. express --- session详解

    之前一直做前端相关的工作,所以不太清楚session,也没有主动了解,最近在学node,对session的认识又有所加深,故总结之. 注: 关于session的一些配置问题,可以看这里. 第一部分: ...

  2. Qt 学习(4)

    Qt UI 文件机制 使用 Qt 设计界面程序时,若界面是静态的,可以借助 Qt Designer 进行所见即所得的界面设计.设计好界面后,在界面类中对 ui 对象进行操作非常方便. QtCreato ...

  3. HDU 5419——Victor and Toys——————【线段树|差分前缀和】

    Victor and Toys Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others ...

  4. phpstorm 配置 webserver ,配置根目录

    原文链接    http://blog.csdn.net/pony_maggie/article/details/52367093 phpstorm自带了一个web server,我们可以直接在IDE ...

  5. 使用ajax获取用户所在地的天气

    1.要获取用户归属地的天气,首先得获取用户所在的市区, 这里先获取用户的IP,通过IP获取IP的归属地,从而得到用户 地址. 获取客户端ip: js: <scripttype="tex ...

  6. 最小白的webpack+react环境搭建

    本文也同步发表在我的公众号“我的天空” 从零开始,用最少的配置.最少的代码.最少的依赖来搭建一个最简单的webpack+react环境. 最近在玩webpack+react+移动端,那么第一步自然是搭 ...

  7. java之finally的用法

    package com.smbea.demo.tryCatchFinally; /** * java之finally的用法 * @author hapday * @2017年2月5日 @上午12:21 ...

  8. python反爬之用户代理

    # requests是第三方库,需要安装 pip install requests import requests import random # 通常很多网站都会设置检测请求头中的User-Agen ...

  9. [转]C# 单例模式

    最近在学设计模式,学到创建型模式的时候,碰到单例模式(或叫单件模式),现在整理一下笔记. 在<Design Patterns:Elements of Resuable Object-Orient ...

  10. python模块详解 re

    摘自:python中的正则表达式(re模块) 一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被 ...