最近做了藍牙打印票據(jù)以及標簽打印機的功能烘浦,在這里做一下總結(jié)和分享生宛,希望能對有需要的小伙伴提供幫助
首先廢話不多說,直接上代碼帖烘,先搜索到設(shè)備亮曹,連接設(shè)備然后再慢慢了解原理
1、引入頭文件 并遵守協(xié)議CBCentralManagerDelegate
,CBPeripheralDelegate
然后初始化藍牙管理類
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreBluetooth/CBService.h>
centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
2秘症、初始化藍牙管理類之后會走如下代理回調(diào)照卦,當藍牙狀態(tài)為CBCentralManagerStatePoweredOn
繼續(xù)下面的回調(diào)發(fā)現(xiàn)設(shè)備
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString * state = nil;
switch ([central state])
{
case CBCentralManagerStateUnsupported:
state = @"The platform doesn't support the Bluetooth Low Energy Central/Client role.";
break;
case CBCentralManagerStateUnauthorized:
state = @"The application is not authorized to use the Bluetooth Low Energy role.";
break;
case CBCentralManagerStatePoweredOff:
state = @"藍牙關(guān)閉狀態(tài)";
break;
case CBCentralManagerStatePoweredOn:
state = @"藍牙打開狀態(tài)可用";
break;
case CBCentralManagerStateUnknown:
default:
;
}
NSLog(@"Central manager state: %@", state);
}
3、發(fā)現(xiàn)設(shè)備可以獲取到設(shè)備的名稱以及信號強度等乡摹,然后選擇你要連接的設(shè)備執(zhí)行[centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : YES}];
開始連接
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
if (peripheral)
{
NSLog(@"foundDevice. name[%s],RSSI[%d]\n",peripheral.name.UTF8String,peripheral.RSSI.intValue);
[centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : YES}];
//發(fā)現(xiàn)設(shè)備后即可連接該設(shè)備 調(diào)用完該方法后會調(diào)用代理CBCentralManagerDelegate的- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral表示連接上了設(shè)別
//如果不能連接會調(diào)用 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
}
4役耕、設(shè)備連接失敗進入如下代理回調(diào)
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
//此時連接發(fā)生錯誤
NSLog(@"connected periphheral failed");
[self alertMessage:@"連接失敗聪廉!"];
}
5瞬痘、設(shè)備連接成功進入如下代理回調(diào),此時設(shè)備已經(jīng)連接上了 你要做的就是找到該設(shè)備上的指定服務(wù) 調(diào)用完該方法后會調(diào)用代理CBPeripheralDelegate
(現(xiàn)在開始調(diào)用另一個代理的方法了)的
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"has connected");
//發(fā)現(xiàn)指定server
[peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
//發(fā)現(xiàn)所有server
[peripheral discoverServices:nil];
}
6、搜索到服務(wù)之后根據(jù)我們的需要找到我們需要的特性
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error==nil)
{
//在這個方法中我們要查找到我們需要的服務(wù) 然后調(diào)用discoverCharacteristics方法查找我們需要的特性
//該discoverCharacteristics方法調(diào)用完后會調(diào)用代理CBPeripheralDelegate的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
for (CBService *service in peripheral.services)
{
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
7板熊、找到需要的read框全、write、notify特性干签,到這一步其實藍牙才算正式建立連接
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error==nil) {
//在這個方法中我們要找到我們所需的服務(wù)的特性 然后調(diào)用setNotifyValue方法告知我們要監(jiān)測這個服務(wù)特性的狀態(tài)變化
//當setNotifyValue方法調(diào)用后調(diào)用代理CBPeripheralDelegate的- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics)
{
//寫數(shù)據(jù)
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kWriteCharacteristicUUID]])
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
activeWriteCharacteristic = characteristic;
}
//讀數(shù)據(jù)
else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kReadCharacteristicUUID]])
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
activeReadCharacteristic = characteristic;
}
//監(jiān)聽藍牙設(shè)備的廣播通知
else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kFlowControlCharacteristicUUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
activeFlowControlCharacteristic = characteristic;
}
}
}
}
8津辩、接下來我們可以給打印機發(fā)送打印指令打印數(shù)據(jù)
[activeDevice writeValue:data forCharacteristic:activeWriteCharacteristic type:CBCharacteristicWriteWithResponse];
9、CBCharacteristicWriteWithResponse
寫數(shù)據(jù)寫入成功失敗狀態(tài)會在以下回調(diào)中獲取
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
{
NSLog(@"Write edata success!");
}
10容劳、打印機打印的結(jié)果狀態(tài)會體現(xiàn)在如下回調(diào)中
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSLog(@"enter didUpdateValueForCharacteristic!");
NSData *data = characteristic.value;
NSLog(@"secondview:read data=%@!",data);
NSLog(@"qzf come here data=%@",data);
}
總結(jié)
以上就是整個藍牙搜索設(shè)備連接設(shè)備喘沿,發(fā)送指令打印的整個流程,當然打印指令發(fā)送前還有一個數(shù)據(jù)的準備階段竭贩,而且不同的打印機支持不同的指令集蚜印,這部分后續(xù)文章中會寫到,如果有小伙伴使用中有不同看法或者疑議的地方留量,歡迎留言探討大家一起學(xué)習(xí)窄赋,謝謝