最近做的是有關(guān)智能硬件相關(guān)的項目,項目完成之后想著整理一下相關(guān)的知識點,以便以后查閱.
一.藍(lán)牙相關(guān)概念
- 藍(lán)牙開發(fā)中主要設(shè)備概念有中心設(shè)備(iPhone手機(jī)),連接設(shè)備這兩個概念
- 搜索過程中有服務(wù)和特征值這兩個概念,可以通過篩選服務(wù)的UUID從而達(dá)到只展示特定某類產(chǎn)品的藍(lán)牙設(shè)備.
- 需要注意一點的是iOS不提供MAC地址,所以設(shè)備的UUID是由手機(jī)的ID和MAC地址算出來的.因此,只有同一手機(jī)連接同一藍(lán)牙設(shè)備的時候,UUID作為唯一標(biāo)識才有用處.
二.藍(lán)牙相關(guān)方法
- 連接藍(lán)牙步驟
// 創(chuàng)建中心設(shè)備管理
_bluetoothManager= [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];
// 開始掃描外設(shè)
[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
- 中心設(shè)備代理方法
// 中心設(shè)備的藍(lán)牙狀態(tài)發(fā)生變化之后會調(diào)用此方法 [必須實現(xiàn)的方法]
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
// 中心設(shè)備狀態(tài)枚舉
typedef NS_ENUM(NSInteger, CBCentralManagerState) {
CBCentralManagerStateUnknown = CBManagerStateUnknown,// 藍(lán)牙狀態(tài)未知
CBCentralManagerStateResetting = CBManagerStateResetting,
CBCentralManagerStateUnsupported = CBManagerStateUnsupported, // 不支持藍(lán)牙
CBCentralManagerStateUnauthorized = CBManagerStateUnauthorized, // 藍(lán)牙未授權(quán)
CBCentralManagerStatePoweredOff = CBManagerStatePoweredOff, // 藍(lán)牙關(guān)閉狀態(tài)
CBCentralManagerStatePoweredOn = CBManagerStatePoweredOn, // 藍(lán)牙開啟狀態(tài)
} NS_DEPRECATED(NA, NA, 5_0, 10_0, "Use CBManagerState instead");
// 應(yīng)用從后臺恢復(fù)到前臺的時候,會和系統(tǒng)藍(lán)牙進(jìn)行同步,調(diào)用此方法
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;
// 代理方法的dict中相關(guān)的key值
CBCentralManagerRestoredStatePeripheralsKey // 返回一個中心設(shè)備正在連接的所有外設(shè)數(shù)組
CBCentralManagerRestoredStateScanServicesKey // 返回一個中心設(shè)備正在掃描的所有服務(wù)UUID的數(shù)組
CBCentralManagerRestoredStateScanOptionsKey // 返回一個字典包含正在被使用的外設(shè)的掃描選項
// 中心設(shè)備發(fā)現(xiàn)外設(shè)的時候調(diào)用的方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;
// 中心設(shè)備連接上外設(shè)時候調(diào)用的方法
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
// 中心設(shè)備連接外設(shè)失敗時調(diào)用的方法
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
// 中心設(shè)備與已連接的外設(shè)斷開連接時調(diào)用的方法
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
- 外設(shè)代理方法
// 連接外設(shè)之后發(fā)現(xiàn)服務(wù)時調(diào)用的方法 調(diào)用discoverServices:之后發(fā)現(xiàn)服務(wù)后會調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
// 連接外設(shè)之后發(fā)現(xiàn)特征時調(diào)用的方法 調(diào)用discoverCharacteristics:forService:之后發(fā)現(xiàn)特征后會調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
// 讀取外設(shè)數(shù)據(jù) 調(diào)用readValueForCharacteristic:之后調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 將數(shù)據(jù)寫入外設(shè) 調(diào)用writeValue:forCharacteristic:type:之后調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 外設(shè)更改名字時調(diào)用的方法
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
// 外設(shè)服務(wù)發(fā)生改變的時候調(diào)用此方法
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);
// 外設(shè)讀取RSSI的方法
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);
// 訂閱之后訂閱是否成功調(diào)用此方法 調(diào)用setNotifyValue:forCharacteristic:之后調(diào)用此代理方法;訂閱特征的特征值有變化的時候 調(diào)用peripheral:didUpdateValueForCharacteristic:error:這個方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 調(diào)用discoverDescriptorsForCharacteristic:之后調(diào)用代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
// 讀取特征描述后調(diào)用方法 調(diào)用readValueForDescriptor:之后調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
// 寫入特征描述后調(diào)用的方法 調(diào)用writeValue:forDescriptor:之后調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
// 調(diào)用discoverIncludedServices:forService:之后發(fā)現(xiàn)includedServices中特定的服務(wù)后調(diào)用此代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
三.info.plist
- 如果需要在后臺使用藍(lán)牙的話,需要在info.plist中Required background modes數(shù)組中添加兩個元素App shares data using CoreBluetooth;App communicates using CoreBluetooth.需要注意的是不要手寫,直接選擇比較好.有次上傳app的時候,因為這個上傳不成功.直接手寫的話,看Info.plist的源文件顯示的不正確,所以還是直接選擇比較好.
- iOS10之后要再Info.plist文件中加入key為Privacy - Bluetooth Peripheral Usage Description的使用藍(lán)牙設(shè)備描述