提示:本文主要是講解藍牙中心管理者和外設管理者之間的通訊過程,沒有實際的意義,因為傳輸受字節限制,效率低。
老樣子,先附上效果圖和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 <CoreBluetooth/CoreBluetooth.h>
//可通過終端命令 uuidgen來生成
#define TRANSFER_SERVICE_UUID @"D63D44E5-E798-4EA5-A1C0-3F9EEEC2CDEB"
#define TRANSFER_CHARACTERISTIC_UUID @"1652CAD2-6B0D-4D34-96A0-75058E606A98"
@interface CBCentralManagerController ()<CBCentralManagerDelegate, CBPeripheralDelegate>
/** 中心管理者 */
@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 <CoreBluetooth/CoreBluetooth.h>
//可通過終端命令 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 ()<CBPeripheralManagerDelegate>
/** 外設管理者 */
@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<CBATTRequest *> *)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