1.創建中心設備,並設置代理
CBCentralManagerDelegate,CBPeripheralDelegate
。
一般情況下,手機是中心設備,藍牙設備是外圍設備。
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
1.創建之后,會進入到改變藍牙狀態的代理方法中。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
在這個方法中,可以判斷藍牙的狀態,進行相應的操作。
CBCentralManagerStateUnknown = CBManagerStateUnknown,
CBCentralManagerStateResetting = CBManagerStateResetting,
CBCentralManagerStateUnsupported = CBManagerStateUnsupported,
CBCentralManagerStateUnauthorized = CBManagerStateUnauthorized,
CBCentralManagerStatePoweredOff = CBManagerStatePoweredOff,
CBCentralManagerStatePoweredOn = CBManagerStatePoweredOn,
2.當藍牙的狀態是開啟的時候,開始掃描外圍設備。如果藍牙支持后台模式,那么要指定服務的UUID,否則在后台斷開之后,連接不上藍牙,不支持后台的話,就設置成nil。option:CBCentralManagerScanOptionAllowDuplicatesKey 如果為YES,會讓中心設備不斷的監聽外部設備的消息,NO則不能。如果為YES可能會很耗電,根據需求設置就好。
if (central.state == CBCentralManagerStatePoweredOn) {
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:nil];
}
3.當掃描到外圍設備的時候,就會進入下面方法。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<<span style="color: #00b1ff">NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI;
在這個方法中,我們可以根據不同的條件來篩選我們需要連接的外圍設備,我這邊是根據名字來篩選的。匹配到你需要的設備之后,就開始連接這個設備了。
if ([peripheral.name isEqualToString:kPeripheralName]) {
peripheral.delegate = self;
self.selectedPeripheral = peripheral;
// 開始連接設備
[self.centralManager connectPeripheral:self.selectedPeripheral options:nil];
}
4.連接成功和失敗都會進入代理方法。
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error ;
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
這里暫停一下,
如果你附近就一個需要連接的藍牙,那么直接判斷名字來連接就好了,但是如果像藍牙鎖這種,附近可能名字一樣的藍牙有很多,那么你怎么知道你要連接的是哪一個藍牙呢?那么,在這個時候,就要根據藍牙的mac地址來區分了。因為iOS7之后,蘋果就無法獲取設備的mac地址,所以現在的藍牙大部分是將藍牙的mac地址寫在藍牙設備系統的sevrice里面,一般UUID是180A(Device Information),而特征的UUID 是2A23(System ID)。根據你需要連接的藍牙的mac地址,來連接。
好了繼續剛才說的,掃描相應的特征。
for (CBService *service in peripheral.services) {
if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180A"]]) {
[self.selectedPeripheral discoverCharacteristics:nil forService:service];
}
}
5.掃描到特征之后,進入代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;```
在這里就可以進行讀寫的操作了。讀數據有兩種方式,一種是read,一種是notify。我們可以用LightBlue來看到這個特征是否是支持notify,一般固定不變的數據都是用read,會有改變的數據用notify。
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A23"]]) {
// 這里是讀取Mac地址,數據固定,用readValueForCharacteristic,不用setNotifyValue:setNotifyValue
[self.selectedPeripheral readValueForCharacteristic:characteristic];
}
}
6.讀取到數據的時候,進入代理方法。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
在這里根據公司定下的數據規則來解析數據就好了。
我這邊把mac地址的解析出來了,當例子來看吧。
NSString *orStr = characteristic.value.description;
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A23"]]) {
NSMutableString *macString = [[NSMutableString alloc] init];
[macString appendString:[[orStr substringWithRange:NSMakeRange(16, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[orStr substringWithRange:NSMakeRange(14, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[orStr substringWithRange:NSMakeRange(12, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[orStr substringWithRange:NSMakeRange(5, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[orStr substringWithRange:NSMakeRange(3, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[orStr substringWithRange:NSMakeRange(1, 2)] uppercaseString]];
}
7.寫入數據