wifidirectplugin 0.0.6 → 0.0.8
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,10 @@ 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
|
+
|
|
31
35
|
@Override
|
|
32
36
|
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
37
|
Logger.info("WifiDirectPlugin loaded");
|
|
40
38
|
manager = (WifiP2pManager) getContext().getSystemService(Context.WIFI_P2P_SERVICE);
|
|
41
39
|
channel = manager.initialize(getContext(), getActivity().getMainLooper(), null);
|
|
@@ -48,6 +46,29 @@ public class WifiDirectPlugin extends Plugin {
|
|
|
48
46
|
|
|
49
47
|
}
|
|
50
48
|
|
|
49
|
+
private void unregisterExistingReceiver() {
|
|
50
|
+
if (receiver != null) {
|
|
51
|
+
try {
|
|
52
|
+
getContext().unregisterReceiver(receiver);
|
|
53
|
+
} catch (IllegalArgumentException ignored) {
|
|
54
|
+
}
|
|
55
|
+
receiver = null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private void cancelExistingConnection(Runnable onComplete) {
|
|
60
|
+
manager.cancelConnect(channel, new WifiP2pManager.ActionListener() {
|
|
61
|
+
@Override
|
|
62
|
+
public void onSuccess() {
|
|
63
|
+
onComplete.run();
|
|
64
|
+
}
|
|
65
|
+
@Override
|
|
66
|
+
public void onFailure(int reason) {
|
|
67
|
+
onComplete.run();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
51
72
|
@PluginMethod
|
|
52
73
|
public void discoverAndConnect(PluginCall call) {
|
|
53
74
|
WifiManager wifiManager = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
@@ -64,34 +85,54 @@ public class WifiDirectPlugin extends Plugin {
|
|
|
64
85
|
}
|
|
65
86
|
}
|
|
66
87
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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");
|
|
80
111
|
}
|
|
81
112
|
}
|
|
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
|
-
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
90
115
|
}
|
|
91
|
-
}
|
|
92
|
-
};
|
|
116
|
+
};
|
|
117
|
+
});
|
|
93
118
|
|
|
94
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
|
+
});
|
|
95
136
|
}
|
|
96
137
|
|
|
97
138
|
private void connectToDevice(WifiP2pDevice device, PluginCall call) {
|