From 4eef300fae039a8c7c0d79f92ed78a4c031b75fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvinas=20Jakavi=C4=8Dius?= Date: Tue, 6 Aug 2024 16:25:35 +0200 Subject: [PATCH] Fix: android discovery (#167) * fix: android discovery crash * chore: remove comments * chore: remove comment --- .../EscPosPrinterDiscoveryModule.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterDiscoveryModule.java b/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterDiscoveryModule.java index cac7082..b97504e 100644 --- a/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterDiscoveryModule.java +++ b/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterDiscoveryModule.java @@ -41,13 +41,17 @@ import com.reactnativeescposprinter.EposStringHelper; +import java.util.ArrayList; +import java.util.List; @ReactModule(name = EscPosPrinterDiscoveryModule.NAME) public class EscPosPrinterDiscoveryModule extends ReactContextBaseJavaModule implements ActivityEventListener { private Context mContext; - private WritableArray mPrinterList = null; + + private List mDeviceList = new ArrayList<>(); + private final ReactApplicationContext reactContext; public static final String NAME = "EscPosPrinterDiscovery"; @@ -154,8 +158,8 @@ private void sendEvent(ReactApplicationContext reactContext, String eventName, @ @ReactMethod private void startDiscovery(final ReadableMap paramsMap, Promise promise) { + mDeviceList.clear(); FilterOption mFilterOption = getFilterOptionsFromParams(paramsMap); - mPrinterList = Arguments.createArray(); try { Discovery.start(mContext, mFilterOption, mDiscoveryListener); @@ -219,18 +223,20 @@ public void onDiscovery(final DeviceInfo deviceInfo) { UiThreadUtil.runOnUiThread(new Runnable() { @Override public synchronized void run() { - WritableMap printerData = Arguments.createMap(); - - - printerData.putString("target", deviceInfo.getTarget()); - printerData.putString("deviceName", deviceInfo.getDeviceName()); - printerData.putString("ipAddress", deviceInfo.getIpAddress()); - printerData.putString("macAddress", deviceInfo.getMacAddress()); - printerData.putString("bdAddress", deviceInfo.getBdAddress()); - - mPrinterList.pushMap(printerData); - - sendEvent(reactContext, "onDiscovery", Arguments.fromList(mPrinterList.toArrayList())); + mDeviceList.add(deviceInfo); + + WritableArray mPrinterList = Arguments.createArray(); + for (DeviceInfo device : mDeviceList) { + WritableMap printerData = Arguments.createMap(); + printerData.putString("target", device.getTarget()); + printerData.putString("deviceName", device.getDeviceName()); + printerData.putString("ipAddress", device.getIpAddress()); + printerData.putString("macAddress", device.getMacAddress()); + printerData.putString("bdAddress", device.getBdAddress()); + mPrinterList.pushMap(printerData); + } + + sendEvent(reactContext, "onDiscovery", mPrinterList); } }); }