蘋果的藍(lán)牙通訊使用的是CoreBluetooth.framework,實(shí)際交互是使用的CBCentralManager,搜索鏈接使用的代理是:CBCentralManagerDelegate捻艳,特性的讀取和數(shù)據(jù)交互使用的代理是CBPeripheralDelegate榜轿。本文主要目的是為了對于藍(lán)牙通訊進(jìn)行梳理勃黍。
本文是以CBCentralMannager 中心模式 :以手機(jī)(app)作為中心,連接其他外設(shè)的場景(主要寫此種該模式的應(yīng)用方法)
主要要使用的是CBPeripheral币叹、CBService矢洲、CBCharacteristic
三者的關(guān)系:
一個(gè)CBPeripheral有一個(gè)或者多個(gè)CBService,而每一個(gè)CBService有一個(gè)或者多個(gè)CBCharacteristic足陨,通過可寫的CBCharacteristic發(fā)送數(shù)據(jù),而每一個(gè)CBCharacteristic有一個(gè)或者多個(gè)Description用于描述characteristic的信息或?qū)傩?/p>
下面介紹一下使用藍(lán)牙設(shè)備連接的主要步奏
第一步引用頭文件嫂粟、代理和管理器
#import <CoreBluetooth/CoreBluetooth.h>
@interface BluetoochManager()<CBCentralManagerDelegate,CBPeripheralDelegate>{
CBCentralManager *CBManager;
}
初始化藍(lán)牙設(shè)備代理
CBManager=[[CBCentralManager alloc]initWithDelegate:self queue:nil];
//代理回調(diào)
#pragma mark CBCentralManagerDelegate
//設(shè)備列表相關(guān)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString * state = nil;
switch ([central state])
{
case CBManagerStateUnsupported:
{
state = @"硬件不支持低電量藍(lán)牙";
}
break;
case CBManagerStateUnauthorized:
{
state = @"這個(gè)應(yīng)用程序未被授權(quán)使用藍(lán)牙";
}
break;
case CBManagerStatePoweredOff:
{
state = @"藍(lán)牙處于未開啟,請開啟藍(lán)牙";
}
break;
case CBManagerStatePoweredOn:
{
state = @"work";
NSLog(@"藍(lán)牙啟動(dòng)");
//切記藍(lán)牙啟動(dòng)后才可以調(diào)用相關(guān)搜索設(shè)備的功能
}
break;
default :
{
state = @"未知名錯(cuò)誤";
}
break;
}
}
搜索藍(lán)牙設(shè)備
/**
查找藍(lán)牙設(shè)備
*/
-(void)scanForPeripherals{
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[[self manager] scanForPeripheralsWithServices:self.scanUUIDS options:options];
}
//代理回調(diào)
#pragma mark CBCentralManagerDelegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI{
NSString *str = [NSString stringWithFormat:@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral.name, RSSI, peripheral.identifier.UUIDString, advertisementData];
NSLog(@"外設(shè):\n %@",str);
}
連接設(shè)備
// 連接某個(gè)設(shè)備
- (void)connectPeripheral:(CBPeripheral*)peripheral{
if ([peripheral state]==CBPeripheralStateDisconnected) {
[CBManager connectPeripheral:peripheral options:nil];
}else{
NSString *state = @"設(shè)備正在使用中";
}
}
//代理回調(diào)
#pragma mark CBCentralManagerDelegate 連接外設(shè)
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
NSLog(@"連接外設(shè) error:\n %@",error);
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@", peripheral);
[self stopScanning];
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
NSLog(@"Did disconnect to peripheral: %@", peripheral);
}
查找設(shè)備服務(wù)
/**
發(fā)現(xiàn)某個(gè)藍(lán)牙設(shè)備的服務(wù)
@param peripheral 設(shè)備信息
*/
-(void)discoverServicesAtPeripheral:(CBPeripheral*)peripheral{
peripheral.delegate = self;
NSArray *uuids = [NSArray new];
[peripheral discoverServices:uuids];
}
//代理回調(diào)
#pragma mark CBPeripheralDelegate 設(shè)備服務(wù)
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
NSArray *services = nil;
if (error) {
NSLog(@"設(shè)備的服務(wù)查找 Error %@\n", error);
}
services = [peripheral services];
if (!services || ![services count]) {
NSString *s_message=@"該設(shè)備沒有服務(wù)";
}
}
查找服務(wù)的特性
/**
發(fā)現(xiàn)某個(gè)藍(lán)牙設(shè)備服務(wù)的特性類別
@param peripheral 設(shè)備
@param aService 服務(wù)
@param aCallBack 特性列表
*/
-(void)discoverCharacteristicsAtPeripheral:(CBPeripheral*)peripheral ofService:(CBService*)aService{
[peripheral discoverCharacteristics:[NSArray new] forService:aService];
}
//代理回調(diào)
#pragma mark CBPeripheralDelegate 服務(wù)的特性
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{
NSArray *characteristics = [service characteristics];
if (error) {
NSLog(@"設(shè)備的服務(wù)的特性查找 Error %@\n", error);
}
if (!characteristics||!characteristics.count) {
NSString *c_message=@"該設(shè)備沒有服務(wù)相關(guān)的特性";
}
}
讀取墨缘、訂閱星虹、寫入信息
/**
訂閱某個(gè)藍(lán)牙設(shè)備服務(wù)的特性變化
@param peripheral 設(shè)備
@param characteristic 特性
*/
-(void)setNotifyValueAtPeripheral:(CBPeripheral*)peripheral forCharacteristic:(CBCharacteristic*)characteristic{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
/**
讀取設(shè)備服務(wù)的特性里的數(shù)據(jù)
@param peripheral 設(shè)備
@param characteristic 特性
*/
-(void)readValueAtPeripheral:(CBPeripheral*)peripheral forCharacteristic:(CBCharacteristic *)characteristic{
[peripheral readValueForCharacteristic:characteristic];
}
/**
根據(jù)設(shè)備服務(wù)的特性寫入數(shù)據(jù)
@param data 待數(shù)據(jù)
@param peripheral 設(shè)備
@param characteristic 特性
@param aType CBCharacteristicWriteType 類型
*/
-(void)writeValue:(NSData *)data atPeripheral:(CBPeripheral*)peripheral forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)aType {
[peripheral writeValue:data forCharacteristic:characteristic type:aType];
}
//代理回調(diào)
#pragma mark CBPeripheralDelegate 讀寫回調(diào)
//數(shù)據(jù)更新
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
NSLog(@"數(shù)據(jù)更新失敗了 %@",error);
}
NSLog(@"收到的數(shù)據(jù):%@",characteristic);
}
//訂閱特性的狀態(tài)通知
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
NSLog(@"訂閱特性狀態(tài)更新出錯(cuò)了 %@",error);
}
NSLog(@"狀態(tài)的數(shù)據(jù):%@",characteristic);
}
//寫入數(shù)據(jù)的狀態(tài)消息
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
NSLog(@"特性寫入出錯(cuò)了 %@",error);
}
NSLog(@"寫入的數(shù)據(jù):%@",characteristic);
}
藍(lán)牙工具的源碼地址:Github