wifidirectplugin 1.1.0 → 1.1.2

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.
@@ -1,6 +1,9 @@
1
1
  package com.ht.plugins.wifidirect;
2
2
 
3
+ import android.content.BroadcastReceiver;
3
4
  import android.content.Context;
5
+ import android.content.Intent;
6
+ import android.content.IntentFilter;
4
7
  import android.net.wifi.WpsInfo;
5
8
  import android.net.wifi.p2p.*;
6
9
  import android.net.NetworkInfo;
@@ -24,13 +27,44 @@ public class WifiDirectPlugin extends Plugin {
24
27
  private List<WifiP2pDevice> peerWifiDirDevices = new ArrayList<>();
25
28
  private static final String TAG = "WifiDirectPlugin";
26
29
 
30
+ private final BroadcastReceiver wifiP2pReceiver = new BroadcastReceiver() {
31
+ @Override
32
+ public void onReceive(Context context, Intent intent) {
33
+ String action = intent.getAction();
34
+
35
+ if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
36
+ NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
37
+ if (networkInfo != null && networkInfo.isConnected()) {
38
+ Log.d(TAG, "Wi-Fi Direct connected");
39
+
40
+ // 필요한 경우 연결된 상태를 Flutter/Ionic에 전달하는 코드를 여기서 실행
41
+ // 예: call JavaScript 이벤트 전송 또는 콜백 호출
42
+ } else {
43
+ Log.d(TAG, "Wi-Fi Direct disconnected");
44
+ // 연결 해제 상태 처리
45
+ }
46
+ }
47
+ }
48
+ };
49
+
27
50
  @Override
28
51
  public void load() {
29
52
  context = getContext();
30
53
  manager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
31
54
  channel = manager.initialize(context, context.getMainLooper(), null);
55
+
56
+ IntentFilter intentFilter = new IntentFilter();
57
+ intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
58
+ context.registerReceiver(wifiP2pReceiver, intentFilter);
32
59
  }
33
60
 
61
+ public void unload() {
62
+ try {
63
+ context.unregisterReceiver(wifiP2pReceiver);
64
+ } catch (IllegalArgumentException e) {
65
+ Log.w(TAG, "Receiver not registerd or already registered");
66
+ }
67
+ }
34
68
  @PluginMethod
35
69
  public void scanWifiPeers(PluginCall call) {
36
70
  if(manager == null || channel == null) {
@@ -78,34 +112,54 @@ public class WifiDirectPlugin extends Plugin {
78
112
  @PluginMethod
79
113
  public void connectToDevice(PluginCall call) {
80
114
  Log.d(TAG, "connectToDevice Called");
115
+ if(manager == null || channel == null) {
116
+ Log.e(TAG, "WifiP2pManager or Channel is null");
117
+ call.reject("WifiP2pManager or Channel not initialized");
118
+ return;
119
+ }
81
120
  String deviceAddress = call.getString("deviceAddress");
121
+ Log.d(TAG, "Device Address: " + deviceAddress);
82
122
  String pin = call.getString("pin");
83
123
 
84
124
  if(deviceAddress == null) {
125
+ Log.e(TAG, "Device address is null");
85
126
  call.reject("Device address is required");
86
127
  return;
87
128
  }
88
129
  WifiP2pConfig config = new WifiP2pConfig();
89
130
  config.deviceAddress = deviceAddress;
90
131
 
91
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && pin != null) {
92
- config.wps.setup = WpsInfo.KEYPAD;
93
- config.wps.pin = pin;
94
- }
95
- manager.connect(channel, config, new WifiP2pManager.ActionListener() {
96
- @Override
97
- public void onSuccess() {
98
- Log.d(TAG, "connect onSuccess Called");
99
- call.resolve();
132
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
133
+ if(pin != null && pin.length() == 8 && pin.matches("\\d+")) {
134
+ config.wps.setup = WpsInfo.KEYPAD;
135
+ config.wps.pin = pin;
136
+ } else {
137
+ Log.d(TAG, "Pin: " + pin);
138
+ Log.w(TAG, "Invalid Pin");
100
139
  }
140
+ }
141
+ try {
142
+ manager.connect(channel, config, new WifiP2pManager.ActionListener() {
143
+ @Override
144
+ public void onSuccess() {
145
+ Log.d(TAG, "connect onSuccess Called");
146
+ call.resolve();
147
+ unload();
148
+ }
149
+
150
+ @Override
151
+ public void onFailure(int reason) {
152
+ Log.e(TAG, "connect onFailure Called");
153
+ call.reject("Connection failed: " + reason);
154
+ unload();
155
+ }
156
+ });
157
+ } catch (Exception e) {
158
+ Log.e(TAG, "connect exception", e);
159
+ call.reject("Connection exception: " + e.getMessage());
160
+ unload();
161
+ }
101
162
 
102
- @Override
103
- public void onFailure(int reason) {
104
- Log.e(TAG, "connect onFailure Called");
105
-
106
- call.reject("Connection failed: " + reason);
107
- }
108
- });
109
163
  }
110
164
 
111
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wifidirectplugin",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "HT-Installer Wifi Direct Plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",