iOS蓝牙调用的一般流程
一、服务端(也叫周边设备吧。。脑残的翻译)
1.实现类必须遵守协议 CBPeripheralManagerDelegate
2.需要的主要类有:
@property(strong,nonatomic) CBPeripheralManager *peripheraManager;
@property(strong,nonatomic) CBMutableCharacteristic *customerCharacteristic;
@property (strong,nonatomic) CBMutableService *customerService;
3.调用流程代码中有注释
//
// ViewController.m
// BlueToothDemo
//
// Created by PSH_Chen_Tao on 7/3/13.
// Copyright (c) 2013 wolfman. All rights reserved.
// #import "ViewController.h"
static NSString *const kCharacteristicUUID = @"CCE62C0F-1098-4CD0-ADFA-C8FC7EA2EE90"; static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-F62016F26E7B";
@interface ViewController () @end @implementation ViewController @synthesize peripheraManager;
@synthesize customerCharacteristic;
@synthesize customerService;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化后会直接调用代理的 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral peripheraManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];
// [peripheraManager startAdvertising:nil]; } -(void)setUp{
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
customerCharacteristic = [[CBMutableCharacteristic alloc]initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
customerService = [[CBMutableService alloc]initWithType:serviceUUID primary:YES];
[customerService setCharacteristics:@[characteristicUUID]];
//添加后就会调用代理的- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
[peripheraManager addService:customerService]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark -- CBPeripheralManagerDelegate
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
switch (peripheral.state) {
//在这里判断蓝牙设别的状态 当开启了则可调用 setUp方法(自定义)
case CBPeripheralManagerStatePoweredOn:
NSLog(@"powered on");
[self setUp];
break;
case CBPeripheralManagerStatePoweredOff:
NSLog(@"powered off");
break; default:
break;
}
} - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{
if (error == nil) {
//添加服务后可以在此向外界发出通告 调用完这个方法后会调用代理的
//(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
[peripheraManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"Service",CBAdvertisementDataServiceUUIDsKey : [CBUUID UUIDWithString:kServiceUUID]}];
} } - (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{
NSLog(@"in peripheralManagerDidStartAdvertisiong:error");
}
@end
二、客户端(也叫中心设备吧)
1.实现类要遵守协议<CBCentralManagerDelegate,CBPeripheralDelegate>
2.主要用到的类有
@property(strong,nonatomic)CBCentralManager *centralManager;
@property(strong,nonatomic)NSMutableData *mutableData;
@property(strong,nonatomic)CBPeripheral *peripheral;
3.一般的流程
//
// ViewController.m
// BlueToothClient
//
// Created by PSH_Chen_Tao on 7/3/13.
// Copyright (c) 2013 wolfman. All rights reserved.
// #import "ViewController.h"
static NSString *const kCharacteristicUUID = @"CCE62C0F-1098-4CD0-ADFA-C8FC7EA2EE90"; static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-F62016F26E7B";
@interface ViewController () @end @implementation ViewController @synthesize centralManager;
@synthesize mutableData;
@synthesize peripheral; - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化后会调用代理CBCentralManagerDelegate 的 - (void)centralManagerDidUpdateState:(CBCentralManager *)central
centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark -- CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
//判断状态开始扫瞄周围设备 第一个参数为空则会扫瞄所有的可连接设备 你可以
//指定一个CBUUID对象 从而只扫瞄注册用指定服务的设备
//scanForPeripheralsWithServices方法调用完后会调用代理CBCentralManagerDelegate的
//- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI方法
case CBCentralManagerStatePoweredOn:
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : YES}]; break; default:
break;
}
} - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
if (peripheral) {
self.peripheral = peripheral;
//发现设备后即可连接该设备 调用完该方法后会调用代理CBCentralManagerDelegate的- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral表示连接上了设别
//如果不能连接会调用 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
[centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : YES}];
}
} - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"has connected");
[mutableData setLength:];
self.peripheral.delegate = self;
//此时设备已经连接上了 你要做的就是找到该设备上的指定服务 调用完该方法后会调用代理CBPeripheralDelegate(现在开始调用另一个代理的方法了)的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
[self.peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
//此时连接发生错误
NSLog(@"connected periphheral failed");
} #pragma mark -- CBPeripheralDelegate
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
if (error==nil) {
//在这个方法中我们要查找到我们需要的服务 然后调用discoverCharacteristics方法查找我们需要的特性
//该discoverCharacteristics方法调用完后会调用代理CBPeripheralDelegate的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
for (CBService *service in peripheral.services) {
if ([service.UUID isEqual:[CBUUID UUIDWithString:kServiceUUID]]) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kCharacteristicUUID]] forService:service];
}
}
}
} - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error==nil) {
//在这个方法中我们要找到我们所需的服务的特性 然后调用setNotifyValue方法告知我们要监测这个服务特性的状态变化
//当setNotifyValue方法调用后调用代理CBPeripheralDelegate的- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) { [peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
} - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error==nil) {
//调用下面的方法后 会调用到代理的- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
[peripheral readValueForCharacteristic:characteristic];
}
} - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ } @end
iOS蓝牙调用的一般流程的更多相关文章
- iOS蓝牙开发
蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- ios 蓝牙相关
ios蓝牙开发项目实战 -(附小米手环实例) 前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮 ...
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)
转载自:http://www.cocoachina.com/ios/20151106/14072.html 原文作者:刘彦玮 BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBlue ...
- IOS 蓝牙相关-BabyBluetooth蓝牙库介绍(4)
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- ios蓝牙开发(四)BabyBluetooth蓝牙库
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程
The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...
- iOS蓝牙BLE开发
蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...
随机推荐
- Moq 和RhinoMocks
Moq & RhinoMocks 使用Mock对象进行测试一般都会有以下三个关键步骤: 使用接口来描述需要测试的对象 为实际的产品代码实现这个接口 以测试为目的,在Mock对象中实现这个接口 ...
- Web监控工具
.Rabbitmq的使用及Web监控工具使用 一.文档资料 1.官方网站:http://www.rabbitmq.com/ 2.安装教程:http://www.rabb ...
- jsp的<%%>
于jsp于 可以使用<% %> 嵌入java代码,简称jsp文字. 可以使用<% -- -- %> 凝视,这是jsp注视 可以使用<%! %> 声明全局变量 版权声 ...
- 证明N={1,2,...,n,...}最高万元 黄晓宁
证明N={1,2....,n....}最高万元 黄小宁(通讯:广州市华南师大南区9-303 510631) 5000年数学一直不知{2,3,....n+1,...}(n的变域是N)中"深 ...
- 【高德地图API】从零开始学高德JS API(三)覆盖物——标注|折线|多边形|信息窗口|聚合marker|麻点图|图片覆盖物
原文:[高德地图API]从零开始学高德JS API(三)覆盖物——标注|折线|多边形|信息窗口|聚合marker|麻点图|图片覆盖物 摘要:覆盖物,是一张地图的灵魂.有覆盖物的地图,才是完整的地图.在 ...
- Redis源代码分析(二十七)--- rio制I/O包裹
I/O每个操作系统,它的一个组成部分.和I/O业务质量,在一定程度上也影响了系统的效率. 今天,我在了解了Redis中间I/O的,相同的,Redis在他自己的系统中.也封装了一个I/O层.简称RIO. ...
- 阅读安卓在线(Android)系统源代码
http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/ 版权声明:本文博客原创 ...
- TRS_WCM(拓尔思信息技术有限公司)内容协作平台平台置标经验攻略
TRS_WCM置标过程中经验积累 版本V4.0-2014.6.24-穿越者7号 目录 1.嵌套模板置标 1 2.栏目名称超链接置标 1 3.列表循环输出文档标题包含超链接 1 4.取既定栏目下第一篇文 ...
- WPF应用程序支持多国语言解决方案
原文:WPF应用程序支持多国语言解决方案 促使程序赢得更多客户的最好.最经济的方法是使之支持多国语言,而不是将潜在的客户群限制为全球近70亿人口中的一小部分.本文介绍四种实现WPF应用程序支持多国语言 ...
- Linq to Sql:N层应用中的查询(上) : 返回自定义实体
原文:Linq to Sql:N层应用中的查询(上) : 返回自定义实体 如果允许在UI层直接访问Linq to Sql的DataContext,可以省去很多问题,譬如在处理多表join的时候,我们使 ...