wifidirectplugin 0.0.7 → 0.0.9
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.
|
@@ -11,6 +11,8 @@ import android.net.wifi.p2p.WifiP2pDeviceList;
|
|
|
11
11
|
import android.net.wifi.WifiManager;
|
|
12
12
|
import android.content.Intent;
|
|
13
13
|
import android.os.Build;
|
|
14
|
+
import android.os.Handler;
|
|
15
|
+
import android.os.Looper;
|
|
14
16
|
|
|
15
17
|
import com.getcapacitor.JSObject;
|
|
16
18
|
import com.getcapacitor.Logger;
|
|
@@ -28,14 +30,11 @@ public class WifiDirectPlugin extends Plugin {
|
|
|
28
30
|
private BroadcastReceiver receiver;
|
|
29
31
|
private IntentFilter intentFilter;
|
|
30
32
|
|
|
33
|
+
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
34
|
+
private boolean isConnecting = false;
|
|
35
|
+
|
|
31
36
|
@Override
|
|
32
37
|
public void load() {
|
|
33
|
-
// WifiP2pManager 초기화
|
|
34
|
-
// boolean isP2pSupported = getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT);
|
|
35
|
-
// if (!isP2pSupported) {
|
|
36
|
-
// Logger.error("WifiDirectPlugin WiFi P2P not supported on this device");
|
|
37
|
-
// return;
|
|
38
|
-
// }
|
|
39
38
|
Logger.info("WifiDirectPlugin loaded");
|
|
40
39
|
manager = (WifiP2pManager) getContext().getSystemService(Context.WIFI_P2P_SERVICE);
|
|
41
40
|
channel = manager.initialize(getContext(), getActivity().getMainLooper(), null);
|
|
@@ -48,6 +47,41 @@ public class WifiDirectPlugin extends Plugin {
|
|
|
48
47
|
|
|
49
48
|
}
|
|
50
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
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
51
85
|
@PluginMethod
|
|
52
86
|
public void discoverAndConnect(PluginCall call) {
|
|
53
87
|
WifiManager wifiManager = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
@@ -64,32 +98,36 @@ public class WifiDirectPlugin extends Plugin {
|
|
|
64
98
|
}
|
|
65
99
|
}
|
|
66
100
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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");
|
|
80
124
|
}
|
|
81
125
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
Logger.info("Found and connecting to device: " + targetDevice.deviceName);
|
|
85
|
-
} else {
|
|
86
|
-
call.reject("No Direct-LINKVUE device found");
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
90
128
|
}
|
|
91
|
-
}
|
|
92
|
-
};
|
|
129
|
+
};
|
|
130
|
+
});
|
|
93
131
|
|
|
94
132
|
getContext().registerReceiver(receiver, intentFilter);
|
|
95
133
|
|
|
@@ -111,6 +149,8 @@ public class WifiDirectPlugin extends Plugin {
|
|
|
111
149
|
}
|
|
112
150
|
|
|
113
151
|
private void connectToDevice(WifiP2pDevice device, PluginCall call) {
|
|
152
|
+
isConnecting = true;
|
|
153
|
+
|
|
114
154
|
WifiP2pConfig config = new WifiP2pConfig();
|
|
115
155
|
config.deviceAddress = device.deviceAddress;
|
|
116
156
|
|