Make a bytes packet and send to External accessory Device for print using Bluetooth in iOS

爷,独闯天下 提交于 2019-12-13 03:37:18

问题


I am working to print image using External Accessory in iOS but I have problem to send request to Printer.
Here, I attached image in which Packet need to send Printer. but i don't know how to make this packet and send packet to printer.


回答1:


Here, I attached the sample code to make the packet and send to External Accessory Device (printer) using Objective C language.

- (void)PrintImage:(UIImage *)image
    {
        NSMutableData* imagedata = [UIImageJPEGRepresentation(image, 0.0) mutableCopy];

    NSMutableData *RequestData=[[NSMutableData alloc]init];

    NSString *bitSeries = @"";
    NSData *data = [[NSData alloc]init];
    //    1
    bitSeries = @"00011011";
    data=[self stringToData:bitSeries];
    NSLog(@"1 : %@", data);
    [RequestData appendData:data];
    //    2
    bitSeries = @"00101010";
    data=[self stringToData:bitSeries];
    NSLog(@"2 : %@", data);
    [RequestData appendData:data];
    //    3
    bitSeries = @"01000011";
    data=[self stringToData:bitSeries];
    NSLog(@"3 : %@", data);
    [RequestData appendData:data];
    //    4
    bitSeries = @"01000001";
    data=[self stringToData:bitSeries];
    NSLog(@"4 : %@", data);
    [RequestData appendData:data];
    //    5
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"5 : %@", data);
    [RequestData appendData:data];
    //    6
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"6 : %@", data);
    [RequestData appendData:data];
    //    7
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"7 : %@", data);
    [RequestData appendData:data];
    //    8
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"8 : %@", data);
    [RequestData appendData:data];
    //    9

    NSMutableArray *arrstr = [self intToBinary:(int)[imagedata length]];

    bitSeries = [arrstr objectAtIndex:0];
    data=[self stringToData:bitSeries];
    NSLog(@"9 : %@", data);
    [RequestData appendData:data];

    bitSeries = [arrstr objectAtIndex:1];
    data=[self stringToData:bitSeries];
    NSLog(@"9 : %@", data);
    [RequestData appendData:data];

    bitSeries =[arrstr objectAtIndex:2];
    data=[self stringToData:bitSeries];
    NSLog(@"9 : %@", data);
    [RequestData appendData:data];

    //    10
    bitSeries = @"00000001";
    data=[self stringToData:bitSeries];
    NSLog(@"10 : %@", data);
    [RequestData appendData:data];
    //    11
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"11 : %@", data);
    [RequestData appendData:data];
    //    12
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"12 : %@", data);
    [RequestData appendData:data];

    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"12 : %@", data);
    [RequestData appendData:data];

    //    13
    bitSeries = @"00000000";
    data=[self stringToData:bitSeries];
    NSLog(@"13 : %@", data);
    [RequestData appendData:data];

    NSLog(@"Final Data is : %@", RequestData);


    if (imagedata !=nil)
    {
        SessionData *obj=[SessionData sharedInstance];
        obj.imageData=imagedata;
        obj.RequestData = RequestData;
        [obj setupSessionManager];
    }
    else
    {
        [[[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Select Image." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
    }
}

-(NSData *)stringToData : (NSString *)bitSeries
{
    uint8_t value = strtoul([bitSeries UTF8String], NULL, 2);
    NSLog(@"bitSeries : %@ -------> Value : %u",bitSeries, value);
    NSData *data = [NSData dataWithBytes:&value length:sizeof(value)];
    return data;
}

- (NSMutableArray *)intToBinary:(int)number
{
    int bits =  sizeof(number) * 8;
    int bitcnt=bits;

    NSMutableArray *arrStr=[[NSMutableArray alloc]init];
    NSMutableString *binaryStr = [NSMutableString string];

    for (; bits > 0; bits--, number >>= 1)
    {
        [binaryStr insertString:((number & 1) ? @"1" : @"0") atIndex:0];
    }

    if (bitcnt == 32)
    {
        binaryStr=[[binaryStr substringFromIndex:8] mutableCopy];
    }
    else if(bitcnt ==16)
    {
        binaryStr = [[@"00000000" stringByAppendingString:binaryStr] mutableCopy];
    }
    else if (bitcnt == 8)
    {
        binaryStr = [[@"0000000000000000" stringByAppendingString:binaryStr] mutableCopy];
    }
    [arrStr addObject:[binaryStr substringToIndex:8]];
    [arrStr addObject:[binaryStr substringWithRange:NSMakeRange(8, 8)]];
    [arrStr addObject:[binaryStr substringFromIndex:16]];

    NSLog(@"%@",arrStr);

    return arrStr;
}


来源:https://stackoverflow.com/questions/31399382/make-a-bytes-packet-and-send-to-external-accessory-device-for-print-using-blueto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!