Skip to content

Instantly share code, notes, and snippets.

@choipd
Created May 13, 2016 14:53
Show Gist options
  • Save choipd/75af21caf0adbae773aaac91b90df85b to your computer and use it in GitHub Desktop.
Save choipd/75af21caf0adbae773aaac91b90df85b to your computer and use it in GitHub Desktop.
Write to ESC/POS protocol to BLE printer
- (IBAction)printHelloWorld:(id)sender {
NSString * str = @"Hello World\n\r";
NSString *command = @"1B2100";
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [command length]/2; i++) {
byte_chars[0] = [command characterAtIndex:i*2];
byte_chars[1] = [command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
[commandToSend appendData:data];
[self.printerPeripheral writeValue:commandToSend forCharacteristic:self.writeCharacteristic type:CBCharacteristicWriteWithoutResponse];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment