wifidirectplugin 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,6 +50,13 @@ public class WifiDirectPlugin extends Plugin {
50
50
 
51
51
  private PluginCall pendingConnectCall = null;
52
52
 
53
+ // peer 검색: discoverPeers 는 "시작"만 알리므로, 실제 peer 는 WIFI_P2P_PEERS_CHANGED_ACTION
54
+ // 브로드캐스트가 올 때 requestPeers 로 수집한다. target 을 찾으면 즉시 resolve, 못 찾으면 timeout 으로 마감.
55
+ private PluginCall pendingScanCall = null;
56
+ private String pendingScanTargetName = null;
57
+ private Runnable pendingScanTimeout = null;
58
+ private static final long SCAN_TIMEOUT_MS = 15000L;
59
+
53
60
  // Group 형성 broadcast가 안 올 경우 대비한 timeout과 메인 핸들러
54
61
  private final Handler mainHandler = new Handler(Looper.getMainLooper());
55
62
  private Runnable pendingConnectTimeout = null;
@@ -93,6 +100,11 @@ public class WifiDirectPlugin extends Plugin {
93
100
  pendingConnectCall = null;
94
101
  }
95
102
  }
103
+ } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
104
+ // peer 목록이 갱신됨 → 진행 중인 스캔이 있으면 수집 시도
105
+ if (pendingScanCall != null) {
106
+ requestAndResolvePeers(false);
107
+ }
96
108
  }
97
109
  }
98
110
  };
@@ -127,6 +139,10 @@ public class WifiDirectPlugin extends Plugin {
127
139
  } catch (IllegalArgumentException e) {
128
140
  Log.w(TAG, "Receiver not registered or already registered");
129
141
  }
142
+ if (pendingScanCall != null) {
143
+ pendingScanCall.reject("Plugin unloaded");
144
+ clearPendingScan();
145
+ }
130
146
  unregisterP2pNetworkCallback();
131
147
  }
132
148
  @PluginMethod
@@ -138,47 +154,84 @@ public class WifiDirectPlugin extends Plugin {
138
154
  call.reject("WifiP2pManager or Channel not initialized");
139
155
  return;
140
156
  }
157
+
158
+ // 진행 중인 이전 스캔이 있으면 정리(누수 방지)
159
+ if (pendingScanCall != null) {
160
+ pendingScanCall.reject("superseded by a new scan");
161
+ clearPendingScan();
162
+ }
163
+ pendingScanCall = call;
164
+ pendingScanTargetName = targetDeviceName;
165
+
166
+ // 타임아웃: 그때까지 target peer 를 못 찾으면 현재 목록(빈 배열 가능)으로 마감
167
+ pendingScanTimeout = () -> {
168
+ Log.d(TAG, "scanWifiPeers timeout — resolving with current peers");
169
+ requestAndResolvePeers(true);
170
+ };
171
+ mainHandler.postDelayed(pendingScanTimeout, SCAN_TIMEOUT_MS);
172
+
141
173
  manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
142
174
  @Override
143
175
  public void onSuccess() {
144
- manager.requestPeers(channel, peerList -> {
145
- Log.d(TAG, "scanWifi onSuccess Called");
146
-
147
- peerWifiDirDevices.clear();
148
- peerWifiDirDevices.addAll(peerList.getDeviceList());
149
- JSONArray devicesJson = new JSONArray();
150
- for(WifiP2pDevice device: peerWifiDirDevices) {
151
- try {
152
- if (targetDeviceName != null && !targetDeviceName.isEmpty()) {
153
- if (!device.deviceName.toUpperCase().contains(targetDeviceName.toUpperCase())) {
154
- continue;
155
- }
156
- }
157
- JSONObject deviceJson = new JSONObject();
158
- deviceJson.put("deviceName", device.deviceName);
159
- deviceJson.put("deviceAddress", device.deviceAddress);
160
- deviceJson.put("status", device.status);
161
- deviceJson.put("connected", device.status == WifiP2pDevice.CONNECTED);
162
-
163
- devicesJson.put(deviceJson);
164
- } catch (JSONException e) {
165
- Log.e(TAG, "JSON error: ", e);
166
- }
167
- }
168
- JSObject ret = new JSObject();
169
- ret.put("devices", devicesJson);
170
- call.resolve(ret);
171
- });
176
+ // discovery "시작"된 것뿐. 실제 peer 는 WIFI_P2P_PEERS_CHANGED_ACTION 에서 수집한다.
177
+ Log.d(TAG, "discoverPeers started");
172
178
  }
173
179
 
174
180
  @Override
175
181
  public void onFailure(int reason) {
176
182
  Log.e(TAG, "scanWifi onFailure: " + reason);
183
+ clearPendingScan();
177
184
  call.reject("Peer discovery failed: " + reason);
178
185
  }
179
186
  });
180
187
  }
181
188
 
189
+ // 현재 peer 목록을 받아 target 필터를 적용해 pendingScanCall 을 resolve 한다.
190
+ // resolveEvenIfEmpty=false 면 target 을 찾았을 때만 resolve(아니면 계속 대기), true 면 빈 목록이라도 마감.
191
+ private void requestAndResolvePeers(boolean resolveEvenIfEmpty) {
192
+ if (manager == null || channel == null || pendingScanCall == null) return;
193
+ final String target = pendingScanTargetName;
194
+ manager.requestPeers(channel, peerList -> {
195
+ if (pendingScanCall == null) return; // 이미 처리됨
196
+ peerWifiDirDevices.clear();
197
+ peerWifiDirDevices.addAll(peerList.getDeviceList());
198
+ JSONArray devicesJson = new JSONArray();
199
+ for (WifiP2pDevice device : peerWifiDirDevices) {
200
+ if (target != null && !target.isEmpty()
201
+ && !device.deviceName.toUpperCase().contains(target.toUpperCase())) {
202
+ continue;
203
+ }
204
+ try {
205
+ JSONObject deviceJson = new JSONObject();
206
+ deviceJson.put("deviceName", device.deviceName);
207
+ deviceJson.put("deviceAddress", device.deviceAddress);
208
+ deviceJson.put("status", device.status);
209
+ deviceJson.put("connected", device.status == WifiP2pDevice.CONNECTED);
210
+ devicesJson.put(deviceJson);
211
+ } catch (JSONException e) {
212
+ Log.e(TAG, "JSON error: ", e);
213
+ }
214
+ }
215
+ if (devicesJson.length() > 0 || resolveEvenIfEmpty) {
216
+ PluginCall call = pendingScanCall;
217
+ clearPendingScan();
218
+ JSObject ret = new JSObject();
219
+ ret.put("devices", devicesJson);
220
+ call.resolve(ret);
221
+ }
222
+ // 그 외(target 미발견): 다음 PEERS_CHANGED 또는 timeout 까지 계속 대기
223
+ });
224
+ }
225
+
226
+ private void clearPendingScan() {
227
+ if (pendingScanTimeout != null) {
228
+ mainHandler.removeCallbacks(pendingScanTimeout);
229
+ pendingScanTimeout = null;
230
+ }
231
+ pendingScanCall = null;
232
+ pendingScanTargetName = null;
233
+ }
234
+
182
235
  @PluginMethod
183
236
  public void connectToDevice(PluginCall call) {
184
237
  Log.d(TAG, "connectToDevice Called");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wifidirectplugin",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "HT-Installer Wifi Direct Plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",