wifidirectplugin 1.3.4 → 1.3.6

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,5 +1,6 @@
1
1
  package com.ht.plugins.wifidirect;
2
2
 
3
+ import android.Manifest;
3
4
  import android.content.BroadcastReceiver;
4
5
  import android.content.Context;
5
6
  import android.content.Intent;
@@ -8,16 +9,24 @@ import android.net.ConnectivityManager;
8
9
  import android.net.Network;
9
10
  import android.net.NetworkCapabilities;
10
11
  import android.net.NetworkRequest;
12
+ import android.bluetooth.BluetoothAdapter;
13
+ import android.net.wifi.WifiManager;
11
14
  import android.net.wifi.WpsInfo;
12
15
  import android.net.wifi.p2p.*;
13
16
  import android.net.NetworkInfo;
14
17
  import android.os.Build;
15
18
  import android.os.Handler;
16
19
  import android.os.Looper;
20
+ import android.provider.Settings;
17
21
  import android.util.Log;
18
22
 
23
+ import androidx.activity.result.ActivityResult;
24
+
19
25
  import com.getcapacitor.*;
26
+ import com.getcapacitor.annotation.ActivityCallback;
20
27
  import com.getcapacitor.annotation.CapacitorPlugin;
28
+ import com.getcapacitor.annotation.Permission;
29
+ import com.getcapacitor.annotation.PermissionCallback;
21
30
 
22
31
  import org.json.JSONArray;
23
32
  import org.json.JSONException;
@@ -25,7 +34,12 @@ import org.json.JSONObject;
25
34
 
26
35
  import java.util.*;
27
36
 
28
- @CapacitorPlugin(name = "WifiDirect")
37
+ @CapacitorPlugin(
38
+ name = "WifiDirect",
39
+ permissions = {
40
+ @Permission(alias = "bluetooth", strings = { Manifest.permission.BLUETOOTH_CONNECT })
41
+ }
42
+ )
29
43
  public class WifiDirectPlugin extends Plugin {
30
44
  private WifiP2pManager manager;
31
45
  private WifiP2pManager.Channel channel;
@@ -47,6 +61,10 @@ public class WifiDirectPlugin extends Plugin {
47
61
  private ConnectivityManager.NetworkCallback p2pNetworkCallback = null;
48
62
  private Network boundP2pNetwork = null;
49
63
 
64
+ private void showToast(String message) {
65
+ Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
66
+ }
67
+
50
68
  private final BroadcastReceiver wifiP2pReceiver = new BroadcastReceiver() {
51
69
  @Override
52
70
  public void onReceive(Context context, Intent intent) {
@@ -531,5 +549,74 @@ public class WifiDirectPlugin extends Plugin {
531
549
  });
532
550
  }
533
551
 
552
+ // ── Wi-Fi / Bluetooth 활성화 보장 ────────────────────────────────────────
553
+ // 페이지 진입 시 사용자 기기의 Wi-Fi / Bluetooth 가 꺼져있으면
554
+ // 1) Wi-Fi 패널 → 2) Bluetooth enable intent 를 순서대로 띄우고,
555
+ // 최종 상태(둘 다의 enable 여부)를 한 번에 resolve 한다.
556
+ // Android 10+ 에서는 앱이 직접 Wi-Fi 를 켤 수 없어 시스템 패널을 통한 사용자 동의 필요.
557
+ @PluginMethod
558
+ public void ensureWifiAndBluetooth(PluginCall call) {
559
+ WifiManager wifiManager = (WifiManager) context
560
+ .getApplicationContext()
561
+ .getSystemService(Context.WIFI_SERVICE);
562
+ if (wifiManager != null && !wifiManager.isWifiEnabled()) {
563
+ showToast("Turn On the Wifi.");
564
+ Intent panelIntent = new Intent(Settings.Panel.ACTION_WIFI);
565
+ startActivityForResult(call, panelIntent, "wifiPanelResult");
566
+ return;
567
+ }
568
+ ensureBluetoothStep(call);
569
+ }
570
+
571
+ @ActivityCallback
572
+ private void wifiPanelResult(PluginCall call, ActivityResult result) {
573
+ if (call == null) return;
574
+ ensureBluetoothStep(call);
575
+ }
576
+
577
+ private void ensureBluetoothStep(PluginCall call) {
578
+ BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
579
+ if (btAdapter != null && !btAdapter.isEnabled()) {
580
+ // Android 12+(API 31)에서는 ACTION_REQUEST_ENABLE 인텐트를 띄우려면
581
+ // BLUETOOTH_CONNECT 런타임 권한이 필요하다. 미승인 상태로 띄우면 SecurityException.
582
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
583
+ && getPermissionState("bluetooth") != PermissionState.GRANTED) {
584
+ requestPermissionForAlias("bluetooth", call, "bluetoothPermCallback");
585
+ return;
586
+ }
587
+ showToast("Turn On the Bluetooth.");
588
+ Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
589
+ startActivityForResult(call, btIntent, "bluetoothEnableResult");
590
+ return;
591
+ }
592
+ resolveStatus(call);
593
+ }
534
594
 
595
+ @PermissionCallback
596
+ private void bluetoothPermCallback(PluginCall call) {
597
+ if (getPermissionState("bluetooth") == PermissionState.GRANTED) {
598
+ // 권한 승인됨 → 다시 시도하면 이번엔 enable 인텐트를 띄울 수 있다.
599
+ ensureBluetoothStep(call);
600
+ } else {
601
+ // 권한 거부 시 크래시 대신 현재 상태만 반환한다.
602
+ resolveStatus(call);
603
+ }
604
+ }
605
+
606
+ @ActivityCallback
607
+ private void bluetoothEnableResult(PluginCall call, ActivityResult result) {
608
+ if (call == null) return;
609
+ resolveStatus(call);
610
+ }
611
+
612
+ private void resolveStatus(PluginCall call) {
613
+ WifiManager wifiManager = (WifiManager) context
614
+ .getApplicationContext()
615
+ .getSystemService(Context.WIFI_SERVICE);
616
+ BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
617
+ JSObject ret = new JSObject();
618
+ ret.put("wifiEnabled", wifiManager != null && wifiManager.isWifiEnabled());
619
+ ret.put("bluetoothEnabled", btAdapter != null && btAdapter.isEnabled());
620
+ call.resolve(ret);
621
+ }
535
622
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wifidirectplugin",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
4
4
  "description": "HT-Installer Wifi Direct Plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",