iOS藍(lán)牙開(kāi)發(fā)數(shù)據(jù)實(shí)時(shí)傳輸
隨著iOS項(xiàng)目開(kāi)發(fā) 很多app需要通過(guò)藍(lán)牙與設(shè)備連接
藍(lán)牙開(kāi)發(fā)注意:
先定義中心設(shè)備和外圍設(shè)備以及遵守藍(lán)牙協(xié)議
@interface ViewController()<CBCentralManagerDelegate,CBPeripheralDelegate> @property (strong, nonatomic) CBCentralManager *manager; @property (nonatomic, strong) CBPeripheral *peripheral; @property (nonatomic, weak)NSTimer * connentTimer; @end
再實(shí)現(xiàn)delegate方法
1.判斷藍(lán)牙狀態(tài),如成功則掃描指定UUID設(shè)備(如不指定UUID,則無(wú)法后臺(tái)持續(xù)連接)
2.當(dāng)發(fā)現(xiàn)指定設(shè)備后,連接該設(shè)備
3.當(dāng)連接指定外圍設(shè)備成功,編寫(xiě)定時(shí)器,每秒讀取1次RSSI
4.當(dāng)監(jiān)聽(tīng)到失去和外圍設(shè)備連接,重新建立連接
5.當(dāng)讀取到RSSI值,打印出它的值
//藍(lán)牙狀態(tài) - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSString * state = nil; switch ([central state]) { case CBCentralManagerStateUnsupported: state = @"The platform/hardware doesn't support Bluetooth Low Energy."; break; //應(yīng)用程序沒(méi)有被授權(quán)使用藍(lán)牙 case CBCentralManagerStateUnauthorized: state = @"The app is not authorized to use Bluetooth Low Energy."; break; //尚未打開(kāi)藍(lán)牙 case CBCentralManagerStatePoweredOff: state = @"Bluetooth is currently powered off."; break; //連接成功 case CBCentralManagerStatePoweredOn: [self.manager scanForPeripheralsWithServices:nil options:nil]; state = @"work"; break; case CBCentralManagerStateUnknown: default: ; } NSLog(@"Central manager state: %@", state); } //查找設(shè)備 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { //每個(gè)藍(lán)牙設(shè)備有自己唯一的標(biāo)識(shí)符,根據(jù)標(biāo)識(shí)符確認(rèn)自己要連接的設(shè)備 if ([peripheral.identifier isEqual:self.peripheral.identifier]) { self.peripheral = peripheral; //數(shù)據(jù)連接定時(shí)器 self.connentTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(connentPeripheral) userInfo:@"timer" repeats:YES]; [self.connentTimer fire]; } } - (void)connentPeripheral { //連接外設(shè) self.manager.delegate = self; [self.manager connectPeripheral:_peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]]; } //連接成功后調(diào)用 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Did connect to peripheral: %@,%@", peripheral,peripheral.name); [peripheral setDelegate:self]; //查找服務(wù) [peripheral discoverServices:nil]; [self.connentTimer invalidate]; //監(jiān)測(cè)設(shè)備是否斷開(kāi)了 // [self createWorkDataSourceWithTimeInterval:1]; } //當(dāng)監(jiān)聽(tīng)到失去和外圍設(shè)備連接,重新建立連接 //這個(gè)方法是必須實(shí)現(xiàn)的,因?yàn)樗{(lán)牙會(huì)中斷連接,正好觸發(fā)這個(gè)方法重建連接。重建連接可能造成數(shù)秒后才能讀取到RSSI。 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { [self.manager connectPeripheral:peripheral options:nil]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"%@",error.description); } //返回的藍(lán)牙服務(wù)通知通過(guò)代理實(shí)現(xiàn) - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); return; } for (CBService *service in peripheral.services) { // NSLog(@"Service found with UUID: %@", service.UUID.UUIDString); //發(fā)現(xiàn)服務(wù) if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180D"]])//heart rate { //在一個(gè)服務(wù)中尋找特征值 [peripheral discoverCharacteristics:nil forService:service]; } } } //返回的藍(lán)牙特征值通知通過(guò)代理實(shí)現(xiàn) - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]); return; } for (CBCharacteristic * characteristic in service.characteristics) { NSLog(@"characteristic:%@",characteristic); if( [characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]]) { [self notification:service.UUID characteristicUUID:characteristic.UUID peripheral:peripheral on:YES]; // [self.peripheral setNotifyValue:YES forCharacteristic:characteristic]; } } } //處理藍(lán)牙發(fā)過(guò)來(lái)的數(shù)據(jù) - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { } -(void) notification:(CBUUID *) serviceUUID characteristicUUID:(CBUUID *)characteristicUUID peripheral:(CBPeripheral *)p on:(BOOL)on { CBService *service = [self getServiceFromUUID:serviceUUID p:p]; if (!service) { // if (p.UUID == NULL) return; // zach ios6 addedche // NSLog(@"Could not find service with UUID on peripheral with UUID \n"); return; } CBCharacteristic *characteristic = [self getCharacteristicFromUUID:characteristicUUID service:service]; if (!characteristic) { // if (p.UUID == NULL) return; // zach ios6 added // NSLog(@"Could not find characteristic with UUID on service with UUID on peripheral with UUID\n"); return; } [p setNotifyValue:on forCharacteristic:characteristic]; } -(CBService *) getServiceFromUUID:(CBUUID *)UUID p:(CBPeripheral *)p { for (CBService* s in p.services) { if ([s.UUID isEqual:UUID]) return s; } return nil; //Service not found on this peripheral } -(CBCharacteristic *) getCharacteristicFromUUID:(CBUUID *)UUID service:(CBService*)service { for (CBCharacteristic* c in service.characteristics) { if ([c.UUID isEqual:UUID]) return c; } return nil; //Characteristic not found on this service }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:iOS實(shí)現(xiàn)模擬定位功能的示例代碼
欄 目:IOS
下一篇:iOS藍(lán)牙開(kāi)發(fā) 藍(lán)牙連接和數(shù)據(jù)讀寫(xiě)
本文標(biāo)題:iOS藍(lán)牙開(kāi)發(fā)數(shù)據(jù)實(shí)時(shí)傳輸
本文地址:http://mengdiqiu.com.cn/a1/IOS/11850.html
您可能感興趣的文章
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n))
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11iOS調(diào)試Block引用對(duì)象無(wú)法被釋放的小技巧分享
- 01-11iOS動(dòng)態(tài)更換Icon的全過(guò)程記錄
- 01-11iOS實(shí)現(xiàn)文本分頁(yè)的方法示例
- 01-11iOS常見(jiàn)宏理解及使用方法
- 01-11iOs遷至WKWebView跨過(guò)的一些坑
- 01-11iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
- 01-11Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資源
- 01-11iOS中如何獲取某個(gè)視圖的截圖詳析


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11UILabel顯示定時(shí)器文本跳動(dòng)問(wèn)題的解決
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11詳解MacOs免密登錄CentOs操作步驟
- 01-11iOS動(dòng)態(tài)更換Icon的全過(guò)程記錄
- 01-11iOS調(diào)試Block引用對(duì)象無(wú)法被釋放的小技
- 01-11iOS常見(jiàn)宏理解及使用方法
- 01-11iOS實(shí)現(xiàn)文本分頁(yè)的方法示例
- 01-11iOs遷至WKWebView跨過(guò)的一些坑
- 01-11iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什