在寫這個博客之前,空余時間抽看了近一個月的文檔和Demo,系統給的解釋很詳細,接口也比較實用,唯獨有一點,對于設備的唯一標示,網上眾說
紛紜,在這里我目前也還沒有自己的見解,只是在不斷的測試各種情況,親測同一設備的UUID對于每臺iPhone設備都不一樣,只能盡量保證設備的唯一性,特別是自動重連的過程,讓用戶沒有感知。我之前也找了很久,發現CBCentralManager和CBPeripheral里邊都找不到和Mac地址有關的東西,后來發現一般是外設在Device Information服務中的某個特征返回的。經過與硬件工程師的協商,決定APP端將從這個服務中獲取到藍牙設備以及我的iPhone手機的藍牙Mac地址,為自動連接的唯一性做準備。
這里經過和硬件工程師的測試,發現設備端在獲取手機藍牙MAC地址的時候,當用戶手機重啟之后,這個地址也是會隨機變化的,也就是說,作為開發者,只有設備的MAC地址能夠保持唯一性不變化。
ble.png
有疑問的朋友可以先去這里瞅一瞅
下面是兩臺iPhone6連接同一臺藍牙設備的結果:
**成功連接**** peripheral:with UUID:<__NSConcreteUUID0x17003d980>50084F69-BA5A-34AC-8A6E-6F0CEADB21CD************成功連接**** peripheral:with UUID:<__NSConcreteUUID0x174036c00>55B7D759-0F1E-6271-EA14-BC5A9C9EEEEC**
進入正題
iOS的藍牙開發很簡單,只要包含一個庫,創建CBCentralManager實例,實現代理方法,然后就可以直接和設備進行通信。
發現附近的特定藍牙設備
#import
首先可以定義一些即將使用到的UUID的宏
#definekPeripheralName? ? @"360qws Electric Bike Service"http://外圍設備名稱#definekServiceUUID? ? ? ? @"7CACEB8B-DFC4-4A40-A942-AAD653D174DC"http://服務的UUID#definekCharacteristicUUID @"282A67B2-8DAB-4577-A42F-C4871A3EEC4F"http://特征的UUID
如果不是把手機作為中心設備的話,這些沒有必要設置。
這里我也沒有用到,僅僅是提了一下,具體操作后續添加。
對于生成UUID,大家可以谷歌一下,直接通過mac終端生成32位UUID。
1.聲明屬性
@property(weak,nonatomic)IBOutletUITableView*bluetoothTable;@property(weak,nonatomic)IBOutletUITextView*resultTextView;@propertyBOOLcbReady;@property(nonatomic)floatbatteryValue;@property(nonatomic,strong) CBCentralManager *manager;@property(nonatomic,strong) CBPeripheral *peripheral;@property(strong,nonatomic) CBCharacteristic *writeCharacteristic;@property(strong,nonatomic)NSMutableArray*nDevices;@property(strong,nonatomic)NSMutableArray*nServices;@property(strong,nonatomic)NSMutableArray*nCharacteristics;
2.遵守協議(這里我用到了table)
@interfaceViewController ()
3.初始化數據
- (void)viewDidLoad {? ? [superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.self.manager = [[CBCentralManager alloc] initWithDelegate:selfqueue:nil];? ? _cbReady =false;? ? _nDevices = [[NSMutableArrayalloc]init];? ? _nServices = [[NSMutableArrayalloc]init];? ? _nCharacteristics = [[NSMutableArrayalloc]init];? ? _bluetoothTable.delegate =self;? ? _bluetoothTable.dataSource =self;? ? count =0;}
4.實現藍牙的協議方法
(1)檢測藍牙狀態
//開始查看服務,藍牙開啟-(void)centralManagerDidUpdateState:(CBCentralManager *)central{switch(central.state) {caseCBCentralManagerStatePoweredOn:{? ? ? ? ? ? [selfupdateLog:@"藍牙已打開,請掃描外設"];? ? ? ? ? ? [_activity startAnimating];? ? ? ? ? ? [_managerscanForPeripheralsWithServices:@[[CBUUIDUUIDWithString:@"FF15"]]options:@{CBCentralManagerScanOptionAllowDuplicatesKey :@YES}];? ? ? ? }break;caseCBCentralManagerStatePoweredOff:[selfupdateLog:@"藍牙沒有打開,請先打開藍牙"];break;default:break;? ? }}
注:[_manager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FF15"]]? options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];中間的@[[CBUUID UUIDWithString:@"FF15"]]是為了過濾掉其他設備,可以搜索特定標示的設備。
(2)檢測到外設后,停止掃描,連接設備
//查到外設后,停止掃描,連接設備-(void)centralManager:(CBCentralManager *)centraldidDiscoverPeripheral:(CBPeripheral *)peripheraladvertisementData:(NSDictionary *)advertisementDataRSSI:(NSNumber *)RSSI{? ? [selfupdateLog:[NSStringstringWithFormat:@"已發現 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier, advertisementData]];? ? _peripheral = peripheral;? ? [_managerconnectPeripheral:_peripheraloptions:nil];? ? [self.manager stopScan];? ? [_activity stopAnimating];? ? BOOL replace = NO;// Match if we have this device from beforefor(inti=0; i < _nDevices.count; i++) {? ? ? ? CBPeripheral *p = [_nDevicesobjectAtIndex:i];if([pisEqual:peripheral]) {? ? ? ? ? ? [_nDevicesreplaceObjectAtIndex:iwithObject:peripheral];? ? ? ? ? ? replace = YES;? ? ? ? }? ? }if(!replace) {? ? ? ? [_nDevicesaddObject:peripheral];? ? ? ? [_bluetoothTable reloadData];? ? }}
(3)連接外設后的處理
//連接外設成功,開始發現服務- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {NSLog(@"%@", [NSStringstringWithFormat:@"成功連接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier]);? ? [selfupdateLog:[NSStringstringWithFormat:@"成功連接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier]];? ? [self.peripheral setDelegate:self];? ? [self.peripheral discoverServices:nil];? ? [selfupdateLog:@"掃描服務"];}//連接外設失敗-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError*)error{NSLog(@"%@",error);}-(void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError*)error{NSLog(@"%s,%@",__PRETTY_FUNCTION__,peripheral);intrssi = abs([peripheral.RSSI intValue]);CGFloatci = (rssi -49) / (10*4.);NSString*length = [NSStringstringWithFormat:@"發現BLT4.0熱點:%@,距離:%.1fm",_peripheral,pow(10,ci)];? ? [selfupdateLog:[NSStringstringWithFormat:@"距離:%@", length]];}
(4)發現服務和搜索到的Characteristice
//已發現服務-(void)peripheral:(CBPeripheral *)peripheraldidDiscoverServices:(NSError *)error{? ? [selfupdateLog:@"發現服務."];inti=0;for(CBService *sinperipheral.services) {? ? ? ? [self.nServicesaddObject:s];? ? }for(CBService *sinperipheral.services) {? ? ? ? [selfupdateLog:[NSStringstringWithFormat:@"%d :服務 UUID: %@(%@)",i,s.UUID.data,s.UUID]];? ? ? ? i++;? ? ? ? [peripheraldiscoverCharacteristics:nilforService:s];if([s.UUIDisEqual:[CBUUIDUUIDWithString:@"FF15"]]) {? ? ? ? ? ? BOOL replace = NO;// Match if we have this device from beforefor(inti=0; i < _nDevices.count; i++) {? ? ? ? ? ? ? ? CBPeripheral *p = [_nDevicesobjectAtIndex:i];if([pisEqual:peripheral]) {? ? ? ? ? ? ? ? ? ? [_nDevicesreplaceObjectAtIndex:iwithObject:peripheral];? ? ? ? ? ? ? ? ? ? replace = YES;? ? ? ? ? ? ? ? }? ? ? ? ? ? }if(!replace) {? ? ? ? ? ? ? ? [_nDevicesaddObject:peripheral];? ? ? ? ? ? ? ? [_bluetoothTable reloadData];? ? ? ? ? ? }? ? ? ? }? ? }}//已搜索到Characteristics-(void)peripheral:(CBPeripheral *)peripheraldidDiscoverCharacteristicsForService:(CBService *)serviceerror:(NSError *)error{? ? [selfupdateLog:[NSStringstringWithFormat:@"發現特征的服務:%@ (%@)",service.UUID.data ,service.UUID]];for(CBCharacteristic *cinservice.characteristics) {? ? ? ? [selfupdateLog:[NSStringstringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF01"]]) {? ? ? ? ? ? _writeCharacteristic = c;? ? ? ? }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF02"]]) {? ? ? ? ? ? [_peripheralreadValueForCharacteristic:c];? ? ? ? ? ? [_peripheralsetNotifyValue:YESforCharacteristic:c];? ? ? ? }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF04"]]) {? ? ? ? ? ? [_peripheralreadValueForCharacteristic:c];? ? ? ? }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF05"]]) {? ? ? ? ? ? [_peripheralreadValueForCharacteristic:c];? ? ? ? ? ? [_peripheralsetNotifyValue:YESforCharacteristic:c];? ? ? ? }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FFA1"]]) {? ? ? ? ? ? [_peripheral readRSSI];? ? ? ? }? ? ? ? [_nCharacteristicsaddObject:c];? ? }}- (void)centralManager:(CBCentralManager *)centraldidDisconnectPeripheral:(CBPeripheral *)peripheralerror:(NSError *)error {? ? [selfupdateLog:[NSStringstringWithFormat:@"已斷開與設備:[%@]的連接", peripheral.name]];}
(5)獲取外設發來的數據
//獲取外設發來的數據,不論是read和notify,獲取數據都是從這個方法中讀取。- (void)peripheral:(CBPeripheral *)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError *)error{if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF02"]]) {? ? ? ? NSData * data = characteristic.value;? ? ? ? Byte * resultByte = (Byte *)[data bytes];for(inti=0;i<[data length];i++)? ? ? ? ? ? printf("testByteFF02[%d] = %d\n",i,resultByte[i]);if(resultByte[1] ==0) {switch(resultByte[0]) {case3:// 加解鎖{if(resultByte[2] ==0) {? ? ? ? ? ? ? ? ? ? ? ? [selfupdateLog:@"撤防成功!!!"];? ? ? ? ? ? ? ? ? ? }elseif(resultByte[2] ==1) {? ? ? ? ? ? ? ? ? ? ? ? [selfupdateLog:@"設防成功!!!"];? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }break;case4:// 開坐桶{if(resultByte[2] ==0) {? ? ? ? ? ? ? ? ? ? ? ? [selfupdateLog:@"關坐桶成功!!!"];? ? ? ? ? ? ? ? ? ? }elseif(resultByte[2] ==1) {? ? ? ? ? ? ? ? ? ? ? ? [selfupdateLog:@"開坐桶成功!!!"];? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }break;case5:// 鎖定電機{if(resultByte[2] ==0) {? ? ? ? ? ? ? ? ? ? ? ? [selfupdateLog:@"解鎖電機控制器成功!!!"];? ? ? ? ? ? ? ? ? ? }elseif(resultByte[2] ==1) {? ? ? ? ? ? ? ? ? ? ? ? [selfupdateLog:@"鎖定電機控制器成功!!!"];? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }break;default:break;? ? ? ? ? ? }? ? ? ? }elseif(resultByte[1] ==1) {? ? ? ? ? ? [selfupdateLog:@"未知錯誤"];? ? ? ? }elseif(resultByte[1] ==2) {? ? ? ? ? ? [selfupdateLog:@"鑒權失敗"];? ? ? ? }? ? }if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF04"]]) {? ? ? ? NSData * data = characteristic.value;? ? ? ? Byte * resultByte = (Byte *)[data bytes];for(inti=0;i<[data length];i++)? ? ? ? ? ? printf("testByteFF04[%d] = %d\n",i,resultByte[i]);if(resultByte[0] ==0) {// 未綁定 -》寫鑒權碼[selfupdateLog:@"當前車輛未綁定,請鑒權"];? ? ? ? ? ? [self authentication];// 鑒權[selfwritePassword:nilnewPw:nil];? ? ? ? }elseif(resultByte[0] ==1) {// 已綁定 -》鑒權[selfupdateLog:@"當前車輛已經綁定,請鑒權"];? ? ? ? ? ? [selfwritePassword:nilnewPw:nil];? ? ? ? }elseif(resultByte[0] ==2) {// 允許綁定[selfupdateLog:@"當前車輛允許綁定"];? ? ? ? }? ? }if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF05"]]) {? ? ? ? NSData * data = characteristic.value;? ? ? ? Byte * resultByte = (Byte *)[data bytes];for(inti=0;i<[data length];i++)? ? ? ? ? ? printf("testByteFF05[%d] = %d\n",i,resultByte[i]);if(resultByte[0] ==0) {// 設備加解鎖狀態 0 撤防? ? 1 設防[selfupdateLog:@"當前車輛撤防狀態"];? ? ? ? }elseif(resultByte[0] ==1) {// 設備加解鎖狀態 0 撤防? ? 1 設防[selfupdateLog:@"當前車輛設防狀態"];? ? ? ? }? ? }}//中心讀取外設實時數據- (void)peripheral:(CBPeripheral *)peripheraldidUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError *)error {if(error) {? ? ? ? NSLog(@"Error changing notification state: %@", error.localizedDescription);? ? }// Notification has startedif(characteristic.isNotifying) {? ? ? ? [peripheralreadValueForCharacteristic:characteristic];? ? }else{// Notification has stopped// so disconnect from the peripheralNSLog(@"Notification stopped on %@.? Disconnecting", characteristic);? ? ? ? [selfupdateLog:[NSStringstringWithFormat:@"Notification stopped on %@.? Disconnecting", characteristic]];? ? ? ? [self.managercancelPeripheralConnection:self.peripheral];? ? }}//用于檢測中心向外設寫數據是否成功-(void)peripheral:(CBPeripheral *)peripheraldidWriteValueForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError *)error{if(error) {? ? ? ? NSLog(@"=======%@",error.userInfo);? ? ? ? [selfupdateLog:[error.userInfo JSONString]];? ? }else{? ? ? ? NSLog(@"發送數據成功");? ? ? ? [selfupdateLog:@"發送數據成功"];? ? }/* When a write occurs, need to set off a re-read of the local CBCharacteristic to update its value */[peripheralreadValueForCharacteristic:characteristic];}
(6)其他輔助性的
#pragma mark - 藍牙的相關操作- (IBAction)bluetoothAction:(UIButton*)sender {switch(sender.tag) {case201:? ? ? ? {// 搜索設備[selfupdateLog:@"正在掃描外設..."];? ? ? ? ? ? [_activity startAnimating];? ? ? ? ? ? [_manager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FF15"]]? options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];doubledelayInSeconds =30.0;? ? ? ? ? ? dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds *NSEC_PER_SEC));? ? ? ? ? ? dispatch_after(popTime, dispatch_get_main_queue(), ^(void){? ? ? ? ? ? ? ? [self.manager stopScan];? ? ? ? ? ? ? ? [_activity stopAnimating];? ? ? ? ? ? ? ? [selfupdateLog:@"掃描超時,停止掃描"];? ? ? ? ? ? });? ? ? ? }break;case202:? ? ? ? {// 連接if(_peripheral && _cbReady) {? ? ? ? ? ? ? ? [_manager connectPeripheral:_peripheral options:nil];? ? ? ? ? ? ? ? _cbReady =NO;? ? ? ? ? ? }? ? ? ? }break;case203:? ? ? ? {// 斷開if(_peripheral && !_cbReady) {? ? ? ? ? ? ? ? [_manager cancelPeripheralConnection:_peripheral];? ? ? ? ? ? ? ? _cbReady =YES;? ? ? ? ? ? }? ? ? ? }break;case204:? ? ? ? {// 暫停搜索[self.manager stopScan];? ? ? ? }break;case211:? ? ? ? {// 車輛上鎖[selflock];? ? ? ? }break;case212:? ? ? ? {// 車輛解鎖[selfunLock];? ? ? ? }break;case213:? ? ? ? {// 開啟坐桶[selfopen];? ? ? ? }break;case214:? ? ? ? {// 立即尋車[selffind];? ? ? ? }break;default:break;? ? }}-(NSOperationQueue*)queue {if(!_queue) {// 請求隊列self.queue = [[NSOperationQueuealloc]init];? ? ? ? [self.queue setMaxConcurrentOperationCount:1];? ? }return_queue;}#pragma mark - 命令#pragma mark - 鑒權-(void)authentication {? ? Byte byte[] = {1,1,2,3,4,5,6,7,8};if(_peripheral.state == CBPeripheralStateConnected) {? ? ? ? [_peripheral writeValue:[NSDatadataWithBytes:byte length:9] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];? ? }}#pragma mark - 寫密碼-(void)writePassword:(NSString*)initialPw newPw:(NSString*)newPw {? ? Byte byte[] = {2,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8};if(_peripheral.state == CBPeripheralStateConnected) {? ? ? ? [_peripheral writeValue:[NSDatadataWithBytes:byte length:17] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];? ? }}#pragma mark - 車輛上鎖-(void)lock {? ? Byte byte[] = {3,1};if(_peripheral.state == CBPeripheralStateConnected) {? ? ? ? [_peripheral writeValue:[NSDatadataWithBytes:byte length:2] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];? ? }}#pragma mark - 車輛解鎖-(void)unLock {? ? Byte byte[] = {3,0};if(_peripheral.state == CBPeripheralStateConnected) {? ? ? ? [_peripheral writeValue:[NSDatadataWithBytes:byte length:2] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];? ? }}#pragma mark - 開啟坐桶-(void)open {? ? Byte byte[] = {4,1};if(_peripheral.state == CBPeripheralStateConnected) {? ? ? ? [_peripheral writeValue:[NSDatadataWithBytes:byte length:2] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];? ? }}#pragma mark - 立即尋車-(void)find {? ? Byte byte[] = {7};if(_peripheral.state == CBPeripheralStateConnected) {? ? ? ? [_peripheral writeValue:[NSDatadataWithBytes:byte length:1] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];? ? }}-(NSData*)hexString:(NSString*)hexString {intj=0;? ? Byte bytes[20];///3ds key的Byte 數組, 128位for(inti=0; i<[hexString length]; i++)? ? {intint_ch;/// 兩位16進制數轉化后的10進制數unicharhex_char1 = [hexString characterAtIndex:i];////兩位16進制數中的第一位(高位*16)intint_ch1;if(hex_char1 >='0'&& hex_char1 <='9')? ? ? ? ? ? int_ch1 = (hex_char1-48)*16;//// 0 的Ascll - 48elseif(hex_char1 >='A'&& hex_char1 <='F')? ? ? ? ? ? int_ch1 = (hex_char1-55)*16;//// A 的Ascll - 65elseint_ch1 = (hex_char1-87)*16;//// a 的Ascll - 97i++;unicharhex_char2 = [hexString characterAtIndex:i];///兩位16進制數中的第二位(低位)intint_ch2;if(hex_char2 >='0'&& hex_char2 <='9')? ? ? ? ? ? int_ch2 = (hex_char2-48);//// 0 的Ascll - 48elseif(hex_char1 >='A'&& hex_char1 <='F')? ? ? ? ? ? int_ch2 = hex_char2-55;//// A 的Ascll - 65elseint_ch2 = hex_char2-87;//// a 的Ascll - 97int_ch = int_ch1+int_ch2;NSLog(@"int_ch=%d",int_ch);? ? ? ? bytes[j] = int_ch;///將轉化后的數放入Byte數組里j++;? ? }NSData*newData = [[NSDataalloc] initWithBytes:bytes length:20];returnnewData;}
在和硬件之間的數據發送和接受,用的都是byte數組。最后,添加一個存儲已連接過得設備
- (void) addSavedDevice:(CFUUIDRef)uuid {NSArray*storedDevices = [[NSUserDefaultsstandardUserDefaults] arrayForKey:@"StoredDevices"];NSMutableArray*newDevices =nil;CFStringRefuuidString =NULL;if(![storedDevices isKindOfClass:[NSArrayclass]]) {NSLog(@"Can't find/create an array to store the uuid");return;? ? }? ? newDevices = [NSMutableArrayarrayWithArray:storedDevices];? ? uuidString =CFUUIDCreateString(NULL, uuid);if(uuidString) {? ? ? ? [newDevices addObject:(__bridgeNSString*)uuidString];CFRelease(uuidString);? ? }/* Store */[[NSUserDefaultsstandardUserDefaults] setObject:newDevices forKey:@"StoredDevices"];? ? [[NSUserDefaultsstandardUserDefaults] synchronize];}- (void) loadSavedDevices {NSArray*storedDevices = [[NSUserDefaultsstandardUserDefaults] arrayForKey:@"StoredDevices"];if(![storedDevices isKindOfClass:[NSArrayclass]]) {NSLog(@"No stored array to load");return;? ? }for(iddeviceUUIDStringinstoredDevices) {if(![deviceUUIDString isKindOfClass:[NSStringclass]])continue;CFUUIDRefuuid =CFUUIDCreateFromString(NULL, (CFStringRef)deviceUUIDString);if(!uuid)continue;? ? ? ? [CBCentralManager retrievePeripherals:[NSArrayarrayWithObject:(__bridgeid)uuid]];CFRelease(uuid);? ? }}//And the delegate function for the Retrieve Peripheral- (void) centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray*)peripherals {? ? CBPeripheral *peripheral;/* Add to list. */for(peripheralinperipherals) {? ? ? ? [central connectPeripheral:peripheral options:nil];? ? }} - (void) centralManager:(CBCentralManager *)central didRetrievePeripheral:(CBPeripheral *)peripheral {? ? [central connectPeripheral:peripheral options:nil];}
后記
最主要是用UUID來確定你要干的事情,特征和服務的UUID都是外設定義好的。我們只需要讀取,確定你要讀取什么的時候,就去判斷UUID是否相符。 一般來說我們使用的iPhone都是做centralManager的,藍牙模塊是peripheral的,所以我們是want datas,需要接受數據。
1.判斷狀態為powerOn,然后執行掃描
2.停止掃描,連接外設
3.連接成功,尋找服務
4.在服務里尋找特征
5.為特征添加通知
5.通知添加成功,那么就可以實時的讀取value[也就是說只要外設發送數據[一般外設的頻率為10Hz],代理就會調用此方法]。
6.處理接收到的value,[hex值,得轉換] 之后就自由發揮了,在這期間都是通過代理來實現的,也就是說你只需要處理你想要做的事情,代理會幫你調用方法。[別忘了添加代理]
2015-07-28 更
關于write我這里還有些注意的地方要強調!!!!
并不是每一個Characteristic都可以通過回調函數來查看它寫入狀態的。就比如針對 immediateAlertService(1802) 的 alertLevelCharacteristic(2A06),就是一個不能有response的Characteristic。剛開始我就一直用CBCharacteristicWriteType.WithResponse來進行寫入始終不成功,郁悶壞了,最后看到每個Characteristic還有個屬性值是指示這個的,我將每個Characteristic打印出來有如下信息:
immediateAlertService Discover characteristiclinkLossAlertService Discover characteristic
這個的properties是什么剛開始不知道,覺得他沒意義,后面才注意到properties是Characteristic的一個參數,具體解釋如下:
DeclarationSWIFTstruct CBCharacteristicProperties :RawOptionSetType{? ? init(_value: UInt)? ? var value: UInt? ? static var Broadcast: CBCharacteristicProperties { get }staticvarRead:CBCharacteristicProperties{ get }staticvarWriteWithoutResponse:CBCharacteristicProperties{ get }staticvarWrite:CBCharacteristicProperties{ get }staticvarNotify:CBCharacteristicProperties{ get }staticvarIndicate:CBCharacteristicProperties{ get }staticvarAuthenticatedSignedWrites:CBCharacteristicProperties{ get }staticvarExtendedProperties:CBCharacteristicProperties{ get }staticvarNotifyEncryptionRequired:CBCharacteristicProperties{ get }staticvarIndicateEncryptionRequired:CBCharacteristicProperties{ get }}OBJECTIVE-Ctypedefenum{? CBCharacteristicPropertyBroadcast =0x01,? CBCharacteristicPropertyRead =0x02,? CBCharacteristicPropertyWriteWithoutResponse =0x04,? CBCharacteristicPropertyWrite =0x08,? CBCharacteristicPropertyNotify =0x10,? CBCharacteristicPropertyIndicate =0x20,? CBCharacteristicPropertyAuthenticatedSignedWrites =0x40,? CBCharacteristicPropertyExtendedProperties =0x80,? CBCharacteristicPropertyNotifyEncryptionRequired =0x100,? CBCharacteristicPropertyIndicateEncryptionRequired =0x200,}CBCharacteristicProperties;
可以看到0x04對應的是CBCharacteristicPropertyWriteWithoutResponse
0x0A對應的是CBCharacteristicPropertyNotify
所以 immediateAlertService(1802) 的 alertLevelCharacteristic(2A06)是不能用CBCharacteristicWriteType.WithRespons進行寫入,只能用CBCharacteristicWriteType.WithOutRespons。這樣在以后的開發中可以對每個Characteristic的這個參數進行檢查再進行設置。
最后講一下關于藍牙綁定的過程,在iOS中,沒有講當綁定的過程,直接就是掃描、連接、交互。從而很多人會認為,連接就是綁定了,其實不然。在iOS開發中,連接并沒有完成綁定,在網上找到了個很好的解釋:
you cannot initiate pairing from the iOS central side. Instead, you have to read/write a characteristic value,
and then let your peripheral respond with an "Insufficient Authentication" error.
iOS will then initiate pairing, will store the keys for later use (bonding) and encrypts the link. As far as I know,
it also caches discovery information, so that future connections can be set up faster.
就是當發生讀寫交互時,系統在會和外設進行綁定操作!!!
2016-02-20 更
如題,手機作為主設備,在使用CoreBluetooth時候,想獲取藍牙的數據廣播包。在使用
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)aPeripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
方法時候獲取的advertisementData打印出來只有
******kCBAdvDataIsConnectable****kCBAdvDataLocalName****kCBAdvDataManufacturerData**
三個屬性對應的值,但這并非廣播包的數據。例如安卓可以通過scandata來獲取到廣播包的值,那么iOS這邊我應該怎么做呢?
好像蘋果這邊禁止讀取這種廣播內容的的,真要的話你可以讓硬件那邊把數據做到kCBAdvDataManufacturerData這個字段里面。
Demo地址:一個藍牙4.0的智能硬件Demo