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];
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
然后關鍵在于CBCentralManagerDelegate的使用。這個之后再講。
4.2 Step 2 尋找CBPeripheral外設
有了CBCentralManager,接下來就是尋找CBPeripheral外設,方法很簡單:
[self.centralManager scanForPeripheralsWithServices:@[] options:nil];
- 1
- 1
這里的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]; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
我們在這里創建了一個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]; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
我們這里先停止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]; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
我們根據找到的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];
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
這里我們可以使用readValueForCharacteristic:來讀取數據。如果數據是不斷更新的,則可以使用setNotifyValue:forCharacteristic:來實現只要有新數據,就獲取。
4.7 Step 7 處理數據
讀到數據后會調用delegate方法:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{NSData *data = characteristic.value;// Parse data ...}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4.8 Step 8 向設備寫數據
這個很簡單,只要使用:
[self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
- 1
- 1
data是NSData類型。
5 實驗
使用小米手環實驗,得到如下結果:
2015-06-10 16:52:31.607 KetherDemo[13786:1792995] scaning device 2015-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 peripheral 2015-06-10 16:52:37.538 KetherDemo[13786:1793031] peripheral did connect 2015-06-10 16:52:37.984 KetherDemo[13786:1793031] service:FEE7 2015-06-10 16:52:37.985 KetherDemo[13786:1793031] service:Device Information 2015-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 connect 2015-06-10 16:52:38.101 KetherDemo[13786:1793032] Kether did connect 2015-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>",
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
6 小結
通過上面的方法,我們就可以輕松的對BLE進行開發。實際上比想象的要簡單。