最近做了蓝牙打印票据以及标签打印机的功能,在这里做一下总结和分享,希望能对有需要的小伙伴提供帮助
首先废话不多说,直接上代码,先搜索到设备,连接设备然后再慢慢了解原理
1、引入头文件 并遵守协议CBCentralManagerDelegate
,CBPeripheralDelegate
然后初始化蓝牙管理类
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreBluetooth/CBService.h>
centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
2、初始化蓝牙管理类之后会走如下代理回调,当蓝牙状态为CBCentralManagerStatePoweredOn
继续下面的回调发现设备
- (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 = @"蓝牙关闭状态";
break;
case CBCentralManagerStatePoweredOn:
state = @"蓝牙打开状态可用";
break;
case CBCentralManagerStateUnknown:
default:
;
}
NSLog(@"Central manager state: %@", state);
}
3、发现设备可以获取到设备的名称以及信号强度等,然后选择你要连接的设备执行[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}];
//发现设备后即可连接该设备 调用完该方法后会调用代理CBCentralManagerDelegate的- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral表示连接上了设别
//如果不能连接会调用 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
}
4、设备连接失败进入如下代理回调
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
//此时连接发生错误
NSLog(@"connected periphheral failed");
[self alertMessage:@"连接失败!"];
}
5、设备连接成功进入如下代理回调,此时设备已经连接上了 你要做的就是找到该设备上的指定服务 调用完该方法后会调用代理CBPeripheralDelegate
(现在开始调用另一个代理的方法了)的
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"has connected");
//发现指定server
[peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
//发现所有server
[peripheral discoverServices:nil];
}
6、搜索到服务之后根据我们的需要找到我们需要的特性
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error==nil)
{
//在这个方法中我们要查找到我们需要的服务 然后调用discoverCharacteristics方法查找我们需要的特性
//该discoverCharacteristics方法调用完后会调用代理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) {
//在这个方法中我们要找到我们所需的服务的特性 然后调用setNotifyValue方法告知我们要监测这个服务特性的状态变化
//当setNotifyValue方法调用后调用代理CBPeripheralDelegate的- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics)
{
//写数据
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kWriteCharacteristicUUID]])
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
activeWriteCharacteristic = characteristic;
}
//读数据
else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kReadCharacteristicUUID]])
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
activeReadCharacteristic = characteristic;
}
//监听蓝牙设备的广播通知
else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kFlowControlCharacteristicUUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
activeFlowControlCharacteristic = characteristic;
}
}
}
}
8、接下来我们可以给打印机发送打印指令打印数据
[activeDevice writeValue:data forCharacteristic:activeWriteCharacteristic type:CBCharacteristicWriteWithResponse];
9、CBCharacteristicWriteWithResponse
写数据写入成功失败状态会在以下回调中获取
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
{
NSLog(@"Write edata success!");
}
10、打印机打印的结果状态会体现在如下回调中
- (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);
}
总结
以上就是整个蓝牙搜索设备连接设备,发送指令打印的整个流程,当然打印指令发送前还有一个数据的准备阶段,而且不同的打印机支持不同的指令集,这部分后续文章中会写到,如果有小伙伴使用中有不同看法或者疑议的地方,欢迎留言探讨大家一起学习,谢谢