wifidirectplugin 0.0.9 → 1.0.0
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,99 @@
|
|
|
1
1
|
package com.ht.plugins.wifidirect;
|
|
2
2
|
|
|
3
3
|
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;
|
|
4
|
+
import android.net.wifi.WpsInfo;
|
|
5
|
+
import android.net.wifi.p2p.*;
|
|
6
|
+
import android.net.NetworkInfo;
|
|
13
7
|
import android.os.Build;
|
|
14
|
-
import android.
|
|
15
|
-
import android.os.Looper;
|
|
8
|
+
import android.util.Log;
|
|
16
9
|
|
|
17
|
-
import com.getcapacitor
|
|
18
|
-
import com.getcapacitor.Logger;
|
|
19
|
-
import com.getcapacitor.Plugin;
|
|
20
|
-
import com.getcapacitor.PluginCall;
|
|
21
|
-
import com.getcapacitor.PluginMethod;
|
|
10
|
+
import com.getcapacitor.*;
|
|
22
11
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
23
12
|
|
|
13
|
+
import org.json.JSONArray;
|
|
14
|
+
import org.json.JSONException;
|
|
15
|
+
import org.json.JSONObject;
|
|
16
|
+
|
|
17
|
+
import java.util.*;
|
|
18
|
+
|
|
24
19
|
@CapacitorPlugin(name = "WifiDirect")
|
|
25
20
|
public class WifiDirectPlugin extends Plugin {
|
|
26
|
-
|
|
27
21
|
private WifiP2pManager manager;
|
|
28
22
|
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;
|
|
23
|
+
private Context context;
|
|
24
|
+
private List<WifiP2pDevice> peerWifiDirDevices = new ArrayList<>();
|
|
35
25
|
|
|
36
26
|
@Override
|
|
37
27
|
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
|
-
}
|
|
28
|
+
context = getContext();
|
|
29
|
+
manager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
|
|
30
|
+
channel = manager.initialize(context, context.getMainLooper(), null);
|
|
83
31
|
}
|
|
84
32
|
|
|
85
33
|
@PluginMethod
|
|
86
|
-
public void
|
|
87
|
-
WifiManager wifiManager = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
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
|
-
|
|
34
|
+
public void scanWifiPeers(PluginCall call) {
|
|
134
35
|
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
|
135
36
|
@Override
|
|
136
37
|
public void onSuccess() {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
38
|
+
manager.requestPeers(channel, peerList -> {
|
|
39
|
+
peerWifiDirDevices.clear();
|
|
40
|
+
peerWifiDirDevices.addAll(peerList.getDeviceList());
|
|
41
|
+
JSONArray devicesJson = new JSONArray();
|
|
42
|
+
for(WifiP2pDevice device: peerWifiDirDevices) {
|
|
43
|
+
try {
|
|
44
|
+
if(!device.deviceName.toUpperCase().contains("LINKVUE")) continue;
|
|
45
|
+
|
|
46
|
+
JSONObject deviceJson = new JSONObject();
|
|
47
|
+
deviceJson.put("deviceName", device.deviceName);
|
|
48
|
+
deviceJson.put("deviceAddress", device.deviceAddress);
|
|
49
|
+
deviceJson.put("status", device.status);
|
|
50
|
+
deviceJson.put("connected", device.status == WifiP2pDevice.CONNECTED);
|
|
51
|
+
devicesJson.put(deviceJson);
|
|
52
|
+
} catch (JSONException e) {
|
|
53
|
+
e.printStackTrace();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
JSObject ret = new JSObject();
|
|
57
|
+
ret.put("devices", devicesJson);
|
|
58
|
+
call.resolve(ret);
|
|
59
|
+
});
|
|
141
60
|
}
|
|
142
|
-
|
|
61
|
+
|
|
143
62
|
@Override
|
|
144
63
|
public void onFailure(int reason) {
|
|
145
|
-
call.reject("Peer discovery failed
|
|
146
|
-
getContext().unregisterReceiver(receiver);
|
|
64
|
+
call.reject("Peer discovery failed: " + reason);
|
|
147
65
|
}
|
|
148
66
|
});
|
|
149
67
|
}
|
|
150
68
|
|
|
151
|
-
|
|
152
|
-
|
|
69
|
+
@PluginMethod
|
|
70
|
+
public void connectToDevice(PluginCall call) {
|
|
71
|
+
String deviceAddress = call.getString("deviceAddress");
|
|
72
|
+
String pin = call.getString("pin");
|
|
153
73
|
|
|
74
|
+
if(deviceAddress == null) {
|
|
75
|
+
call.reject("Device address is required");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
154
78
|
WifiP2pConfig config = new WifiP2pConfig();
|
|
155
|
-
config.deviceAddress =
|
|
79
|
+
config.deviceAddress = deviceAddress;
|
|
156
80
|
|
|
81
|
+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && pin != null) {
|
|
82
|
+
config.wps.setup = WpsInfo.KEYPAD;
|
|
83
|
+
config.wps.pin = pin;
|
|
84
|
+
}
|
|
157
85
|
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
|
|
158
86
|
@Override
|
|
159
87
|
public void onSuccess() {
|
|
160
|
-
|
|
161
|
-
ret.put("message", "Connecting to " + device.deviceName);
|
|
162
|
-
call.resolve(ret);
|
|
163
|
-
getContext().unregisterReceiver(receiver); // 연결 시도 후 리시버 해제
|
|
88
|
+
call.resolve();
|
|
164
89
|
}
|
|
165
90
|
|
|
166
91
|
@Override
|
|
167
92
|
public void onFailure(int reason) {
|
|
168
|
-
call.reject("Connection failed
|
|
169
|
-
getContext().unregisterReceiver(receiver);
|
|
93
|
+
call.reject("Connection failed: " + reason);
|
|
170
94
|
}
|
|
171
95
|
});
|
|
172
96
|
}
|
|
97
|
+
|
|
98
|
+
|
|
173
99
|
}
|