From 1563ca642eedf8781314fb79844bef3c1a433964 Mon Sep 17 00:00:00 2001 From: arcad Date: Thu, 28 Jul 2022 12:50:55 +0100 Subject: [PATCH 1/4] support for bluetooth for iOS tested on ql820nwb --- README.md | 42 +++++++++++++++++++++++++++- src/ios/BRBluetoothPrintOperation.h | 4 +-- src/ios/BRBluetoothPrintOperation.m | 3 +- src/ios/BrotherPrinter.m | 43 +++++++++++++++++++---------- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 32536d6..0202416 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,49 @@ __Tested models:__ `QL-720NW`, `QL-820NWB`, `TD-2120N` ## Supported interfaces (by this plugin): * Wi-Fi (Infrastructure mode) -* Bluetooth (Android only, at the moment, iOS needs more work. See [PR10](https://github.com/gordol/cordova-brother-label-printer/pull/10) +* Bluetooth (Android and iOS) * USB +### BlueTooth notes: +If your app is supporting BlueTooth, you will need the following changes in you rproject: +#### Android permissions +``` + + + +``` +#### iOS + +* For iOS, you need to apply for an MFi PPID. +Please follow the steps at +https://secure6.brother.co.jp/mfi/Top.aspx + +* Code change in your cordova config + +``` + + Bluetooth access for Brother label printer + + + Bluetooth access for Brother label printer + + + + com.brother.ptcbp + + + + Used to discover printers on your network + + + + _printer._tcp + _pdl-datastream._tcp + _ipp._tcp + + +``` + ## Usage diff --git a/src/ios/BRBluetoothPrintOperation.h b/src/ios/BRBluetoothPrintOperation.h index 8a99133..438b997 100644 --- a/src/ios/BRBluetoothPrintOperation.h +++ b/src/ios/BRBluetoothPrintOperation.h @@ -5,9 +5,8 @@ // Created by Kusumoto Naoki on 2015/08/18. // Copyright (c) 2015年 Kusumoto Naoki. All rights reserved. // -/* #import -#import +#import @interface BRBluetoothPrintOperation : NSOperation { } @@ -20,4 +19,3 @@ numberOfPaper:(int)targetNumberOfPaper serialNumber:(NSString *)targetSerialNumber; @end -*/ diff --git a/src/ios/BRBluetoothPrintOperation.m b/src/ios/BRBluetoothPrintOperation.m index 9971825..ab95078 100644 --- a/src/ios/BRBluetoothPrintOperation.m +++ b/src/ios/BRBluetoothPrintOperation.m @@ -5,7 +5,7 @@ // Created by Kusumoto Naoki on 2015/08/18. // Copyright (c) 2015年 Kusumoto Naoki. All rights reserved. // -/* + #import "BRUserDefaults.h" #import "BRBluetoothPrintOperation.h" @@ -81,4 +81,3 @@ -(void)main { } @end -*/ diff --git a/src/ios/BrotherPrinter.m b/src/ios/BrotherPrinter.m index 13c8102..af393e2 100644 --- a/src/ios/BrotherPrinter.m +++ b/src/ios/BrotherPrinter.m @@ -267,10 +267,11 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command { NSString *ipAddress = obj[@"ipAddress"]; NSString *modelName = obj[@"modelName"]; NSString *paperLabelName = obj[@"paperLabelName"]; - NSString *numberOfCopies = obj[@"numberOfCopies"]; + NSString *numberOfCopies = obj[@"numberOfCopies"]; NSString *orientation = obj[@"orientation"]; NSString *customPaperFilePath = obj[@"customPaperFilePath"]; - + NSString *serialNumber = obj[@"serialNumber"]; + if (!modelName) { [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Expected a \"modelName\" key in the given object"] @@ -290,12 +291,23 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command { } if ([@"BLUETOOTH" isEqualToString:port]) { + + if(!serialNumber){ + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Expected a \"serialNumber\" key in the given object for BlueTooth devices"] + callbackId:command.callbackId]; + return; + } [userDefaults setObject:@"0" forKey:kIsWiFi]; [userDefaults setObject:@"1" forKey:kIsBluetooth]; + + [userDefaults + setObject:serialNumber + forKey:kSerialNumber]; } if ([@"NET" isEqualToString:port]) { @@ -305,16 +317,16 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command { [userDefaults setObject:@"0" forKey:kIsBluetooth]; + + [userDefaults + setObject:@"0" + forKey:kSerialNumber]; } [userDefaults setObject:ipAddress forKey:kIPAddress]; - [userDefaults - setObject:@"0" - forKey:kSerialNumber]; - if (paperLabelName) { [userDefaults setObject:paperLabelName @@ -410,7 +422,7 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { NSString *numPaper = [self stringValueFromDefaults:userDefaults forKey:kPrintNumberOfPaperKey withFallback:@"1"]; // Item 1 printInfo.strPaperName = [self stringValueFromDefaults:userDefaults forKey:kPaperLabelName withFallback:@"62mm"]; // Item 2 - printInfo.nOrientation = (int) [self integerValueFromDefaults:userDefaults forKey:kPrintOrientationKey withFallback:0x00]; // Item 3 + printInfo.nOrientation = (int) [self integerValueFromDefaults:userDefaults forKey:kPrintOrientationKey withFallback:Landscape]; // Item 3 printInfo.nPrintMode = (int) [self integerValueFromDefaults:userDefaults forKey:kScalingModeKey withFallback:Fit]; // Item 4 printInfo.scaleValue = [self doubleValueFromDefaults:userDefaults forKey:kScalingFactorKey withFallback:1.0]; // Item 5 /////////// @@ -501,7 +513,7 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { finalDeviceName = [NSString stringWithFormat:@"Brother %@", printerName]; } }]; - + NSFileManager *fileManager = [NSFileManager defaultManager]; if (isBluetooth == 1) { @@ -510,12 +522,13 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { if ([fileManager fileExistsAtPath:_customPaperFilePath]){ [_ptp setCustomPaperFile:_customPaperFilePath]; } -// [_ptp setupForBluetoothDeviceWithSerialNumber:serialNumber]; + + [_ptp setupForBluetoothDeviceWithSerialNumber:serialNumber]; } else if (isWifi == 1) { _ptp = [[BRPtouchPrinter alloc] initWithPrinterName:finalDeviceName interface:CONNECTION_TYPE_WLAN]; - - if ([fileManager fileExistsAtPath:_customPaperFilePath]){ + + if ([fileManager fileExistsAtPath:_customPaperFilePath]){ [_ptp setCustomPaperFile:_customPaperFilePath]; } // [_ptp setIPAddress:ipAddress]; @@ -536,7 +549,7 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { NSOperation *operation = nil; if (isBluetooth == 1) { - /* BRBluetoothPrintOperation *bluetoothPrintOperation = [[BRBluetoothPrintOperation alloc] + BRBluetoothPrintOperation *bluetoothPrintOperation = [[BRBluetoothPrintOperation alloc] initWithOperation:_ptp printInfo:printInfo imgRef:[_image CGImage] @@ -554,7 +567,7 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { context:nil]; operation = bluetoothPrintOperation; - */ + } else if (isWifi == 1) { BRWLANPrintOperation *wlanPrintOperation = [[BRWLANPrintOperation alloc] initWithOperation:_ptp @@ -655,7 +668,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath return; } } else if ([keyPath isEqualToString:@"communicationResultForBT"]) { - /* BRBluetoothPrintOperation *bluetoothOperation = (BRBluetoothPrintOperation *) operation; + BRBluetoothPrintOperation *bluetoothOperation = (BRBluetoothPrintOperation *) operation; BOOL result = bluetoothOperation.communicationResultForBT; NSLog(@"Communication Result: %d", result); if (!result) { @@ -667,7 +680,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath callbackId:_printCallbackId]; _printCallbackId = nil; _image = nil; - }*/ + } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } From 58555255dcd281bc32347efcc4ee6f9bc9c0c5c3 Mon Sep 17 00:00:00 2001 From: arcad Date: Thu, 28 Jul 2022 13:16:45 +0100 Subject: [PATCH 2/4] formatting --- src/ios/BrotherPrinter.m | 66 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/ios/BrotherPrinter.m b/src/ios/BrotherPrinter.m index af393e2..832e42d 100644 --- a/src/ios/BrotherPrinter.m +++ b/src/ios/BrotherPrinter.m @@ -267,7 +267,7 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command { NSString *ipAddress = obj[@"ipAddress"]; NSString *modelName = obj[@"modelName"]; NSString *paperLabelName = obj[@"paperLabelName"]; - NSString *numberOfCopies = obj[@"numberOfCopies"]; + NSString *numberOfCopies = obj[@"numberOfCopies"]; NSString *orientation = obj[@"orientation"]; NSString *customPaperFilePath = obj[@"customPaperFilePath"]; NSString *serialNumber = obj[@"serialNumber"]; @@ -522,7 +522,7 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { if ([fileManager fileExistsAtPath:_customPaperFilePath]){ [_ptp setCustomPaperFile:_customPaperFilePath]; } - + [_ptp setupForBluetoothDeviceWithSerialNumber:serialNumber]; } else if (isWifi == 1) { @@ -549,24 +549,24 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command { NSOperation *operation = nil; if (isBluetooth == 1) { - BRBluetoothPrintOperation *bluetoothPrintOperation = [[BRBluetoothPrintOperation alloc] - initWithOperation:_ptp - printInfo:printInfo - imgRef:[_image CGImage] - numberOfPaper:[numPaper intValue] - serialNumber:serialNumber]; - - [bluetoothPrintOperation addObserver:self - forKeyPath:@"isFinishedForBT" - options:NSKeyValueObservingOptionNew - context:nil]; - - [bluetoothPrintOperation addObserver:self - forKeyPath:@"communicationResultForBT" - options:NSKeyValueObservingOptionNew - context:nil]; - - operation = bluetoothPrintOperation; + BRBluetoothPrintOperation *bluetoothPrintOperation = [[BRBluetoothPrintOperation alloc] + initWithOperation:_ptp + printInfo:printInfo + imgRef:[_image CGImage] + numberOfPaper:[numPaper intValue] + serialNumber:serialNumber]; + + [bluetoothPrintOperation addObserver:self + forKeyPath:@"isFinishedForBT" + options:NSKeyValueObservingOptionNew + context:nil]; + + [bluetoothPrintOperation addObserver:self + forKeyPath:@"communicationResultForBT" + options:NSKeyValueObservingOptionNew + context:nil]; + + operation = bluetoothPrintOperation; } else if (isWifi == 1) { BRWLANPrintOperation *wlanPrintOperation = [[BRWLANPrintOperation alloc] @@ -668,19 +668,19 @@ - (void)observeValueForKeyPath:(NSString *)keyPath return; } } else if ([keyPath isEqualToString:@"communicationResultForBT"]) { - BRBluetoothPrintOperation *bluetoothOperation = (BRBluetoothPrintOperation *) operation; - BOOL result = bluetoothOperation.communicationResultForBT; - NSLog(@"Communication Result: %d", result); - if (!result) { - [operation removeObserver:self forKeyPath:@"isFinishedForBT"]; - [operation removeObserver:self forKeyPath:@"communicationResultForBT"]; - PTSTATUSINFO resultStatus = bluetoothOperation.resultStatus; - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Error occured while Bluetooth printing"] - callbackId:_printCallbackId]; - _printCallbackId = nil; - _image = nil; - } + BRBluetoothPrintOperation *bluetoothOperation = (BRBluetoothPrintOperation *) operation; + BOOL result = bluetoothOperation.communicationResultForBT; + NSLog(@"Communication Result: %d", result); + if (!result) { + [operation removeObserver:self forKeyPath:@"isFinishedForBT"]; + [operation removeObserver:self forKeyPath:@"communicationResultForBT"]; + PTSTATUSINFO resultStatus = bluetoothOperation.resultStatus; + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Error occured while Bluetooth printing"] + callbackId:_printCallbackId]; + _printCallbackId = nil; + _image = nil; + } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } From db8879f4259d94022c1b9b5dd5493a7d435ac99d Mon Sep 17 00:00:00 2001 From: arcad Date: Thu, 28 Jul 2022 13:51:06 +0100 Subject: [PATCH 3/4] bumping version --- package.json | 2 +- plugin.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index bf6f1e0..433ebc5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-brother-label-printer", - "version": "1.5.6", + "version": "1.6.0", "description": "Cordova plugin for brother label printers", "cordova": { "id": "cordova-plugin-brother-label-printer", diff --git a/plugin.xml b/plugin.xml index ac885b6..6b25913 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,7 +3,7 @@ + version="1.6.0"> BrotherPrinter From c84bcb2108f5c9c15cd42e86a7d8e25193a627e7 Mon Sep 17 00:00:00 2001 From: arcad Date: Thu, 28 Jul 2022 14:35:16 +0100 Subject: [PATCH 4/4] missing enum --- src/ios/BRUserDefaults.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ios/BRUserDefaults.h b/src/ios/BRUserDefaults.h index dfd418a..2105b0c 100644 --- a/src/ios/BRUserDefaults.h +++ b/src/ios/BRUserDefaults.h @@ -49,6 +49,14 @@ enum PrintPaperSize Legal }; +// Item 3 + #define kPrintOrientationKey @"PrintOrientationKey" + enum PrintOrientationKey + { + Landscape = 0x00, + Portrate = 0x01 + }; + // Item 4 #define kScalingModeKey @"ScalingModeKey" enum PrintFitKey