wifidirectplugin 0.0.9 → 1.0.1
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,173 +1,102 @@
|
|
|
1
1
|
package com.ht.plugins.wifidirect;
|
|
2
2
|
|
|
3
|
+
import static android.content.ContentValues.TAG;
|
|
4
|
+
|
|
3
5
|
import android.content.Context;
|
|
4
|
-
import android.
|
|
5
|
-
import android.
|
|
6
|
-
import android.
|
|
7
|
-
import android.net.wifi.p2p.WifiP2pDevice;
|
|
8
|
-
import android.net.wifi.p2p.WifiP2pManager;
|
|
9
|
-
import android.net.wifi.p2p.WifiP2pConfig;
|
|
10
|
-
import android.net.wifi.p2p.WifiP2pDeviceList;
|
|
11
|
-
import android.net.wifi.WifiManager;
|
|
12
|
-
import android.content.Intent;
|
|
6
|
+
import android.net.wifi.WpsInfo;
|
|
7
|
+
import android.net.wifi.p2p.*;
|
|
8
|
+
import android.net.NetworkInfo;
|
|
13
9
|
import android.os.Build;
|
|
14
|
-
import android.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
import com.getcapacitor.JSObject;
|
|
18
|
-
import com.getcapacitor.Logger;
|
|
19
|
-
import com.getcapacitor.Plugin;
|
|
20
|
-
import com.getcapacitor.PluginCall;
|
|
21
|
-
import com.getcapacitor.PluginMethod;
|
|
10
|
+
import android.util.Log;
|
|
11
|
+
|
|
12
|
+
import com.getcapacitor.*;
|
|
22
13
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
23
14
|
|
|
15
|
+
import org.json.JSONArray;
|
|
16
|
+
import org.json.JSONException;
|
|
17
|
+
import org.json.JSONObject;
|
|
18
|
+
|
|
19
|
+
import java.util.*;
|
|
20
|
+
|
|
24
21
|
@CapacitorPlugin(name = "WifiDirect")
|
|
25
22
|
public class WifiDirectPlugin extends Plugin {
|
|
26
|
-
|
|
27
23
|
private WifiP2pManager manager;
|
|
28
24
|
private WifiP2pManager.Channel channel;
|
|
29
|
-
|
|
30
|
-
private
|
|
31
|
-
private IntentFilter intentFilter;
|
|
32
|
-
|
|
33
|
-
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
34
|
-
private boolean isConnecting = false;
|
|
25
|
+
private Context context;
|
|
26
|
+
private List<WifiP2pDevice> peerWifiDirDevices = new ArrayList<>();
|
|
35
27
|
|
|
36
28
|
@Override
|
|
37
29
|
public void load() {
|
|
38
|
-
|
|
39
|
-
manager = (WifiP2pManager)
|
|
40
|
-
channel = manager.initialize(
|
|
41
|
-
|
|
42
|
-
intentFilter = new IntentFilter();
|
|
43
|
-
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
|
44
|
-
|
|
45
|
-
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
|
46
|
-
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
private void unregisterExistingReceiver() {
|
|
51
|
-
if (receiver != null) {
|
|
52
|
-
try {
|
|
53
|
-
getContext().unregisterReceiver(receiver);
|
|
54
|
-
} catch (IllegalArgumentException ignored) {
|
|
55
|
-
}
|
|
56
|
-
receiver = null;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private void cancelExistingConnection(Runnable onComplete) {
|
|
61
|
-
if (!isConnecting) {
|
|
62
|
-
onComplete.run();
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
|
|
68
|
-
manager.cancelConnect(channel, new WifiP2pManager.ActionListener() {
|
|
69
|
-
@Override
|
|
70
|
-
public void onSuccess() {
|
|
71
|
-
onComplete.run();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
@Override
|
|
75
|
-
public void onFailure(int reason) {
|
|
76
|
-
onComplete.run();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
} catch (Throwable t) {
|
|
80
|
-
isConnecting = false;
|
|
81
|
-
onComplete.run();
|
|
82
|
-
}
|
|
30
|
+
context = getContext();
|
|
31
|
+
manager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
|
|
32
|
+
channel = manager.initialize(context, context.getMainLooper(), null);
|
|
83
33
|
}
|
|
84
34
|
|
|
85
35
|
@PluginMethod
|
|
86
|
-
public void
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
90
|
-
if (!wifiManager.isWifiEnabled()) {
|
|
91
|
-
// Android 10 이상부터는 WiFi 직접 켜기 불가, 설정 화면 이동 요청
|
|
92
|
-
Intent panelIntent = new Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
|
|
93
|
-
panelIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
94
|
-
getContext().startActivity(panelIntent);
|
|
95
|
-
|
|
96
|
-
call.reject("WiFi is disabled. Please enable WiFi first.");
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
unregisterExistingReceiver();
|
|
102
|
-
|
|
103
|
-
cancelExistingConnection(() -> {
|
|
104
|
-
receiver = new BroadcastReceiver() {
|
|
105
|
-
@Override
|
|
106
|
-
public void onReceive(Context context, android.content.Intent intent) {
|
|
107
|
-
String action = intent.getAction();
|
|
108
|
-
if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
|
|
109
|
-
manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
|
|
110
|
-
@Override
|
|
111
|
-
public void onPeersAvailable(WifiP2pDeviceList peerList) {
|
|
112
|
-
WifiP2pDevice targetDevice = null;
|
|
113
|
-
for (WifiP2pDevice device : peerList.getDeviceList()) {
|
|
114
|
-
if (device.deviceName != null && device.deviceName.startsWith("DIRECT-LINKVUE")) {
|
|
115
|
-
targetDevice = device;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (targetDevice != null) {
|
|
120
|
-
connectToDevice(targetDevice, call);
|
|
121
|
-
Logger.info("Found and connecting to device: " + targetDevice.deviceName);
|
|
122
|
-
} else {
|
|
123
|
-
call.reject("No Direct-LINKVUE device found");
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
getContext().registerReceiver(receiver, intentFilter);
|
|
133
|
-
|
|
36
|
+
public void scanWifiPeers(PluginCall call) {
|
|
37
|
+
Log.d(TAG, "scanWifiPeers Called");
|
|
134
38
|
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
|
135
39
|
@Override
|
|
136
40
|
public void onSuccess() {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
41
|
+
manager.requestPeers(channel, peerList -> {
|
|
42
|
+
peerWifiDirDevices.clear();
|
|
43
|
+
peerWifiDirDevices.addAll(peerList.getDeviceList());
|
|
44
|
+
JSONArray devicesJson = new JSONArray();
|
|
45
|
+
for(WifiP2pDevice device: peerWifiDirDevices) {
|
|
46
|
+
try {
|
|
47
|
+
if(!device.deviceName.toUpperCase().contains("LINKVUE")) continue;
|
|
48
|
+
|
|
49
|
+
JSONObject deviceJson = new JSONObject();
|
|
50
|
+
deviceJson.put("deviceName", device.deviceName);
|
|
51
|
+
deviceJson.put("deviceAddress", device.deviceAddress);
|
|
52
|
+
deviceJson.put("status", device.status);
|
|
53
|
+
deviceJson.put("connected", device.status == WifiP2pDevice.CONNECTED);
|
|
54
|
+
devicesJson.put(deviceJson);
|
|
55
|
+
} catch (JSONException e) {
|
|
56
|
+
e.printStackTrace();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
JSObject ret = new JSObject();
|
|
60
|
+
ret.put("devices", devicesJson);
|
|
61
|
+
call.resolve(ret);
|
|
62
|
+
});
|
|
141
63
|
}
|
|
142
|
-
|
|
64
|
+
|
|
143
65
|
@Override
|
|
144
66
|
public void onFailure(int reason) {
|
|
145
|
-
call.reject("Peer discovery failed
|
|
146
|
-
getContext().unregisterReceiver(receiver);
|
|
67
|
+
call.reject("Peer discovery failed: " + reason);
|
|
147
68
|
}
|
|
148
69
|
});
|
|
149
70
|
}
|
|
150
71
|
|
|
151
|
-
|
|
152
|
-
|
|
72
|
+
@PluginMethod
|
|
73
|
+
public void connectToDevice(PluginCall call) {
|
|
74
|
+
String deviceAddress = call.getString("deviceAddress");
|
|
75
|
+
String pin = call.getString("pin");
|
|
153
76
|
|
|
77
|
+
if(deviceAddress == null) {
|
|
78
|
+
call.reject("Device address is required");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
154
81
|
WifiP2pConfig config = new WifiP2pConfig();
|
|
155
|
-
config.deviceAddress =
|
|
82
|
+
config.deviceAddress = deviceAddress;
|
|
156
83
|
|
|
84
|
+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && pin != null) {
|
|
85
|
+
config.wps.setup = WpsInfo.KEYPAD;
|
|
86
|
+
config.wps.pin = pin;
|
|
87
|
+
}
|
|
157
88
|
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
|
|
158
89
|
@Override
|
|
159
90
|
public void onSuccess() {
|
|
160
|
-
|
|
161
|
-
ret.put("message", "Connecting to " + device.deviceName);
|
|
162
|
-
call.resolve(ret);
|
|
163
|
-
getContext().unregisterReceiver(receiver); // 연결 시도 후 리시버 해제
|
|
91
|
+
call.resolve();
|
|
164
92
|
}
|
|
165
93
|
|
|
166
94
|
@Override
|
|
167
95
|
public void onFailure(int reason) {
|
|
168
|
-
call.reject("Connection failed
|
|
169
|
-
getContext().unregisterReceiver(receiver);
|
|
96
|
+
call.reject("Connection failed: " + reason);
|
|
170
97
|
}
|
|
171
98
|
});
|
|
172
99
|
}
|
|
100
|
+
|
|
101
|
+
|
|
173
102
|
}
|