前一段時(shí)間做一個(gè)iPhone端通過(guò)藍(lán)牙與血壓計(jì)交互的軟件,類(lèi)似于天天血壓這款軟件的功能。在此帶來(lái)了關(guān)于藍(lán)牙的一些分享!
1、外設(shè)
外設(shè)就是一臺(tái)向外發(fā)送信息的設(shè)備,它以廣播的形勢(shì)進(jìn)行消息的發(fā)送,在手機(jī)和血壓計(jì)中,血壓計(jì)所扮演的角色就是外設(shè)。其它的一些例子,又比如智能手環(huán)、打印機(jī)、跑步用的計(jì)步器等等,這些設(shè)備都是屬于外設(shè)。在CoreBluetooth
中是通過(guò)CBPeripheralManager
來(lái)進(jìn)行管理的。
2、中心
中心是我們所說(shuō)的手機(jī)端,用來(lái)接收外設(shè)所發(fā)送的信息,比如血壓計(jì)測(cè)量時(shí)所測(cè)量的值,測(cè)量的最終結(jié)果等信息,另外也可以通過(guò)中心端向外設(shè)寫(xiě)入一些值來(lái)使外設(shè)展示一些功能,比如通過(guò)手機(jī)端發(fā)送的一些指令使得血壓計(jì)啟動(dòng)測(cè)量、關(guān)閉測(cè)量和關(guān)機(jī)功能。
二、實(shí)現(xiàn)手機(jī)端(中心)的代碼邏輯
1、建立中心角色
2、掃描外設(shè)
3、連接外設(shè)
4、掃描外設(shè)中的服務(wù)和特征
4.1 獲取外設(shè)的services
4.2 獲取外設(shè)的Characteristics,獲取Characteristics的值,獲取Characteristics的Descriptor和Descriptor的值
5、與外設(shè)做數(shù)據(jù)交互
6、 訂閱Characteristic的通知
7、斷開(kāi)連接
在此解釋一下什么是服務(wù)(services),在硬件工程師開(kāi)發(fā)一款藍(lán)牙設(shè)備時(shí),會(huì)在設(shè)備中定義好服務(wù)。在每一個(gè)服務(wù)中都有一個(gè)或多個(gè)特征,每個(gè)特征的屬性有只讀、只寫(xiě)、通知等等。
創(chuàng)建中心管理者
//導(dǎo)入框架
#import <CoreBluetooth/CoreBluetooth.h>
@interface XYViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate >
//用于保存被發(fā)現(xiàn)設(shè)備
@property (strong, nonatomic) NSMutableArray* myPeripherals;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.myCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];
}
設(shè)置主設(shè)備的委托,CBCentralManagerDelegate
必須實(shí)現(xiàn)的代理方法:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;//主設(shè)備狀態(tài)改變的委托,在初始化CBCentralManager的適合會(huì)打開(kāi)設(shè)備,只有當(dāng)設(shè)備正確打開(kāi)后才能使用
其他選擇實(shí)現(xiàn)的委托中比較重要的:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI; //找到外設(shè)的委托
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;//連接外設(shè)成功的委托
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//外設(shè)連接失敗的委托
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//斷開(kāi)外設(shè)的委托
掃描到設(shè)備進(jìn)行連接
//掃描到設(shè)備會(huì)進(jìn)入方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
//接下連接測(cè)試設(shè)備,如果沒(méi)有設(shè)備,可以下載一個(gè)叫l(wèi)ightbule的app去模擬一個(gè)設(shè)備
//找到的設(shè)備必須持有它,否則CBCentralManager中也不會(huì)保存peripheral,那么CBPeripheralDelegate中的方法也不會(huì)被調(diào)用??!
[myPeripherals addObject:peripheral];
//連接設(shè)備
[manager connectPeripheral:peripheral options:nil];
}
一個(gè)主設(shè)備最多能連7個(gè)外設(shè),每個(gè)外設(shè)最多只能給一個(gè)主設(shè)備連接,連接成功、失敗、斷開(kāi)都會(huì)進(jìn)入到相應(yīng)的代理
//連接到Peripherals-成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@">>>連接到名稱(chēng)為(%@)的設(shè)備-成功",peripheral.name);
[peripheral setDelegate:self];
//掃描外設(shè)Services,成功后會(huì)進(jìn)入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
[peripheral discoverServices:nil];
}
//連接到Peripherals-失敗
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@">>>連接到名稱(chēng)為(%@)的設(shè)備-失敗,原因:%@",[peripheral name],[error localizedDescription]);
}
//Peripherals斷開(kāi)連接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@">>>外設(shè)連接斷開(kāi)連接 %@: %@\n", [peripheral name], [error localizedDescription]);
}
掃描到Services
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
// NSLog(@">>>掃描到服務(wù):%@",peripheral.services);
if (error){
NSLog(@">>>Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
return;
}
for (CBService *service in peripheral.services) {
NSLog(@"%@",service.UUID);
//掃描每個(gè)service的Characteristics,掃描到后會(huì)進(jìn)入方法: -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
[peripheral discoverCharacteristics:nil forService:service];
}
}
獲取外設(shè)的Characteristics,獲取Characteristics的值,獲取Characteristics的Descriptor和Descriptor的值
//掃描到Characteristics
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error)
{
NSLog(@"error Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
return;
}
for (CBCharacteristic *characteristic in service.characteristics)
{
NSLog(@"service:%@ 的 Characteristic: %@",service.UUID,characteristic.UUID);
}
//獲取Characteristic的值,讀到數(shù)據(jù)會(huì)進(jìn)入方法:-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics){
{
[peripheral readValueForCharacteristic:characteristic];
}
}
//搜索Characteristic的Descriptors,讀到數(shù)據(jù)會(huì)進(jìn)入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics){
[peripheral discoverDescriptorsForCharacteristic:characteristic];
}
}
//獲取的charateristic的值
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
//打印出characteristic的UUID和值
//!注意,value的類(lèi)型是NSData,具體開(kāi)發(fā)時(shí),會(huì)根據(jù)外設(shè)協(xié)議制定的方式去解析數(shù)據(jù)
NSLog(@"characteristic uuid:%@ value:%@",characteristic.UUID,characteristic.value);
}
//搜索到Characteristic的Descriptors
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
//打印出Characteristic和他的Descriptors
NSLog(@"characteristic uuid:%@",characteristic.UUID);
for (CBDescriptor *d in characteristic.descriptors) {
NSLog(@"Descriptor uuid:%@",d.UUID);
}
}
//獲取到Descriptors的值
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{
//打印出DescriptorsUUID 和value
//這個(gè)descriptor都是對(duì)于characteristic的描述,一般都是字符串,所以這里我們轉(zhuǎn)換成字符串去解析
NSLog(@"characteristic uuid:%@ value:%@",[NSString stringWithFormat:@"%@",descriptor.UUID],descriptor.value);
}
把數(shù)據(jù)寫(xiě)到Characteristic中,??在寫(xiě)入的時(shí)候,必須要先寫(xiě)入與外設(shè)建立連接的值,只有建立了連接之后寫(xiě)入其他的值才能有反應(yīng)(ps:我被坑過(guò))
//寫(xiě)數(shù)據(jù)
-(void)writeCharacteristic:(CBPeripheral *)peripheral
characteristic:(CBCharacteristic *)characteristic
value:(NSData *)value{
//打印出 characteristic 的權(quán)限,可以看到有很多種,這是一個(gè)NS_OPTIONS,就是可以同時(shí)用于好幾個(gè)值,
//常見(jiàn)的有read,write,notify,indicate,知知道這幾個(gè)基本就夠用了,前連個(gè)是讀寫(xiě)權(quán)限,后兩個(gè)都是通知,兩種不同的通知方式。
/*
CBCharacteristicPropertyBroadcast
CBCharacteristicPropertyRead
CBCharacteristicPropertyWriteWithoutResponse
CBCharacteristicPropertyWrite
CBCharacteristicPropertyNotify
CBCharacteristicPropertyIndicate
CBCharacteristicPropertyAuthenticatedSignedWrites
CBCharacteristicPropertyExtendedProperties
CBCharacteristicPropertyNotifyEncryptionRequired
CBCharacteristicPropertyIndicateEncryptionRequired
*/
NSLog(@"%lu", (unsigned long)characteristic.properties);
//只有 characteristic.properties 有write的權(quán)限才可以寫(xiě)
if(characteristic.properties & CBCharacteristicPropertyWrite){
/*
最好一個(gè)type參數(shù)可以為CBCharacteristicWriteWithResponse或type:CBCharacteristicWriteWithResponse,區(qū)別是是否會(huì)有反饋
*/
[peripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}else{
NSLog(@"該字段不可寫(xiě)!");
}
}
訂閱Characteristic的通知
//設(shè)置通知
-(void)notifyCharacteristic:(CBPeripheral *)peripheral
characteristic:(CBCharacteristic *)characteristic{
//設(shè)置通知,數(shù)據(jù)通知會(huì)進(jìn)入:didUpdateValueForCharacteristic方法
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
//取消通知
-(void)cancelNotifyCharacteristic:(CBPeripheral *)peripheral
characteristic:(CBCharacteristic *)characteristic{
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
}
斷開(kāi)連接
//停止掃描并斷開(kāi)連接
-(void)disconnectPeripheral:(CBCentralManager *)centralManager
peripheral:(CBPeripheral *)peripheral{
//停止掃描
[centralManager stopScan];
//斷開(kāi)連接
[centralManager cancelPeripheralConnection:peripheral];
}
如何計(jì)算寫(xiě)入的值?
一下為模擬數(shù)據(jù),僅供參考,主要講的是計(jì)算思路
藍(lán)牙文檔如圖所示
按照文檔流程所示可以算出每一項(xiàng)功能所對(duì)應(yīng)的值以及手機(jī)端所接收到值對(duì)應(yīng)的功能
在這些值中其中校驗(yàn)碼的計(jì)算是需要自己計(jì)算一下的,計(jì)算的方法如下圖所示,現(xiàn)將16進(jìn)制轉(zhuǎn)化為2進(jìn)制,然后進(jìn)行異或運(yùn)算獲得