提示:本文主要是讲解蓝牙中心管理者和外设管理者之间的通讯过程,没有实际的意义,因为传输受字节限制,效率低。
老样子,先附上效果图和Demo
Demo
https://github.com/chenfanfang/CollectionsOfExample
运行程序
1、首先得准备两部苹果手机,必须支持BLE, >=iPhone 4s即可。
2、将程序运行到两部手机上
3、进入 “聊天(CoreBluetooth实战1)”
4、一部手机进入中心模式、另一部手机进入外设模式 (记得两部手机都要开启蓝牙哦)
写在前面的废话
由于懒,时隔大半年没写技术文章了,今天心血来潮来发表一篇文章。由于以前没有做过蓝牙的项目,好奇心也蛮强的,想知道蓝牙之间的通讯是怎样的,早就在去年6月份的时候开始看蓝牙的资料,但是基本都是断断续续,没有一股气完成对蓝牙的研究(当初还买了蓝牙开发板,并且傻瓜式地将程序烧录进去,就是效果还没完全写出来,等有时间会写一篇真正蓝牙实战的文章,你也别问我为嘛不用LightBlue,因为那时我没有第二部手机并且觉得实物实战比较好)。本文demo核心内容在去年9月份左右的时候完成的(参照了几个demo,由于时隔太久,无法将所参照的demo所在连接找出来),但是对界面美化要求程度比较高,一直没有发布出来,昨天套了个即时通讯界面的框架JSQMessagesViewController中demo的界面,感觉界面效果还行,称热打铁,写一篇相关文章。我已经将蓝牙通讯的相关代码和界面相关代码分离开来,不会影响大家对蓝牙代码的阅读。由于demo中注释比较详细,本文就不做过多的阐述,直接附上代码。
相关蓝牙资料
若对蓝牙不是太了解的朋友,在此推荐几篇蓝牙相关博客
iOS蓝牙开发(一)蓝牙相关基础知识
iOS蓝牙开发(二)ios连接外设的代码实现
iOS蓝牙开发(三)app作为外设被连接的实现
中心管理者
中心管理者相关流程:(以下流程摘抄自一个demo)
1,主流成:程序之行,就进入回调函数centralManagerDidUpdateState,这个相当于起步函数
2,半主流程:上一步中,centralManager调用scan函数,向周边搜寻服务
3,当扫描到时,会触发centralManager的代理的centralManager:didDiscoverPeripheral函数,赋值周边成员,并试图与其建立连接
4,如果建立建立连接失败,会调用代理的centralManager:didFailToConnectPeripheral:函数
4,如果成功,会触发centralManager:didConnectPeripheral:,这时候讲viewc设置为周边成员的代理
5,在上部的回调函数中,调用周边的discoverServices方法,去发现服务
6,当周边发现服务时,会触发周边的代理的peripheral:didDiscoverServices:
7,在上述回调函数中,周边针对每一个发现的服务去搜寻对应特征
8,发现对应特征后是,会触发周边的代理的peripheral:didDiscoverCharacteristicsForService:函数
9,在上部的回调函数中,判断发现的特征是否是感兴趣的特征,如果是,则通过设定特征通知状态为真,来预订该特征。此时会触发服务端didSubscribeToCharacteristic函数。
10,当周边的特征通知状态发生变化时,会触发周边代理的peripheral:didUpdateNotificationStateForCharacteristic:,并没什么卵用,这个回调函数。
10,服务端的调用updatevalue函数,会触发客户端的didUpdateValueForCharacteristic:函数,从而获得服务端传递的字符串
另外,中央管理器调用取消周边连接函数,会触发,中央管理器代理的centralManager:didDisconnectPeripheral方法,用于对断开连接后进行后续处理
中心管理者(CBCentralManager)相关代码如下
//
// CBCentralManagerController.m
// CoreBluetooth_Demo
//
// Created by mac on 16/9/9.
// Copyright © 2016年 chenfanfang. All rights reserved.
//
#import "CBCentralManagerController.h"
#import
//可通过终端命令 uuidgen来生成
#define TRANSFER_SERVICE_UUID @"D63D44E5-E798-4EA5-A1C0-3F9EEEC2CDEB"
#define TRANSFER_CHARACTERISTIC_UUID @"1652CAD2-6B0D-4D34-96A0-75058E606A98"
@interface CBCentralManagerController ()
/** 中心管理者 */
@property (strong, nonatomic) CBCentralManager *centralManager;
/** 发现的外设 */
@property (strong, nonatomic) CBPeripheral *discoveredPeripheral;
/** 当前的特征 */
@property (nonatomic, strong) CBCharacteristic *characteristic;
/** 数据 */
@property (strong, nonatomic) NSMutableData *data;
@end
@implementation CBCentralManagerController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"中心模式";
// 设置CBCentralManager
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
// 保存接收数据
_data = [[NSMutableData alloc] init];
//菊花转动
[self.activityIndicatorView startAnimating];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.centralManager stopScan];
[self cleanup];
[self.activityIndicatorView stopAnimating];
self.centralManager = nil;
NSLog(@"扫描停止");
}
- (void)dealloc {
NSLog(@"%@控制器销毁成功,无内存泄漏",NSStringFromClass([self class]));
}
//=================================================================
// CBCentralManagerDelegate
//=================================================================
#pragma mark - CBCentralManagerDelegate
//设置成功回调方法
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请打开您的蓝牙" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
return;
}
//开始扫描
[self scan];
}
/** 通过制定的128位的UUID,扫描外设
*/
- (void)scan {
[self.activityIndicatorView startAnimating];
//扫描
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
NSLog(@"正在扫描外设");
}
/** 停止扫描
*/
- (void)stop {
[self.activityIndicatorView stopAnimating];
[self.centralManager stopScan];
NSLog(@"停止扫描外设");
}
//扫描成功调用此方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"发现外设 %@ at %@", peripheral.name, RSSI);
if (self.discoveredPeripheral != peripheral) {
self.discoveredPeripheral = peripheral;
NSLog(@"连接外设 %@", peripheral);
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"连接失败 %@. (%@)", peripheral, [error localizedDescription]);
[self cleanup];
}
//连接外设成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
[self stop];
NSLog(@"停止扫描外设");
[self.data setLength:0];//重置data属性
peripheral.delegate = self;//设置外设对象的委托为self
NSLog(@"外设已连接,正在搜寻服务...");
[peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]];
}
//=================================================================
// CBPeripheralDelegate
//=================================================================
#pragma mark - CBPeripheralDelegate
//发现服务成功
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
[self cleanup];
return;
}
NSLog(@"成功发现服务,正在搜寻特征...");
// 发现特征
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];
}
}
//发现特征成功
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
NSLog(@"发现特征错误: %@", [error localizedDescription]);
[self cleanup];
return;
}
NSLog(@"成功发现特征,正在预定特征...");
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
// 预定特征
[peripheral setNotifyValue:YES forCharacteristic:characteristic];//触发服务端(外设)didSubscribeToCharacteristic函数
self.characteristic = characteristic;
[self.activityIndicatorView stopAnimating];
NSLog(@"找到需要的特征,预定成功");
}
}
}
//特征值发生变化
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"发现特征错误:: %@", [error localizedDescription]);
return;
}
NSLog(@"特征值发生变化");
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
// 判断是否为数据结束
if ([stringFromData isEqualToString:@"END"]) {
// 显示数据
NSString* recString = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
[self addReceiveMessage:recString];
self.data.length = 0;
return;
}
// 接收数据追加到data属性中
[self.data appendData:characteristic.value];
}
//特征通知状态发生变化
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"特征通知状态变化错误: %@", error.localizedDescription);
}
// 如果没有特征传输过来则退出(如果不是我们感兴趣的特质)
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
return;
}
NSLog(@"特征通知状态发生变化");
// 特征通知已经开始
if (characteristic.isNotifying) {
NSLog(@"特征通知已经开始 %@", characteristic);
}
// 特征通知已经停止
else {
NSLog(@"特征通知已经停止 %@", characteristic);
[self.centralManager cancelPeripheralConnection:peripheral];
}
}
//与外设连接断开时回调
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"外设已经断开");
self.discoveredPeripheral = nil;
//外设已经断开情况下,重新扫描
[self scan];
}
/** 清除方法
*/
- (void)cleanup {
NSLog(@"清除订阅特征");
// 如果没有连接则退出
if (self.discoveredPeripheral.state != CBPeripheralStateConnected) {
return;
}
// 判断是否已经预定了特征
for (CBService *service in self.discoveredPeripheral.services) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
if (characteristic.isNotifying) {
//停止接收特征通知
[self.discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];
//断开与外设连接
[self.centralManager cancelPeripheralConnection:self.discoveredPeripheral];
return;
}
}
}
}
}
//添加从外设发来的消息
-(void)addReceiveMessage:(NSString*)message{
NSLog(@"收到从外设发来的消息:\n%@",message);
//下面代码无需研究,只是为了显示在屏幕上
[self receiveMessage:message senderId:kJSQDemoAvatarIdCook senderName:kJSQDemoAvatarDisplayNameCook];
}
//=================================================================
// 发送文本数据
//=================================================================
#pragma mark - 发送文本数据
- (void)didPressSendButton:(UIButton *)button
withMessageText:(NSString *)text
senderId:(NSString *)senderId
senderDisplayName:(NSString *)senderDisplayName
date:(NSDate *)date {
if (self.activityIndicatorView.isAnimating) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"没有与外设建立链接,无法发送数据" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
[alertView show];
return;
}
NSLog(@"要发送的消息为:\n%@",text);
NSData *writeData = [text dataUsingEncoding:NSUTF8StringEncoding];
if (self.characteristic.properties & CBCharacteristicPropertyWrite) {
[self.discoveredPeripheral writeValue:writeData forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
}
//下面代码无需研究,只是为了显示在屏幕上
[super didPressSendButton:button withMessageText:text senderId:senderId senderDisplayName:senderDisplayName date:date];
}
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"给外设写入数据成功");
}
//=================================================================
// 和界面相关,请忽略
//=================================================================
#pragma mark - 和界面相关,请忽略
- (NSString *)senderId {
return kJSQDemoAvatarIdWoz;
}
@end
外设管理者
中心管理者相关流程:(以下流程摘抄自一个demo)
1,入口函数,peripheralManagerDidUpdateState,处理:特征准备,服务准备的工作;设置服务的特征;将服务添加到周边管理器上
2,服务添加成功,会触发周边管理器代理的peripheralManager:didAddService:
3,在上部的回调函数中,广播广播服务,等待中心即客户端预订改服务。。。
4,一旦客户端预订成功,则调用周边管理器代理的peripheralManager:central:didSubscribeToCharacteristic:,表明管道已经打通了,接下来将发送按钮变为有效状态,由服务器决定是否发送
5,在上面的回调函数中,处理发送数据:通过调用周边管理器的updateValue方法,来实现发送。这个应该会触发中央的peripheral:didUpdateValueForCharacteristic函数
外设管理者(CBPeripheralManager)相关代码如下
//
// CBPeripheralManagerController.m
// CoreBluetooth_Demo
//
// Created by mac on 16/9/9.
// Copyright © 2016年 chenfanfang. All rights reserved.
//
#import "CBPeripheralManagerController.h"
#import
//可通过终端命令 uuidgen来生成
#define TRANSFER_SERVICE_UUID @"D63D44E5-E798-4EA5-A1C0-3F9EEEC2CDEB"
#define TRANSFER_CHARACTERISTIC_UUID @"1652CAD2-6B0D-4D34-96A0-75058E606A98"
//每次向中心设备发送数据的最大的数据量
#define SEND_DATA_MAX_AMOUNT 20
@interface CBPeripheralManagerController ()
/** 外设管理者 */
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
/** 特征 */
@property (strong, nonatomic) CBMutableCharacteristic *transferCharacteristic;
/** 需要发送的数据 */
@property (strong, nonatomic) NSData *dataToSend;
/** 需要发送的数据的字节的下标标记 */
@property (nonatomic, readwrite) NSInteger sendDataIndex;
@end
@implementation CBPeripheralManagerController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.title = @"外设模式";
//设置CBPeripheralManager
_peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
[self.activityIndicatorView startAnimating];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.peripheralManager stopAdvertising];
self.peripheralManager = nil;
}
- (void)dealloc {
NSLog(@"%@控制器销毁成功,无内存泄漏",NSStringFromClass([self class]));
}
//=================================================================
// CBPeripheralManagerDelegate
//=================================================================
#pragma mark - CBPeripheralManagerDelegate
//外设设置成功回调此方法
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请您打开蓝牙" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
[alertView show];
return;
}
NSLog(@"蓝牙处于打开状态");
// 初始化特征
self.transferCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]
properties:CBCharacteristicPropertyNotify | CBCharacteristicPropertyWrite
value:nil
permissions:CBAttributePermissionsWriteable]; //CBAttributePermissionsReadable
// 初始化服务
CBMutableService *transferService = [[CBMutableService alloc]
initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]
primary:YES];
// 添加特征到服务
transferService.characteristics = @[self.transferCharacteristic];
// 发布服务与特征(将服务添加到外设中)
[self.peripheralManager addService:transferService];
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral
didAddService:(CBService *)service
error:(NSError *)error {
if (error) {
NSLog(@"添加服务失败: %@", [error localizedDescription]);
}else{
NSLog( @"添加服务成功,准备广播..." );
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }];
}
}
//中心管理者订阅了特征,会回调该方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
[self.activityIndicatorView stopAnimating];
NSLog(@"中心已经订阅了特征");
}
//中心管理者取消订阅了特征,会回调该方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic {
[self.activityIndicatorView startAnimating];
NSLog(@"中心取消订阅特征");
}
//向中心管理者发送数据
//注意,发送大量的数据时,需要将数据分成多个小数据发送,要不然会发送失败
- (void)sendData {
/** 是否开始发送 END */
static BOOL sendingEND = NO;
//开始发送最后的 结束标识符 END
if (sendingEND == YES) {
BOOL didSend = [self.peripheralManager updateValue:[@"END" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];
if (didSend == YES) {
sendingEND = NO;
NSLog(@"发送数据完毕");
}
return;
}
//已经发送完毕
if (self.sendDataIndex >= self.dataToSend.length) {
return;
}
BOOL didSend = YES;
//正在发送数据
while (didSend) {
//本次需要发送的数据量
NSInteger amountToSend = self.dataToSend.length - self.sendDataIndex;
//若发送的数量大于规定的最大发送的数据量
if (amountToSend > SEND_DATA_MAX_AMOUNT) amountToSend = SEND_DATA_MAX_AMOUNT;
//本次需要发送的数据
NSData *smallData = [NSData dataWithBytes:self.dataToSend.bytes + self.sendDataIndex length:amountToSend];
//发送数据
didSend = [self.peripheralManager updateValue:smallData forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];
//发送数据失败 直接return
if (didSend == NO) {
return;
}
//更新需要发送的数据的字节下标
self.sendDataIndex += amountToSend;
//数据完全发送完成,记得需要发送一个结束的标识
if (self.sendDataIndex >= self.dataToSend.length) {
sendingEND = YES;
BOOL endSent = [self.peripheralManager updateValue:[@"END" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];
if (endSent) {
sendingEND = NO;
NSLog(@"发送数据完毕");
}
return;
}
}
}
//发送数据失败后(发送数据的队列已经满了)重新发送
- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral {
[self sendData];
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests {
CBATTRequest *request = requests.firstObject;
NSString *receiveMsg = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
NSLog(@"收到从中心管理者发来的消息:\n%@",receiveMsg);
//下面代码无需研究,只是为了显示在屏幕上
[self receiveMessage:receiveMsg senderId:kJSQDemoAvatarIdWoz senderName:kJSQDemoAvatarDisplayNameWoz];
}
//=================================================================
// 发送文本数据
//=================================================================
#pragma mark - 发送文本数据
- (void)didPressSendButton:(UIButton *)button
withMessageText:(NSString *)text
senderId:(NSString *)senderId
senderDisplayName:(NSString *)senderDisplayName
date:(NSDate *)date {
NSLog(@"要发送的消息为:\n%@",text);
//判断是否与中心管理者保持着链接
if (self.activityIndicatorView.isAnimating) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"没有与中心管理者建立链接,无法发送数据" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
return;
}
self.sendDataIndex = 0;
self.dataToSend = [text dataUsingEncoding:NSUTF8StringEncoding];
[self sendData];
//下面代码无需研究,只是为了显示在屏幕上
[super didPressSendButton:button withMessageText:text senderId:senderId senderDisplayName:senderDisplayName date:date];
}
//=================================================================
// 和界面相关,请忽略
//=================================================================
#pragma mark - 和界面相关,请忽略
- (NSString *)senderId {
return kJSQDemoAvatarIdCook;
}
@end
最后给大家推荐一部电子书《iOS开发高手课》,相信对大家的进步很有帮助
附上课程链接地址:
《iOS 开发高手课》:链接http://gk.link/a/102yH