蓝牙打印小票
2015-09-21 15:34
567 查看
最近个人研究了一下蓝牙,实现了蓝牙打印的相关功能,在此记录
蓝牙的具体步骤
@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:(
e518
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];
}
}
蓝牙的具体步骤
@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:(
e518
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];
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- android通过蓝牙接收文件打开时无法自动选择合适的应用程序
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- iOS开发之路--微博“更多”页面
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android提高之蓝牙隐藏API探秘
- Android编程之蓝牙测试实例
- Android提高之蓝牙传感应用实例
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件