gpt4 book ai didi

java - 如果按下连接按钮,如何创 build 备客户端或发送者

转载 作者:行者123 更新时间:2023-12-02 01:20:52 25 4
gpt4 key购买 nike

我正在使用 wifip2p manager 将文件从一台设备发送到另一台设备。始终同一设备成为客户端,或者换句话说,其他设备始终保持组所有者状态,并始终接收数据

我尝试了下面的代码来发送和接收数据

void onConnectionInfoAvailable(WifiP2pInfo info) {

this.info = info;
this.getView().setVisibility(View.VISIBLE);

// The owner IP is now known.
TextView view = (TextView) mContentView.findViewById(R.id.group_owner);
view.setText(getResources().getString(R.string.group_owner_text) + ((info.isGroupOwner == true) ? getResources().getString(R.string.yes) : getResources().getString(R.string.no)));

// InetAddress from WifiP2pInfo struct.
view = (TextView) mContentView.findViewById(R.id.device_info);
view.setText("Group Owner IP - " + info.groupOwnerAddress.getHostAddress());

// After the group negotiation, we assign the group owner as the file
// server. The file server is single threaded, single connection server
// socket.
if (info.groupFormed && info.isGroupOwner) {
new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text)).execute();
} else if (info.groupFormed) {
// The other device acts as the client. In this case, we enable the
// get file button.
mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources().getString(R.string.client_text));
}
// hide the connect button
mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);
}

最佳答案

这些是您需要检查如何实现解决方案的资源:

https://android.googlesource.com/platform/frameworks/base/+/969c243/docs/html/training/connect-devices-wirelessly/wifi-direct.jd

https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo

请记住:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

(需要许可才能找到附近的同行)

https://developer.android.com/reference/android/net/wifi/WpsInfo.html#PBC

WpsInfo.PBC仅在 api 28 之后被弃用,因此您需要将其设置为 WifiP2pConfig 实例才能连接到 channel

关于java - 如果按下连接按钮,如何创 build 备客户端或发送者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57626653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com