From 58116527011cd25a155bdc0c9890401a6d87e582 Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Mon, 28 Jun 2021 17:36:42 -0300 Subject: [PATCH 01/11] feat: create new android functions --- .../RNZebraBluetoothPrinterModule.java | 157 +++++++++++++----- 1 file changed, 114 insertions(+), 43 deletions(-) diff --git a/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java b/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java index c904e35..d6b93f5 100644 --- a/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java +++ b/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java @@ -47,6 +47,7 @@ import com.zebra.sdk.printer.SGD; import com.zebra.sdk.printer.ZebraPrinter; import com.zebra.sdk.printer.ZebraPrinterFactory; +import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException; import com.zebra.sdk.printer.discovery.DiscoveredPrinter; public class RNZebraBluetoothPrinterModule extends ReactContextBaseJavaModule implements ActivityEventListener,BluetoothServiceStateObserver { @@ -96,46 +97,46 @@ public void getBluetoothManagerInstance(Context c) { } public RNZebraBluetoothPrinterModule(ReactApplicationContext reactContext,BluetoothService bluetoothService) { // Constructor - super(reactContext); - this.reactContext = reactContext; - context = getReactApplicationContext(); - this.getBluetoothManagerInstance(context); - this.mService = bluetoothService; - this.mService.addStateObserver(this); - IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); - filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); - this.reactContext.registerReceiver(discoverReceiver, filter); + super(reactContext); + this.reactContext = reactContext; + context = getReactApplicationContext(); + this.getBluetoothManagerInstance(context); + this.mService = bluetoothService; + this.mService.addStateObserver(this); + IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); + filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + this.reactContext.registerReceiver(discoverReceiver, filter); } private void cancelDiscovery() { - try { - BluetoothAdapter adapter = this.bluetoothManager.getAdapter(); - if (adapter != null && adapter.isDiscovering()) { - adapter.cancelDiscovery(); - } - Log.d(TAG, "Discover canceled"); - } catch (Exception e) { - // ignore - } + try { + BluetoothAdapter adapter = this.bluetoothManager.getAdapter(); + if (adapter != null && adapter.isDiscovering()) { + adapter.cancelDiscovery(); + } + Log.d(TAG, "Discover canceled"); + } catch (Exception e) { + // ignore + } } @Override public String getName() { - return "RNZebraBluetoothPrinter"; + return "RNZebraBluetoothPrinter"; } private void emitRNEvent(String event, @Nullable WritableMap params) { // emit events to JavaScript - getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(event, params); + getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(event, params); } @ReactMethod public void enableBluetooth(final Promise promise) { - try{ - this.reactContext.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1, null); - promise.resolve("enabled"); - }catch(Exception e) { - promise.reject(e); - } //enable bluetooth + try{ + this.reactContext.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1, null); + promise.resolve("enabled"); + }catch(Exception e) { + promise.reject(e); + } //enable bluetooth } @ReactMethod @@ -384,30 +385,98 @@ public static void sleep(int ms) { e.printStackTrace(); } } + + private byte[] getConfigLabel(ZebraPrinter printer, String label) { + byte[] configLabel = null; + String printLabel = label; - public void disconnect() { + PrinterLanguage printerLanguage = printer.getPrinterControlLanguage(); + + Log.d(TAG, "Language: " + printerLanguage.toString()); + + configLabel = printLabel.getBytes(); + + return configLabel; + } + + @ReactMethod + public void connect(String device, final Promise promise){ try { - if (connection != null) { - connection.close(); - } + connection = new BluetoothConnection(device); + connection.open(); + promise.resolve(true); + } catch (ConnectionException e) { + Log.d(TAG, e.toString()); + promise.reject("ConnectionException", "nable to establish connection"); + } + } + @ReactMethod + public void disconnect() { + try { + connection.close(); } catch (ConnectionException e) { - Log.d("Error on disconnect", e.toString()); + Log.d(TAG, e.toString()); } } - - private byte[] getConfigLabel(ZebraPrinter printer, String label) { - byte[] configLabel = null; - String printLabel = label; + + @ReactMethod + public void isConnected(Promise promise){ try { - SGD.SET("device.languages", "zpl", connection); + promise.resolve(connection.isConnected()); + } catch (ConnectionException e) { + Log.d(TAG, e.toString()); + promise.resolve(false); + } catch (ZebraPrinterLanguageUnknownException e){ + Log.d(TAG, e.toString()); + promise.resolve(false); + } + } - configLabel = printLabel.getBytes(); - + @ReactMethod + public void getPrinterControlLanguage(Promise promise){ + try { + if(!connection.isConnected()){ + promise.reject("ConnectionException", "Printer does not connected"); + } + + ZebraPrinter zebraPrinter = ZebraPrinterFactory.getInstance(connection); + + PrinterLanguage printerLanguage = zebraPrinter.getPrinterControlLanguage(); + + Log.d(TAG, "Zebra Printer Language: " + printerLanguage.toString()); + + promise.resolve(printerLanguage.toString()); } catch (ConnectionException e) { - Log.d("Connection err", e.toString()); + Log.d(TAG, e.toString()); + promise.reject("ConnectionException", "Printer does not connected"); + } catch (ZebraPrinterLanguageUnknownException e){ + Log.d(TAG, e.toString()); + promise.reject("ZebraPrinterLanguageUnknownException", "Printer Instance does not detected"); + } + } + + @ReactMethod + public void fastPrint(String label, final Promise promise){ + try{ + if(!connection.isConnected()){ + promise.reject("ConnectionException", "Printer does not connected"); + } + + ZebraPrinter zebraPrinter = ZebraPrinterFactory.getInstance(connection); + + byte[] labelToPrint = label.getBytes(); + + connection.write(labelToPrint); + + promise.resolve(true); + } catch (ConnectionException e) { + Log.d(TAG, e.toString()); + promise.reject("ConnectionException", "Printer does not connected"); + }catch (Exception e) { + Log.d(TAG, e.toString()); + promise.reject("Exception", "FastPrint failed - " + e.toString()); } - return configLabel; } @ReactMethod @@ -421,20 +490,22 @@ public void print(String device, String label,final Promise promise) { connection.open(); } catch (ConnectionException e) { disconnect(); - Log.d("Connection err", e.toString()); + Log.d(TAG, e.toString()); loading = false; success = false; promise.reject("Unable to establish connection.Please try again!!!"); } if (connection.isConnected()) { try { - Log.d("Connection estd", "here"); + Log.d(TAG, "here"); ZebraPrinter zebraPrinter = ZebraPrinterFactory.getInstance(connection); PrinterStatus status = zebraPrinter.getCurrentStatus(); String pl = SGD.GET("device.languages", connection); + Log.d(TAG, "PL: " + pl); + byte[] configLabel = getConfigLabel(zebraPrinter, label); connection.write(configLabel); sleep(1500); @@ -445,7 +516,7 @@ public void print(String device, String label,final Promise promise) { } catch (Exception err) { success = false; loading = false; - Log.d("Connection err", err.toString()); + Log.d(TAG, err.toString()); promise.reject(err.toString()); } finally { disconnect(); From b394eb68d45f8ef7f04cb90f604ccfdd85fcc936 Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Mon, 28 Jun 2021 17:40:18 -0300 Subject: [PATCH 02/11] fix: replace isConnected function --- .../RNZebraBluetoothPrinterModule.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java b/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java index d6b93f5..7938207 100644 --- a/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java +++ b/android/src/main/java/com/zebrabluetoothprinter/RNZebraBluetoothPrinterModule.java @@ -422,15 +422,8 @@ public void disconnect() { @ReactMethod public void isConnected(Promise promise){ - try { - promise.resolve(connection.isConnected()); - } catch (ConnectionException e) { - Log.d(TAG, e.toString()); - promise.resolve(false); - } catch (ZebraPrinterLanguageUnknownException e){ - Log.d(TAG, e.toString()); - promise.resolve(false); - } + boolean isConnected = connection.isConnected(); + promise.resolve(isConnected); } @ReactMethod From bdc08870526190be09271d495086fc7b27b128d2 Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Wed, 30 Jun 2021 00:14:48 -0300 Subject: [PATCH 03/11] new android functions and readme --- README.md | 191 ++++++++++---- .../RNZebraBluetoothPrinterModule.java | 243 +++++++++--------- 2 files changed, 257 insertions(+), 177 deletions(-) diff --git a/README.md b/README.md index 69c81eb..1e5beac 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,9 @@ - # react-native-zebra-bluetooth-printer ## Getting started `$ npm install react-native-zebra-bluetooth-printer --save` - - ### Mostly automatic installation `$ react-native link react-native-zebra-bluetooth-printer` @@ -15,7 +12,6 @@ Note : For react-native > 0.59, don't use above command it uses auto-linking. ### Manual installation - #### iOS 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` @@ -26,104 +22,185 @@ Note : For react-native > 0.59, don't use above command it uses auto-linking. #### Android 1. Open up `android/app/src/main/java/[...]/MainActivity.java` - - Add `import com.reactlibrary.RNZebraBluetoothPrinterPackage;` to the imports at the top of the file - - Add `new RNZebraBluetoothPrinterPackage()` to the list returned by the `getPackages()` method + +- Add `import com.reactlibrary.RNZebraBluetoothPrinterPackage;` to the imports at the top of the file +- Add `new RNZebraBluetoothPrinterPackage()` to the list returned by the `getPackages()` method + 2. Append the following lines to `android/settings.gradle`: - ``` - include ':react-native-zebra-bluetooth-printer' - project(':react-native-zebra-bluetooth-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zebra-bluetooth-printer/android') - ``` + ``` + include ':react-native-zebra-bluetooth-printer' + project(':react-native-zebra-bluetooth-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zebra-bluetooth-printer/android') + ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: - ``` - compile project(':react-native-zebra-bluetooth-printer') - ``` + ``` + compile project(':react-native-zebra-bluetooth-printer') + ``` ### Update Android Manifest + ``` ``` + ## Usage # RNZebraBluetoothPrinter -RNZebraBluetoothPrinter is a module for Bluetooth management and supports print functionality for zebra bluetooth printers ( only ble devices ). +RNZebraBluetoothPrinter is a module for Bluetooth management and supports print functionality for zebra bluetooth printers ( only ble devices ). It includes features like bluetooth enable/disable, pair/unpair a BLE/Bluetooth device, scan for nearby bluetooth devices and printing using bluetooth. -```javascript -import RNZebraBluetoothPrinter from 'react-native-zebra-bluetooth-printer'; +```javascript +import RNZebraBluetoothPrinter from "react-native-zebra-bluetooth-printer"; ``` + 1. isEnabledBluetooth == > async function, check the status of bluetooth service in the bluetooth client(mobile phone) .Returns true if already enabled, false if not enabled. + ```javascript RNZebraBluetoothPrinter.isEnabledBluetooth().then((res) => { - //do something with res -}) + //do something with res +}); ``` + 2. enableBluetooth == > async function, - Android: requests for bluetooth on/off permission on the android device. - iOS: resolves to nil. + Android: requests for bluetooth on/off permission on the android device. + iOS: resolves to nil. ```javascript RNZebraBluetoothPrinter.enableBluetooth().then((res) => { - //do something with res -}) + //do something with res +}); ``` 3. disableBluetooth == > async function, - Android: disables bluetooth if bluetooth is switched on. - iOS: simply resolves by nil. + Android: disables bluetooth if bluetooth is switched on. + iOS: simply resolves by nil. + ```javascript RNZebraBluetoothPrinter.disableBluetooth().then((res) => { - //do something with res -}) -``` + //do something with res +}); +``` + 4. scanDevices == > async function, scans for available nearby bluetooth devices for a specific period of time. + ```javascript RNZebraBluetoothPrinter.scanDevices().then((deviceArray) => { - //do something with res -}) -``` -5. pairedDevices == > async function, - Android: returns already paired devices. - iOS: resolves to nil. + //do something with res +}); +``` + +5. pairedDevices == > async function, + Android: returns already paired devices. + iOS: resolves to nil. + ```javascript RNZebraBluetoothPrinter.pairedDevices().then((deviceArray) => { - //do something with deviceArray -}) -``` + //do something with deviceArray +}); +``` + 6. connectDevice == > async function, for both android and iOS + ```javascript RNZebraBluetoothPrinter.connectDevice(deviceAddress).then((res) => { - //do something with res - //for android, device address is mac address - //for iOS, device address is a long string like 0C347F9F-2881-9CCB-43B0-205976944626 -}) -``` + //do something with res + //for android, device address is mac address + //for iOS, device address is a long string like 0C347F9F-2881-9CCB-43B0-205976944626 +}); +``` + 7. unpairDevice == > async function, - Android: unpair/disconnect a paired device from paired device list. + Android: unpair/disconnect a paired device from paired device list. + ```javascript RNZebraBluetoothPrinter.unpairDevice(deviceAddress).then((res) => { - //do something with res -}) + //do something with res +}); ``` - iOS: function resolves to nil. -8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. -For example : -```javascript -const zpl = "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; + iOS: function resolves to nil. + +8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. + For example : + +```javascript +const zpl = + "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; ``` + Android + ```javascript -RNZebraBluetoothPrinter.print(deviceAddress,zpl).then((res) => { - //do something with res -}) +RNZebraBluetoothPrinter.print(deviceAddress, zpl).then((res) => { + //do something with res +}); ``` + iOS + +```javascript +RNZebraBluetoothPrinter.print(zpl).then((res) => { + //do something with res +}); +``` + +9. isConnected == > async function, returns a boolean value to indicate whether or not there is an open connection. + For example : + +Android + +```javascript +const isConnected = await RNZebraBluetoothPrinter.isConnected(); +``` + +10. getPrinterControlLanguage == > async function, returns a String containing the connected printer language. + For example : + +Android + ```javascript -RNZebraBluetoothPrinter.print(zpl).then((res)=>{ - //do something with res -}) -``` - \ No newline at end of file +const tags = { + ZPL: "...", + CPCL: "...", + PDF: "...", +}; + +const printerLanguage = + await RNZebraBluetoothPrinter.getPrinterControlLanguage(); + +await RNZebraBluetoothPrinter.print(tags[printerLanguage]); +``` + +### Fast Print - [Android only] + +You can control the start and end of the connection for faster printing. For this to work, you need to open the connection at the beginning of your code, and close it only at the end of the whole process. An example would be: + +- I open the connection when starting the screen, print all the labels that are requested, and finally close the connection. + +```javascript +... + +useEffect(() => { + const connectPrinter = async (device) => { + await RNZebraBluetoothPrinter.connect(device.address); + } + + if(device){ + connectPrinter(device); + } + + return () => RNZebraBluetoothPrinter.disconnect(); +}, [device]) + +... + +const printTags = async (quantity) => { + for(let i=0; i boundDevices = adapter.getBondedDevices(); - for (BluetoothDevice d : boundDevices) { - try { - JSONObject obj = new JSONObject(); - obj.put("name", d.getName()); - obj.put("address", d.getAddress()); - pairedDeivce.pushString(obj.toString()); - } catch (Exception e) { - //ignore. - } - } - promise.resolve(pairedDeivce); - }else{ - promise.resolve(null); - } - - } else { - // User did not enable Bluetooth or an error occured - Log.d(TAG, "BT not enabled"); - if (promise != null) { - promise.reject("ERR", new Exception("BT NOT ENABLED")); - } - } - break; - } - } - } + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { + BluetoothAdapter adapter = this.bluetoothManager.getAdapter(); + Log.d(TAG, "onActivityResult " + resultCode); + switch (requestCode) { + case REQUEST_CONNECT_DEVICE: { + // When DeviceListActivity returns with a device to connect + if (resultCode == Activity.RESULT_OK) { + // Get the device MAC address + String address = data.getExtras().getString( + EXTRA_DEVICE_ADDRESS); + // Get the BLuetoothDevice object + if (adapter!=null && BluetoothAdapter.checkBluetoothAddress(address)) { + BluetoothDevice device = adapter + .getRemoteDevice(address); + // Attempt to connect to the device + mService.connect(device); + } + } + break; + } + case REQUEST_ENABLE_BT: { + Promise promise = promiseMap.remove(PROMISE_ENABLE_BT); + // When the request to enable Bluetooth returns + if (resultCode == Activity.RESULT_OK && promise != null) { + // Bluetooth is now enabled, so set up a session + if(adapter!=null){ + WritableArray pairedDeivce =Arguments.createArray(); + Set boundDevices = adapter.getBondedDevices(); + for (BluetoothDevice d : boundDevices) { + try { + JSONObject obj = new JSONObject(); + obj.put("name", d.getName()); + obj.put("address", d.getAddress()); + pairedDeivce.pushString(obj.toString()); + } catch (Exception e) { + //ignore. + } + } + promise.resolve(pairedDeivce); + }else{ + promise.resolve(null); + } + + } else { + // User did not enable Bluetooth or an error occured + Log.d(TAG, "BT not enabled"); + if (promise != null) { + promise.reject("ERR", new Exception("BT NOT ENABLED")); + } + } + break; + } + } + } @Override - public void onNewIntent(Intent intent) { - - } + public void onNewIntent(Intent intent) {} @ReactMethod public void pairedDevices(final Promise promise) { - this.context = getCurrentActivity(); if( bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { - promise.reject("BT NOT ENABLED"); - } - else { - Set pairedDevices = bluetoothAdapter.getBondedDevices(); - List deviceName = new ArrayList(); - List deviceAddress = new ArrayList(); - List ble = new ArrayList(); - try { - WritableArray app_list = new WritableNativeArray(); - for (BluetoothDevice bt : pairedDevices) { - BluetoothClass bluetoothClass = bt.getBluetoothClass(); // get class of bluetooth device - WritableMap info = new WritableNativeMap(); - info.putString("address", bt.getAddress()); - info.putDouble("class", bluetoothClass.getDeviceClass()); //1664 - info.putString("name",bt.getName()); - info.putString("type","paired"); - app_list.pushMap(info); + promise.reject("BluetoothException", "Bluetooth not available"); + } else { + Set pairedDevices = bluetoothAdapter.getBondedDevices(); + + try { + WritableArray devices = new WritableNativeArray(); + + for (BluetoothDevice device : pairedDevices) { + BluetoothClass class = device.getBluetoothClass(); + + WritableMap info = new WritableNativeMap(); + + info.putString("address", device.getAddress()); + info.putDouble("class", class.getDeviceClass()); //1664 + info.putString("name", device.getName()); + info.putString("type","paired"); + + devices.pushMap(info); } - promise.resolve(app_list); + + promise.resolve(devices); } catch (Exception e) { - promise.reject(E_LAYOUT_ERROR, e); + promise.reject("Exception", e); } } @@ -289,6 +288,7 @@ public void unpairDevice(String deviceAddress,final Promise promise) { promise.reject("bluetooth not enabled"); } } + private void unpair(BluetoothDevice device) { try { Method m = device.getClass().getMethod("removeBond",(Class []) null); @@ -517,48 +517,51 @@ public void print(String device, String label,final Promise promise) { } } + @Override - public void onBluetoothServiceStateChanged(int state, Map bundle) { - Log.d(TAG,"on bluetoothServiceStatChange:"+state); - switch (state) { - case BluetoothService.STATE_CONNECTED: - case MESSAGE_DEVICE_NAME: { - // save the connected device's name - mConnectedDeviceName = (String) bundle.get(DEVICE_NAME); - Promise p = promiseMap.remove(PROMISE_CONNECT); - if (p == null) { Log.d(TAG,"No Promise found."); - WritableMap params = Arguments.createMap(); - params.putString(DEVICE_NAME, mConnectedDeviceName); - emitRNEvent(EVENT_CONNECTED, params); - } else { Log.d(TAG,"Promise Resolve."); - p.resolve(mConnectedDeviceName); - } - - break; - } - case MESSAGE_CONNECTION_LOST: { - //Connection lost should not be the connect result. - // Promise p = promiseMap.remove(PROMISE_CONNECT); - // if (p == null) { - emitRNEvent(EVENT_CONNECTION_LOST, null); - // } else { - // p.reject("Device connection was lost"); - //} - break; - } - case MESSAGE_UNABLE_CONNECT: { //无法连接设备 - Promise p = promiseMap.remove(PROMISE_CONNECT); - if (p == null) { - emitRNEvent(EVENT_UNABLE_CONNECT, null); - } else { - p.reject("Unable to connect device"); - } - - break; - } - default: - break; - } + public void onBluetoothServiceStateChanged(int state, Map bundle) { + Log.d(TAG,"on bluetoothServiceStatChange:"+state); + switch (state) { + case BluetoothService.STATE_CONNECTED: + case MESSAGE_DEVICE_NAME: { + // save the connected device's name + mConnectedDeviceName = (String) bundle.get(DEVICE_NAME); + Promise p = promiseMap.remove(PROMISE_CONNECT); + if (p == null) { Log.d(TAG,"No Promise found."); + WritableMap params = Arguments.createMap(); + params.putString(DEVICE_NAME, mConnectedDeviceName); + emitRNEvent(EVENT_CONNECTED, params); + } else { Log.d(TAG,"Promise Resolve."); + p.resolve(mConnectedDeviceName); + } + + break; + } + case MESSAGE_CONNECTION_LOST: { + //Connection lost should not be the connect result. + // Promise p = promiseMap.remove(PROMISE_CONNECT); + // if (p == null) { + emitRNEvent(EVENT_CONNECTION_LOST, null); + // } else { + // p.reject("Device connection was lost"); + //} + break; + } + case MESSAGE_UNABLE_CONNECT: { //无法连接设备 + Promise p = promiseMap.remove(PROMISE_CONNECT); + if (p == null) { + emitRNEvent(EVENT_UNABLE_CONNECT, null); + } else { + p.reject("Unable to connect device"); + } + + break; + } + default: + break; } - - } \ No newline at end of file + } + + } + +} \ No newline at end of file From 5dabfd1bf10b7781cf7bedc088f8bea47ee39b8a Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Wed, 30 Jun 2021 00:17:23 -0300 Subject: [PATCH 04/11] readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1e5beac..9ac5315 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,7 @@ await RNZebraBluetoothPrinter.print(tags[printerLanguage]); You can control the start and end of the connection for faster printing. For this to work, you need to open the connection at the beginning of your code, and close it only at the end of the whole process. An example would be: - I open the connection when starting the screen, print all the labels that are requested, and finally close the connection. +- The fastPrint function will print to the connected printer. ```javascript ... @@ -198,7 +199,7 @@ useEffect(() => { const printTags = async (quantity) => { for(let i=0; i Date: Wed, 30 Jun 2021 13:05:25 -0300 Subject: [PATCH 05/11] radme --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9ac5315..5b4ce0f 100644 --- a/README.md +++ b/README.md @@ -184,23 +184,23 @@ You can control the start and end of the connection for faster printing. For thi ... useEffect(() => { - const connectPrinter = async (device) => { - await RNZebraBluetoothPrinter.connect(device.address); - } + const connectPrinter = async (device) => { + await RNZebraBluetoothPrinter.connect(device.address); + } - if(device){ - connectPrinter(device); - } + if(device){ + connectPrinter(device); + } - return () => RNZebraBluetoothPrinter.disconnect(); + return () => RNZebraBluetoothPrinter.disconnect(); }, [device]) ... const printTags = async (quantity) => { - for(let i=0; i Date: Wed, 30 Jun 2021 13:35:38 -0300 Subject: [PATCH 06/11] readme update --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 5b4ce0f..556e012 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,7 @@ RNZebraBluetoothPrinter.unpairDevice(deviceAddress).then((res) => { iOS: function resolves to nil. -8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. - For example : +8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. CPCL strings can also be printed using this for Android. ```javascript const zpl = From 771d956f4587ca21b6673fc20aadecc246a6a04a Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Wed, 30 Jun 2021 13:36:25 -0300 Subject: [PATCH 07/11] podspec --- react-native-zebra-bluetooth-printer.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-native-zebra-bluetooth-printer.podspec b/react-native-zebra-bluetooth-printer.podspec index 915b05e..0a9ac7b 100644 --- a/react-native-zebra-bluetooth-printer.podspec +++ b/react-native-zebra-bluetooth-printer.podspec @@ -3,7 +3,7 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) Pod::Spec.new do |s| s.name = "react-native-zebra-bluetooth-printer" - s.version = "1.0.6" + s.version = "1.0.7" s.summary = "RNZebraBluetoothPrinter" s.description = <<-DESC RNZebraBluetoothPrinter From 8d66539ed39bf05e33c514b8d71711e8065957ca Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Wed, 30 Jun 2021 13:41:36 -0300 Subject: [PATCH 08/11] restore readme to conflict resolve --- README.md | 63 ++----------------------------------------------------- 1 file changed, 2 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 556e012..6e25642 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,8 @@ RNZebraBluetoothPrinter.unpairDevice(deviceAddress).then((res) => { iOS: function resolves to nil. -8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. CPCL strings can also be printed using this for Android. +8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. + For example : ```javascript const zpl = @@ -144,63 +145,3 @@ RNZebraBluetoothPrinter.print(zpl).then((res) => { //do something with res }); ``` - -9. isConnected == > async function, returns a boolean value to indicate whether or not there is an open connection. - For example : - -Android - -```javascript -const isConnected = await RNZebraBluetoothPrinter.isConnected(); -``` - -10. getPrinterControlLanguage == > async function, returns a String containing the connected printer language. - For example : - -Android - -```javascript -const tags = { - ZPL: "...", - CPCL: "...", - PDF: "...", -}; - -const printerLanguage = - await RNZebraBluetoothPrinter.getPrinterControlLanguage(); - -await RNZebraBluetoothPrinter.print(tags[printerLanguage]); -``` - -### Fast Print - [Android only] - -You can control the start and end of the connection for faster printing. For this to work, you need to open the connection at the beginning of your code, and close it only at the end of the whole process. An example would be: - -- I open the connection when starting the screen, print all the labels that are requested, and finally close the connection. -- The fastPrint function will print to the connected printer. - -```javascript -... - -useEffect(() => { - const connectPrinter = async (device) => { - await RNZebraBluetoothPrinter.connect(device.address); - } - - if(device){ - connectPrinter(device); - } - - return () => RNZebraBluetoothPrinter.disconnect(); -}, [device]) - -... - -const printTags = async (quantity) => { - for(let i=0; i Date: Wed, 30 Jun 2021 13:43:33 -0300 Subject: [PATCH 09/11] original readme --- README.md | 132 +++++++++++++++++++++++------------------------------- 1 file changed, 57 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 6e25642..69c81eb 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ + # react-native-zebra-bluetooth-printer ## Getting started `$ npm install react-native-zebra-bluetooth-printer --save` + + ### Mostly automatic installation `$ react-native link react-native-zebra-bluetooth-printer` @@ -12,6 +15,7 @@ Note : For react-native > 0.59, don't use above command it uses auto-linking. ### Manual installation + #### iOS 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` @@ -22,126 +26,104 @@ Note : For react-native > 0.59, don't use above command it uses auto-linking. #### Android 1. Open up `android/app/src/main/java/[...]/MainActivity.java` - -- Add `import com.reactlibrary.RNZebraBluetoothPrinterPackage;` to the imports at the top of the file -- Add `new RNZebraBluetoothPrinterPackage()` to the list returned by the `getPackages()` method - + - Add `import com.reactlibrary.RNZebraBluetoothPrinterPackage;` to the imports at the top of the file + - Add `new RNZebraBluetoothPrinterPackage()` to the list returned by the `getPackages()` method 2. Append the following lines to `android/settings.gradle`: - ``` - include ':react-native-zebra-bluetooth-printer' - project(':react-native-zebra-bluetooth-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zebra-bluetooth-printer/android') - ``` + ``` + include ':react-native-zebra-bluetooth-printer' + project(':react-native-zebra-bluetooth-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zebra-bluetooth-printer/android') + ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: - ``` - compile project(':react-native-zebra-bluetooth-printer') - ``` + ``` + compile project(':react-native-zebra-bluetooth-printer') + ``` ### Update Android Manifest - ``` ``` - ## Usage # RNZebraBluetoothPrinter -RNZebraBluetoothPrinter is a module for Bluetooth management and supports print functionality for zebra bluetooth printers ( only ble devices ). +RNZebraBluetoothPrinter is a module for Bluetooth management and supports print functionality for zebra bluetooth printers ( only ble devices ). It includes features like bluetooth enable/disable, pair/unpair a BLE/Bluetooth device, scan for nearby bluetooth devices and printing using bluetooth. - ```javascript -import RNZebraBluetoothPrinter from "react-native-zebra-bluetooth-printer"; -``` +import RNZebraBluetoothPrinter from 'react-native-zebra-bluetooth-printer'; +``` 1. isEnabledBluetooth == > async function, check the status of bluetooth service in the bluetooth client(mobile phone) .Returns true if already enabled, false if not enabled. - ```javascript RNZebraBluetoothPrinter.isEnabledBluetooth().then((res) => { - //do something with res -}); + //do something with res +}) ``` - 2. enableBluetooth == > async function, - Android: requests for bluetooth on/off permission on the android device. - iOS: resolves to nil. + Android: requests for bluetooth on/off permission on the android device. + iOS: resolves to nil. ```javascript RNZebraBluetoothPrinter.enableBluetooth().then((res) => { - //do something with res -}); + //do something with res +}) ``` 3. disableBluetooth == > async function, - Android: disables bluetooth if bluetooth is switched on. - iOS: simply resolves by nil. - + Android: disables bluetooth if bluetooth is switched on. + iOS: simply resolves by nil. ```javascript RNZebraBluetoothPrinter.disableBluetooth().then((res) => { - //do something with res -}); -``` - + //do something with res +}) +``` 4. scanDevices == > async function, scans for available nearby bluetooth devices for a specific period of time. - ```javascript RNZebraBluetoothPrinter.scanDevices().then((deviceArray) => { - //do something with res -}); -``` - -5. pairedDevices == > async function, - Android: returns already paired devices. - iOS: resolves to nil. - + //do something with res +}) +``` +5. pairedDevices == > async function, + Android: returns already paired devices. + iOS: resolves to nil. ```javascript RNZebraBluetoothPrinter.pairedDevices().then((deviceArray) => { - //do something with deviceArray -}); -``` - + //do something with deviceArray +}) +``` 6. connectDevice == > async function, for both android and iOS - ```javascript RNZebraBluetoothPrinter.connectDevice(deviceAddress).then((res) => { - //do something with res - //for android, device address is mac address - //for iOS, device address is a long string like 0C347F9F-2881-9CCB-43B0-205976944626 -}); -``` - + //do something with res + //for android, device address is mac address + //for iOS, device address is a long string like 0C347F9F-2881-9CCB-43B0-205976944626 +}) +``` 7. unpairDevice == > async function, - Android: unpair/disconnect a paired device from paired device list. - + Android: unpair/disconnect a paired device from paired device list. ```javascript RNZebraBluetoothPrinter.unpairDevice(deviceAddress).then((res) => { - //do something with res -}); + //do something with res +}) ``` - - iOS: function resolves to nil. - -8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. - For example : - + iOS: function resolves to nil. +8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. +For example : ```javascript -const zpl = - "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; -``` +const zpl = "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; +``` Android - ```javascript -RNZebraBluetoothPrinter.print(deviceAddress, zpl).then((res) => { - //do something with res -}); +RNZebraBluetoothPrinter.print(deviceAddress,zpl).then((res) => { + //do something with res +}) ``` - iOS - ```javascript -RNZebraBluetoothPrinter.print(zpl).then((res) => { - //do something with res -}); -``` +RNZebraBluetoothPrinter.print(zpl).then((res)=>{ + //do something with res +}) +``` + \ No newline at end of file From 13217b366c4a67e7cc905fc4f78ff23569987ada Mon Sep 17 00:00:00 2001 From: Pedro Entringer Date: Wed, 30 Jun 2021 13:44:10 -0300 Subject: [PATCH 10/11] new readme --- README.md | 191 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 134 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 69c81eb..556e012 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,9 @@ - # react-native-zebra-bluetooth-printer ## Getting started `$ npm install react-native-zebra-bluetooth-printer --save` - - ### Mostly automatic installation `$ react-native link react-native-zebra-bluetooth-printer` @@ -15,7 +12,6 @@ Note : For react-native > 0.59, don't use above command it uses auto-linking. ### Manual installation - #### iOS 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` @@ -26,104 +22,185 @@ Note : For react-native > 0.59, don't use above command it uses auto-linking. #### Android 1. Open up `android/app/src/main/java/[...]/MainActivity.java` - - Add `import com.reactlibrary.RNZebraBluetoothPrinterPackage;` to the imports at the top of the file - - Add `new RNZebraBluetoothPrinterPackage()` to the list returned by the `getPackages()` method + +- Add `import com.reactlibrary.RNZebraBluetoothPrinterPackage;` to the imports at the top of the file +- Add `new RNZebraBluetoothPrinterPackage()` to the list returned by the `getPackages()` method + 2. Append the following lines to `android/settings.gradle`: - ``` - include ':react-native-zebra-bluetooth-printer' - project(':react-native-zebra-bluetooth-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zebra-bluetooth-printer/android') - ``` + ``` + include ':react-native-zebra-bluetooth-printer' + project(':react-native-zebra-bluetooth-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zebra-bluetooth-printer/android') + ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: - ``` - compile project(':react-native-zebra-bluetooth-printer') - ``` + ``` + compile project(':react-native-zebra-bluetooth-printer') + ``` ### Update Android Manifest + ``` ``` + ## Usage # RNZebraBluetoothPrinter -RNZebraBluetoothPrinter is a module for Bluetooth management and supports print functionality for zebra bluetooth printers ( only ble devices ). +RNZebraBluetoothPrinter is a module for Bluetooth management and supports print functionality for zebra bluetooth printers ( only ble devices ). It includes features like bluetooth enable/disable, pair/unpair a BLE/Bluetooth device, scan for nearby bluetooth devices and printing using bluetooth. -```javascript -import RNZebraBluetoothPrinter from 'react-native-zebra-bluetooth-printer'; +```javascript +import RNZebraBluetoothPrinter from "react-native-zebra-bluetooth-printer"; ``` + 1. isEnabledBluetooth == > async function, check the status of bluetooth service in the bluetooth client(mobile phone) .Returns true if already enabled, false if not enabled. + ```javascript RNZebraBluetoothPrinter.isEnabledBluetooth().then((res) => { - //do something with res -}) + //do something with res +}); ``` + 2. enableBluetooth == > async function, - Android: requests for bluetooth on/off permission on the android device. - iOS: resolves to nil. + Android: requests for bluetooth on/off permission on the android device. + iOS: resolves to nil. ```javascript RNZebraBluetoothPrinter.enableBluetooth().then((res) => { - //do something with res -}) + //do something with res +}); ``` 3. disableBluetooth == > async function, - Android: disables bluetooth if bluetooth is switched on. - iOS: simply resolves by nil. + Android: disables bluetooth if bluetooth is switched on. + iOS: simply resolves by nil. + ```javascript RNZebraBluetoothPrinter.disableBluetooth().then((res) => { - //do something with res -}) -``` + //do something with res +}); +``` + 4. scanDevices == > async function, scans for available nearby bluetooth devices for a specific period of time. + ```javascript RNZebraBluetoothPrinter.scanDevices().then((deviceArray) => { - //do something with res -}) -``` -5. pairedDevices == > async function, - Android: returns already paired devices. - iOS: resolves to nil. + //do something with res +}); +``` + +5. pairedDevices == > async function, + Android: returns already paired devices. + iOS: resolves to nil. + ```javascript RNZebraBluetoothPrinter.pairedDevices().then((deviceArray) => { - //do something with deviceArray -}) -``` + //do something with deviceArray +}); +``` + 6. connectDevice == > async function, for both android and iOS + ```javascript RNZebraBluetoothPrinter.connectDevice(deviceAddress).then((res) => { - //do something with res - //for android, device address is mac address - //for iOS, device address is a long string like 0C347F9F-2881-9CCB-43B0-205976944626 -}) -``` + //do something with res + //for android, device address is mac address + //for iOS, device address is a long string like 0C347F9F-2881-9CCB-43B0-205976944626 +}); +``` + 7. unpairDevice == > async function, - Android: unpair/disconnect a paired device from paired device list. + Android: unpair/disconnect a paired device from paired device list. + ```javascript RNZebraBluetoothPrinter.unpairDevice(deviceAddress).then((res) => { - //do something with res -}) + //do something with res +}); ``` - iOS: function resolves to nil. -8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. -For example : -```javascript -const zpl = "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; + iOS: function resolves to nil. + +8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. CPCL strings can also be printed using this for Android. + +```javascript +const zpl = + "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; ``` + Android + ```javascript -RNZebraBluetoothPrinter.print(deviceAddress,zpl).then((res) => { - //do something with res -}) +RNZebraBluetoothPrinter.print(deviceAddress, zpl).then((res) => { + //do something with res +}); ``` + iOS + +```javascript +RNZebraBluetoothPrinter.print(zpl).then((res) => { + //do something with res +}); +``` + +9. isConnected == > async function, returns a boolean value to indicate whether or not there is an open connection. + For example : + +Android + +```javascript +const isConnected = await RNZebraBluetoothPrinter.isConnected(); +``` + +10. getPrinterControlLanguage == > async function, returns a String containing the connected printer language. + For example : + +Android + ```javascript -RNZebraBluetoothPrinter.print(zpl).then((res)=>{ - //do something with res -}) -``` - \ No newline at end of file +const tags = { + ZPL: "...", + CPCL: "...", + PDF: "...", +}; + +const printerLanguage = + await RNZebraBluetoothPrinter.getPrinterControlLanguage(); + +await RNZebraBluetoothPrinter.print(tags[printerLanguage]); +``` + +### Fast Print - [Android only] + +You can control the start and end of the connection for faster printing. For this to work, you need to open the connection at the beginning of your code, and close it only at the end of the whole process. An example would be: + +- I open the connection when starting the screen, print all the labels that are requested, and finally close the connection. +- The fastPrint function will print to the connected printer. + +```javascript +... + +useEffect(() => { + const connectPrinter = async (device) => { + await RNZebraBluetoothPrinter.connect(device.address); + } + + if(device){ + connectPrinter(device); + } + + return () => RNZebraBluetoothPrinter.disconnect(); +}, [device]) + +... + +const printTags = async (quantity) => { + for(let i=0; i Date: Wed, 30 Jun 2021 13:48:08 -0300 Subject: [PATCH 11/11] readme conflict --- README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/README.md b/README.md index 7d756c6..556e012 100644 --- a/README.md +++ b/README.md @@ -119,15 +119,6 @@ RNZebraBluetoothPrinter.unpairDevice(deviceAddress).then((res) => { //do something with res }); ``` -<<<<<<< HEAD -======= - iOS: function resolves to nil. -8. print == > async function, prints specific zpl string from a zebra printer for both android and iOS. CPCL strings can also be printed using this for Android. - -For example : -```javascript -const zpl = "^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^ FO75,75 ^ FR ^ GB100, 100, 100 ^ FS^ FO88, 88 ^ GB50, 50, 50 ^ FS ^XZ"; ->>>>>>> 86546293a73d36809c41c207418002eb9d0c2bb7 iOS: function resolves to nil.