藍牙實現(xiàn)打印實現(xiàn)
藍牙連接外設(shè),通過系統(tǒng)框架實現(xiàn),步驟如下:判斷是否打開藍牙——> 搜索藍牙設(shè)備——>連接藍牙設(shè)備——>掃描藍牙服務(wù)——>搜索服務(wù)特性——>如果有打印特性 即可實現(xiàn)打印
判斷藍牙是否打開
? CBCentralManager?*manager = [[CBCentralManageralloc]init];? ? ? ?self.manager?= manager;? ? ? 一旦設(shè)置代理在運行程序的時候,就會調(diào)用協(xié)議里一個必須要完成的方法:? ? ? ?- (void)centralManagerDidUpdateState:(CBCentralManager?*)central;
藍牙掃描設(shè)備
- (void)scanForPeripheralsWithServices:(nullable NSArray *)serviceUUIDs options:(nullable NSDictionary *)options;
( 第一個參數(shù)serviceUUIDs表示掃描帶有相關(guān)服務(wù)的外部設(shè)備,例如填寫@[[CBUUIDUUIDWithString:@"需要連接的外部設(shè)備的服務(wù)的UUID"]],即表示帶有需要連接的外部設(shè)備的服務(wù)的UUID的外部設(shè)備,nil表示掃描全部設(shè)備;? ?
?? options 暫時可以寫一個@{CBCentralManagerScanOptionAllowDuplicatesKey?:@YES}這樣的參數(shù),YES表示會讓中心設(shè)備不斷地監(jiān)聽外部設(shè)備的消息,NO就是不能
篩選設(shè)備并根據(jù)指定藍牙設(shè)備名字連接
- (void)centralManager:(CBCentralManager?*)central didDiscoverPeripheral:(CBPeripheral?*)peripheral advertisementData:(NSDictionary?*)advertisementData RSSI:(NSNumber?*)RSSI;? ? ? 在這個方法里,我們可以根據(jù)我們獲取到的硬件的某些條件進行篩選,然后連接我們需要連接的外部設(shè)備,例如連接名字帶有A的外部設(shè)備:?
? ? ?if?([peripheral.namehasPrefix:@"A"] ) { ? ? ? ??
? ?[manager?connectPeripheral:peripheraloptions:nil];? ??//連接設(shè)備? ? ? ?
? ?}
判斷連接成功失敗-代理方法---(如果連接成功就可以處理你想處理的事情啦~)
- (void)centralManager:(CBCentralManager?*)central didConnectPeripheral:(CBPeripheral?*)peripheral;(連接成功)? ? ?
? - (void)centralManager:(CBCentralManager?*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(nullableNSError?*)error;(連接失?。?/p>
pragma mark ---------------- 發(fā)現(xiàn)服務(wù)的代理 -----------------
(?4、我們在連接成功的方法中開始掃描外部設(shè)備的服務(wù)[peripheral?discoverServices:nil];)
?- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error
#pragma mark ---------------- 服務(wù)特性的代理 -------------------- - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error { if (error)
#pragma mark ---------------- 寫入數(shù)據(jù)的回調(diào) -------------------- - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
掃描到特征之后,我們就可以拿到相應(yīng)的特征進行讀寫操作。? ?
? ?例如進行讀取數(shù)據(jù)的操作:
? ?if?([characteristics.UUID.UUIDStringisEqualToString:@"你需要的特征的UUID"]){? ? ? ? ? ?
?//讀取特征數(shù)據(jù)? ? ? ? ? ?
?[peripheral?readValueForCharacteristic:characteristics];? ?
? ? }? ? ? ?
這就讀取了特征包含的相關(guān)信息,只要讀取就會進入另外一個方法:? ?
? ? - (void)peripheral:(CBPeripheral?*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError?*)error;? ? ? ?
在這個方法里,我們就可以拿到我們需要的數(shù)據(jù)了。進行寫的操作是? ? ? ?
[peripheralwriteValue:data類型的數(shù)據(jù)?forCharacteristic:使用到的特征?type:CBCharacteristicWriteWithResponse];? ? ? ?最后的type類型有兩個,分別是CBCharacteristicWriteWithResponse和 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?CBCharacteristicWriteWithoutResponse;? ? ?
?選擇第一個,每往硬件寫入一次數(shù)據(jù)都會進入?
? ? ? - (void)peripheral:(CBPeripheral?*)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError?*)error;