如何检查Wi-Fi p2p是否已连接?

发布于 2025-01-11 12:31:19 字数 1121 浏览 0 评论 0原文

在我的应用程序中,我需要检查 p2p 是否已连接。以前我使用以下代码对其进行归档,但 NetworkInfo 已弃用,所以我想以另一种方式实现它。

mWifiP2pManager.requestNetworkInfo(mChannel, new NetworkInfoListener() {
        @Override
        public void onNetworkInfoAvailable(NetworkInfo networkInfo) {
            mIsP2pConnected = networkInfo.isConnected();
        }
    });

我尝试使用以下代码来获取 p2p 的状态。但是当我通过 Wi-Fi Direct 连接其他设备时,我找不到此日志。 但是,如果我将addCapability的参数设置为NetworkCapabilities.NET_CAPABILITY_INTERNET并打开wifi,我可以找到此日志。

        mCM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkRequest networkRequest =
                new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P).build();
        mCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                Log.d("MainActivity", "onAvailable: Detect network");

            }
        };
       mCM.registerNetworkCallback(networkRequest, mCallback);

In my app, I need to check if p2p is connected. Previously I use following code to archive it, but NetworkInfo is deprecated, so I want to implement it in another way.

mWifiP2pManager.requestNetworkInfo(mChannel, new NetworkInfoListener() {
        @Override
        public void onNetworkInfoAvailable(NetworkInfo networkInfo) {
            mIsP2pConnected = networkInfo.isConnected();
        }
    });

I tried to use following code to get the status of p2p. But when I connect other device via Wi-Fi Direct, I can't find this log.
However, if I set the parameter of addCapability is NetworkCapabilities.NET_CAPABILITY_INTERNET and turn on wifi, I can find this log.

        mCM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkRequest networkRequest =
                new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P).build();
        mCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                Log.d("MainActivity", "onAvailable: Detect network");

            }
        };
       mCM.registerNetworkCallback(networkRequest, mCallback);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍风燃霜 2025-01-18 12:31:19
wifiP2pManger.requestConnectionInfo
wifiP2pManger.requestConnectionInfo
蒲公英的约定 2025-01-18 12:31:19

尝试添加 WIFI 传输类型以及添加 WIFI_P2P 功能:

...
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
NetworkRequest networkRequest = builder.build();
...

Try to add WIFI transport type along with adding WIFI_P2P capability:

...
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
NetworkRequest networkRequest = builder.build();
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文