最近个人研究了一下蓝牙,实现了蓝牙打印的相关功能,在此记录
蓝牙的具体步骤
@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
{
//系统蓝牙设备管理对象,可以把他理解为主设备,通过他,可以去扫描和链接外设
CBCentralManager *centralManager;
CBPeripheral * p;
CBCharacteristic * c;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
centralManager = [[CBCentralManageralloc]initWithDelegate:selfqueue:dispatch_get_main_queue()];
}
#pragma mark - 蓝牙状态
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
caseCBCentralManagerStateUnknown:
NSLog(@">>>CBCentralManagerStateUnknown");
break;
caseCBCentralManagerStateResetting:
NSLog(@">>>CBCentralManagerStateResetting");
break;
caseCBCentralManagerStateUnsupported:
NSLog(@">>>CBCentralManagerStateUnsupported");
break;
caseCBCentralManagerStateUnauthorized:
NSLog(@">>>CBCentralManagerStateUnauthorized");
break;
caseCBCentralManagerStatePoweredOff:
NSLog(@">>>CBCentralManagerStatePoweredOff");
break;
caseCBCentralManagerStatePoweredOn:
{
NSLog(@">>>CBCentralManagerStatePoweredOn");
//表示蓝牙已经打开,在此刻调用扫描设备方法,如果第一个参数为nil代表扫描全体设备
[centralManagerscanForPeripheralsWithServices:niloptions:nil];
}
break;
default:
break;
}
}
//扫描到设备会进入方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier, advertisementData[@"kCBAdvDataServiceUUIDs"]);
//防治内存泄露
p = peripheral;
//调用连接设备方法
[centralManagerconnectPeripheral:poptions:nil];
}
//连接到Peripherals-成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"连接成功:%@",peripheral.name);
peripheral.delegate = self;
[central stopScan];
[peripheral discoverServices:nil] ;
}
//连接到Peripherals-失败
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheralname],[error localizedDescription]);
}
//Peripherals断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@">>>外设连接断开连接 %@: %@\n", [peripheralname], [error localizedDescription]);
//停止扫描
[central stopScan];
//断开连接
[central cancelPeripheralConnection:peripheral];
}
#pragma mark - 扫描到服务与特征
//扫描到服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error)
{
NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
return;
}
for (CBService *servicein peripheral.services)
{
[peripheral discoverCharacteristics:nilforService:service];
//可以存储service的UUID,下次只扫描此设备
NSLog(@"Service found with UUID: %@", service.UUID);
}
}
//扫描的设备的特征,可以在此进行读取设备的信息
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error)
{
NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
return;
}
for (int i =0; i < service.characteristics.count; i++) {
CBCharacteristic *characteristic = service.characteristics[i];
if (characteristic.properties & CBCharacteristicPropertyWrite) {
p = peripheral;
c = characteristic;
[self printTicket]
NSLog(@"characteristics found with UUID: %@",characteristic.UUID);
}
}
}
//扫描到设备特征后打印小票
- (void)printTicket {
NSMutableArray *array=[[NSMutableArrayalloc] init];
[array addObject:@"欢迎光临,谢谢惠顾"];
[array addObject:@"技术支持:免费进销存-云门店"];
for (NSInteger i =0; i < array.count; i++) {
NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *curPrintContent = [NSStringstringWithFormat:@"%@",array[i]];
NSString *printed = [curPrintContent stringByAppendingFormat:@"%c", '\n'];
NSData *data= [printed dataUsingEncoding: enc];
[p writeValue:data forCharacteristic:ctype:CBCharacteristicWriteWithResponse];
}
}