wifidirectplugin 0.0.8 → 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,158 +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());
|
|
23
|
+
private Context context;
|
|
24
|
+
private List<WifiP2pDevice> peerWifiDirDevices = new ArrayList<>();
|
|
34
25
|
|
|
35
26
|
@Override
|
|
36
27
|
public void load() {
|
|
37
|
-
|
|
38
|
-
manager = (WifiP2pManager)
|
|
39
|
-
channel = manager.initialize(
|
|
40
|
-
|
|
41
|
-
intentFilter = new IntentFilter();
|
|
42
|
-
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
|
43
|
-
|
|
44
|
-
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
|
45
|
-
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private void unregisterExistingReceiver() {
|
|
50
|
-
if (receiver != null) {
|
|
51
|
-
try {
|
|
52
|
-
getContext().unregisterReceiver(receiver);
|
|
53
|
-
} catch (IllegalArgumentException ignored) {
|
|
54
|
-
}
|
|
55
|
-
receiver = null;
|
|
56
|
-
}
|
|
28
|
+
context = getContext();
|
|
29
|
+
manager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
|
|
30
|
+
channel = manager.initialize(context, context.getMainLooper(), null);
|
|
57
31
|
}
|
|
58
32
|
|
|
59
|
-
|
|
60
|
-
|
|
33
|
+
@PluginMethod
|
|
34
|
+
public void scanWifiPeers(PluginCall call) {
|
|
35
|
+
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
|
61
36
|
@Override
|
|
62
37
|
public void onSuccess() {
|
|
63
|
-
|
|
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
|
+
});
|
|
64
60
|
}
|
|
61
|
+
|
|
65
62
|
@Override
|
|
66
63
|
public void onFailure(int reason) {
|
|
67
|
-
|
|
64
|
+
call.reject("Peer discovery failed: " + reason);
|
|
68
65
|
}
|
|
69
66
|
});
|
|
70
67
|
}
|
|
71
68
|
|
|
72
69
|
@PluginMethod
|
|
73
|
-
public void
|
|
74
|
-
|
|
70
|
+
public void connectToDevice(PluginCall call) {
|
|
71
|
+
String deviceAddress = call.getString("deviceAddress");
|
|
72
|
+
String pin = call.getString("pin");
|
|
75
73
|
|
|
76
|
-
if
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Intent panelIntent = new Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
|
|
80
|
-
panelIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
81
|
-
getContext().startActivity(panelIntent);
|
|
82
|
-
|
|
83
|
-
call.reject("WiFi is disabled. Please enable WiFi first.");
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
74
|
+
if(deviceAddress == null) {
|
|
75
|
+
call.reject("Device address is required");
|
|
76
|
+
return;
|
|
86
77
|
}
|
|
87
|
-
|
|
88
|
-
unregisterExistingReceiver();
|
|
89
|
-
|
|
90
|
-
cancelExistingConnection(() -> {
|
|
91
|
-
receiver = new BroadcastReceiver() {
|
|
92
|
-
@Override
|
|
93
|
-
public void onReceive(Context context, android.content.Intent intent) {
|
|
94
|
-
String action = intent.getAction();
|
|
95
|
-
if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
|
|
96
|
-
manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
|
|
97
|
-
@Override
|
|
98
|
-
public void onPeersAvailable(WifiP2pDeviceList peerList) {
|
|
99
|
-
WifiP2pDevice targetDevice = null;
|
|
100
|
-
for (WifiP2pDevice device : peerList.getDeviceList()) {
|
|
101
|
-
if (device.deviceName != null && device.deviceName.startsWith("DIRECT-LINKVUE")) {
|
|
102
|
-
targetDevice = device;
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (targetDevice != null) {
|
|
107
|
-
connectToDevice(targetDevice, call);
|
|
108
|
-
Logger.info("Found and connecting to device: " + targetDevice.deviceName);
|
|
109
|
-
} else {
|
|
110
|
-
call.reject("No Direct-LINKVUE device found");
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
getContext().registerReceiver(receiver, intentFilter);
|
|
120
|
-
|
|
121
|
-
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
|
122
|
-
@Override
|
|
123
|
-
public void onSuccess() {
|
|
124
|
-
Logger.info("Peer discovery started");
|
|
125
|
-
JSObject ret = new JSObject();
|
|
126
|
-
ret.put("message", "Discovery started");
|
|
127
|
-
call.resolve(ret);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
@Override
|
|
131
|
-
public void onFailure(int reason) {
|
|
132
|
-
call.reject("Peer discovery failed, reason: " + reason);
|
|
133
|
-
getContext().unregisterReceiver(receiver);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
private void connectToDevice(WifiP2pDevice device, PluginCall call) {
|
|
139
78
|
WifiP2pConfig config = new WifiP2pConfig();
|
|
140
|
-
config.deviceAddress =
|
|
79
|
+
config.deviceAddress = deviceAddress;
|
|
141
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
|
+
}
|
|
142
85
|
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
|
|
143
86
|
@Override
|
|
144
87
|
public void onSuccess() {
|
|
145
|
-
|
|
146
|
-
ret.put("message", "Connecting to " + device.deviceName);
|
|
147
|
-
call.resolve(ret);
|
|
148
|
-
getContext().unregisterReceiver(receiver); // 연결 시도 후 리시버 해제
|
|
88
|
+
call.resolve();
|
|
149
89
|
}
|
|
150
90
|
|
|
151
91
|
@Override
|
|
152
92
|
public void onFailure(int reason) {
|
|
153
|
-
call.reject("Connection failed
|
|
154
|
-
getContext().unregisterReceiver(receiver);
|
|
93
|
+
call.reject("Connection failed: " + reason);
|
|
155
94
|
}
|
|
156
95
|
});
|
|
157
96
|
}
|
|
97
|
+
|
|
98
|
+
|
|
158
99
|
}
|