Skip to content

Commit

Permalink
Fix: android discovery (#167)
Browse files Browse the repository at this point in the history
* fix: android discovery crash

* chore: remove comments

* chore: remove comment
  • Loading branch information
edvijaka authored Aug 6, 2024
1 parent 8f86031 commit 4eef300
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeviceInfo> mDeviceList = new ArrayList<>();

private final ReactApplicationContext reactContext;

public static final String NAME = "EscPosPrinterDiscovery";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
});
}
Expand Down

0 comments on commit 4eef300

Please sign in to comment.