Skip to content

Commit

Permalink
Merge pull request #48 from MenelicSoftware/feature/bluetooth-support…
Browse files Browse the repository at this point in the history
…-for-ios-tested-on-ql820nwb

Feature/bluetooth support for ios tested on ql820nwb
  • Loading branch information
arcadius authored Jul 28, 2022
2 parents 627465f + c84bcb2 commit d3b4189
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 49 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
```
#### 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

```
<config-file parent="NSBluetoothPeripheralUsageDescription" platform="ios" target="*-Info.plist">
<string>Bluetooth access for Brother label printer</string>
</config-file>
<config-file parent="NSBluetoothAlwaysUsageDescription" platform="ios" target="*-Info.plist">
<string>Bluetooth access for Brother label printer</string>
</config-file>
<config-file parent="UISupportedExternalAccessoryProtocols" platform="ios" target="*-Info.plist">
<array>
<string>com.brother.ptcbp</string>
</array>
</config-file>
<config-file parent="NSLocalNetworkUsageDescription" platform="ios" target="*-Info.plist">
<string>Used to discover printers on your network</string>
</config-file>
<config-file parent="NSBonjourServices" platform="ios" target="*-Info.plist">
<array>
<string>_printer._tcp</string>
<string>_pdl-datastream._tcp</string>
<string>_ipp._tcp</string>
</array>
</config-file>
```


## Usage

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-brother-label-printer"
version="1.5.6">
version="1.6.0">

<name>BrotherPrinter</name>

Expand Down
4 changes: 1 addition & 3 deletions src/ios/BRBluetoothPrintOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
// Created by Kusumoto Naoki on 2015/08/18.
// Copyright (c) 2015年 Kusumoto Naoki. All rights reserved.
//
/*
#import <Foundation/Foundation.h>
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
#import <BRLMPrinterKit/BRPtouchPrinterKit.h>

@interface BRBluetoothPrintOperation : NSOperation {
}
Expand All @@ -20,4 +19,3 @@
numberOfPaper:(int)targetNumberOfPaper
serialNumber:(NSString *)targetSerialNumber;
@end
*/
3 changes: 1 addition & 2 deletions src/ios/BRBluetoothPrintOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -81,4 +81,3 @@ -(void)main {
}

@end
*/
8 changes: 8 additions & 0 deletions src/ios/BRUserDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 54 additions & 41 deletions src/ios/BrotherPrinter.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ - (void)setPrinter:(CDVInvokedUrlCommand *)command {
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"]
Expand All @@ -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]) {
Expand All @@ -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
Expand Down Expand Up @@ -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
///////////
Expand Down Expand Up @@ -501,7 +513,7 @@ - (void)printViaSDK:(CDVInvokedUrlCommand *)command {
finalDeviceName = [NSString stringWithFormat:@"Brother %@", printerName];
}
}];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (isBluetooth == 1) {
Expand All @@ -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];
Expand All @@ -536,25 +549,25 @@ - (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];
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;

[bluetoothPrintOperation addObserver:self
forKeyPath:@"communicationResultForBT"
options:NSKeyValueObservingOptionNew
context:nil];
operation = bluetoothPrintOperation;
*/
} else if (isWifi == 1) {
BRWLANPrintOperation *wlanPrintOperation = [[BRWLANPrintOperation alloc]
initWithOperation:_ptp
Expand Down Expand Up @@ -655,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];
}
Expand Down

0 comments on commit d3b4189

Please sign in to comment.