蓝牙打印小票

最近个人研究了一下蓝牙,实现了蓝牙打印的相关功能,在此记录

蓝牙的具体步骤


@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];

        

        //可以存储serviceUUID,下次只扫描此设备

        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];

        

        [writeValue:data forCharacteristic:ctype:CBCharacteristicWriteWithResponse];


    }

    

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值