iOS開發 之 穿戴式裝置 藍芽4.0 BLE 開發,iosble
1 前言
當前有越來越多的穿戴式裝置使用了藍芽4.0 BLE(Bluetooth Low Energy)。對於iOS開發而言,Apple之前專門推出CoreBluetooth的Framework來支援BLE的開發。對於硬體開發有瞭解的朋友應該知道,在之前使用低版本的藍芽的裝置,要串連到iOS裝置上,需要註冊MFI,擁有MFI協議才能進行相應的開發。如果大家關注我之前對LEGO EV3的研究,就可以發現,EV3是使用了藍芽2.1,因此需要MFI協議來進行開發。
本文將一步一步講解如何使用CoreBluetooth架構來與各種穿戴式裝置進行通訊,使用 小米手環 來進行基本的測試。
2 開發環境
1 Macbook Pro Mac OS X 10.10
2 Xcode 6.3.2
3 iPhone 5s v8.1
4 小米手環
3 基本流程
要開發藍芽,需要對整個通訊過程有個基本瞭解。這裡我摘錄一些Apple官方的文檔Core Bluetooth Programming Guide的圖片來加以說明。這個文檔其實對於開發的流程寫的是非常的清楚,大家最好可以看一下。
3.1 穿戴式裝置與iOS互聯方式
從上面這幅圖可以看到,我們的iOS裝置是Central,用來接收資料和發送命令,而外設比如小米手環是Peripheral,向外傳輸資料和接收命令。我們要做的就是通過Central來串連Peripheral,然後實現資料的接收和控制指令的發送。在做到這一步之後,再根據具體的硬體,對接收到的資料進行parse解析。
3.2 穿戴式裝置藍芽的資料結構
這裡用的是心率裝置來做說明,每個外設Peripheral都有對應的服務Service,比如這裡是心率Service。一個外設可以有不止一個s、Service。每個service裡面可以有多個屬性Characteristic,比如這裡有兩個Characteristic,一個是用來測量心率,一個是用來定位位置。
那麼很關鍵的一點是每個Service,每個Characteristic都是用UUID來確定的。UUID就是每個Service或Characteristic的identifier。
大家可以在iPhone上下載LightBlue這個應用。可以在這裡查看一些裝置的UUID。
在實際使用中,我們都是要通過UUID來擷取資料。這點非常重要。
在CoreBluetooth中,其具體的資料結構圖如下:
4 Step-By-Step 上手BLE開發4.1 Step 1 建立CBCentralManager
從名字上大家可以很清楚的知道,這個類是用來管理BLE的。我們也就是通過這個類來實現串連。
先建立一個:
@property (nonatomic,strong) CBCentralManager *centralManager;dispatch_queue_t centralQueue = dispatch_queue_create("com.manmanlai", DISPATCH_QUEUE_SERIAL); self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue];
然後關鍵在於CBCentralManagerDelegate的使用。這個之後再講。
4.2 Step 2 尋找CBPeripheral外設
有了CBCentralManager,接下來就是尋找CBPeripheral外設,方法很簡單:
[self.centralManager scanForPeripheralsWithServices:@[] options:nil];
這裡的Service就是對應的UUID,如果為空白,這scan所有service。
4.3 Step 3 串連CBPeripheral
在上一步中,如果找到了裝置,則CBCentralManager的delegate會調用下面的方法:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ NSLog(@"name:%@",peripheral); if (!peripheral || !peripheral.name || ([peripheral.name isEqualToString:@""])) { return; } if (!self.peripheral || (self.peripheral.state == CBPeripheralStateDisconnected)) { self.peripheral = peripheral; self.peripheral.delegate = self; NSLog(@"connect peripheral"); [self.centralManager connectPeripheral:peripheral options:nil]; }}
我們在這裡建立了一個CBPeripheral的對象,然後直接連接
CBPeripheral的對象也需要設定delegate.
4.4 Step 4 尋找Service
如果Peripheral串連成功的話,就會調用delegate的方法:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ if (!peripheral) { return; } [self.centralManager stopScan]; NSLog(@"peripheral did connect"); [self.peripheral discoverServices:nil];}
我們這裡先停止Scan,然後讓Peripheral外設尋找其Service。
4.5 Step 5 尋找Characteristic
找到Service後會調用下面的方法:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ NSArray *services = nil; if (peripheral != self.peripheral) { NSLog(@"Wrong Peripheral.\n"); return ; } if (error != nil) { NSLog(@"Error %@\n", error); return ; } services = [peripheral services]; if (!services || ![services count]) { NSLog(@"No Services"); return ; } for (CBService *service in services) { NSLog(@"service:%@",service.UUID); [peripheral discoverCharacteristics:nil forService:service]; }}
我們根據找到的service尋找其對應的Characteristic。
4.6 Step 6 找到Characteristic後讀取資料
找到Characteristic後會調用下面的delegate方法:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ NSLog(@"characteristics:%@",[service characteristics]); NSArray *characteristics = [service characteristics]; if (peripheral != self.peripheral) { NSLog(@"Wrong Peripheral.\n"); return ; } if (error != nil) { NSLog(@"Error %@\n", error); return ; } self.characteristic = [characteristics firstObject]; //[self.peripheral readValueForCharacteristic:self.characteristic]; [self.peripheral setNotifyValue:YES forCharacteristic:self.characteristic];
這裡我們可以使用readValueForCharacteristic:來讀取資料。如果資料是不斷更新的,則可以使用setNotifyValue:forCharacteristic:來實現只要有新資料,就擷取。
4.7 Step 7 處理資料
讀到資料後會調用delegate方法:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ NSData *data = characteristic.value; // Parse data ...}
4.8 Step 8 向裝置寫資料
這個很簡單,只要使用:
[self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
data是NSData類型。
5 實驗
使用小米手環實驗,得到如下結果:
2015-06-10 16:52:31.607 KetherDemo[13786:1792995] scaning device2015-06-10 16:52:33.474 KetherDemo[13786:1793032] name:<CBPeripheral: 0x1700e4380, identifier = 6FF833E3-93C1-28C6-CBC0-74A706AAAE31, name = LS_SCA16, state = disconnected>2015-06-10 16:52:33.475 KetherDemo[13786:1793032] connect peripheral2015-06-10 16:52:37.538 KetherDemo[13786:1793031] peripheral did connect2015-06-10 16:52:37.984 KetherDemo[13786:1793031] service:FEE72015-06-10 16:52:37.985 KetherDemo[13786:1793031] service:Device Information2015-06-10 16:52:38.099 KetherDemo[13786:1793032] characteristics:( "<CBCharacteristic: 0x17409c250, UUID = FEC8, properties = 0x20, value = (null), notifying = NO>", "<CBCharacteristic: 0x17409c200, UUID = FEC7, properties = 0x8, value = (null), notifying = NO>")2015-06-10 16:52:38.100 KetherDemo[13786:1793032] Kether did connect2015-06-10 16:52:38.101 KetherDemo[13786:1793032] Kether did connect2015-06-10 16:52:38.280 KetherDemo[13786:1793031] characteristics:( "<CBCharacteristic: 0x17009f270, UUID = Manufacturer Name String, properties = 0x2, value = (null), notifying = NO>", "<CBCharacteristic: 0x17009f2c0, UUID = Model Number String, properties = 0x2, value = (null), notifying = NO>", "<CBCharacteristic: 0x17009f310, UUID = Serial Number String, properties = 0x2, value = (null), notifying = NO>", "<CBCharacteristic: 0x17009eb90, UUID = Hardware Revision String, properties = 0x2, value = (null), notifying = NO>", "<CBCharacteristic: 0x17009f0e0, UUID = Firmware Revision String, properties = 0x2, value = (null), notifyi`` = NO>",
6 小結
通過上面的方法,我們就可以輕鬆的對BLE進行開發。實際上比想象的要簡單。
【本文為原創文章,轉載請註明出處:blog.csdn.net/songrotek】