gpt4 book ai didi

android - Wi-Fi 直连安卓

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:13 27 4
gpt4 key购买 nike

我想通过 Wi-Fi Direct 在 2 台设备之间传输文件。

我想做与 WifiDirectDemo 中相同的事情,但我无法将数据从群组所有者传输到另一台设备,所以我尝试了这个:每次当其中一台设备点击连接时,另一台设备被设置作为群组所有者,因此在每次连接上,请求连接的设备始终是客户端并且可以发送数据。

问题是 Android 总是记住第一个创建的组,因此它的组所有者。换句话说,除非我进入设置并忘记第一次连接创建的组,否则我所做的只在第一次有效。

我知道通过使用断开连接按钮,Wi-Fi 组被删除,但 Android 系统将其放入记住的组中,并在建立新连接时使用其设置(组所有者协商)。

我尝试的第二件事是在每个设备上(在另一个端口上)创建一个 ServerSocket,这样组所有者和其他设备将同时成为客户端和服务器。我不知道是否可以将群组所有者设置为客户端,但我无法在两台设备上创建 ServerSocket。这是我的代码:

<pre>
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
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),8988)
.execute();
mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
Log.d(WiFiDirectActivity.TAG, "serveur8988cree");
} else if (info.groupFormed) {
// The other device acts as the client. In this case, we enable the
// Get file button.
// In this case we create a server socket on another port
new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text),8987)
.execute();
mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
Log.d(WiFiDirectActivity.TAG, "serveur8987cree");
((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources()
.getString(R.string.client_text));
}
</pre>

感谢您的帮助。

最佳答案

你可以通过反射删除所有组,但是,这有点 hack,类成员以后可能会改变

 private void deletePersistentInfo() {
try {

Class persistentInterface = null;

//Iterate and get class PersistentGroupInfoListener
for (Class<?> classR : WifiP2pManager.class.getDeclaredClasses()) {
if (classR.getName().contains("PersistentGroupInfoListener")) {
persistentInterface = classR;
break;
}

}

final Method deletePersistentGroupMethod = WifiP2pManager.class.getDeclaredMethod("deletePersistentGroup", new Class[]{Channel.class, int.class, ActionListener.class});




//anonymous class to implement PersistentGroupInfoListener which has a method, onPersistentGroupInfoAvailable
Object persitentInterfaceObject =
java.lang.reflect.Proxy.newProxyInstance(persistentInterface.getClassLoader(),
new java.lang.Class[]{persistentInterface},
new java.lang.reflect.InvocationHandler() {
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
String method_name = method.getName();

if (method_name.equals("onPersistentGroupInfoAvailable")) {
Class wifiP2pGroupListClass = Class.forName("android.net.wifi.p2p.WifiP2pGroupList");
Object wifiP2pGroupListObject = wifiP2pGroupListClass.cast(args[0]);

Collection<WifiP2pGroup> wifiP2pGroupList = (Collection<WifiP2pGroup>) wifiP2pGroupListClass.getMethod("getGroupList", null).invoke(wifiP2pGroupListObject, null);
for (WifiP2pGroup group : wifiP2pGroupList) {
deletePersistentGroupMethod.invoke(wifiP2pManager, channel, (Integer) WifiP2pGroup.class.getMethod("getNetworkId").invoke(group, null), new ActionListener() {
@Override
public void onSuccess() {
//All groups deleted
}

@Override
public void onFailure(int i) {

}
});
}
}

return null;
}
});

Method requestPersistentGroupMethod =
WifiP2pManager.class.getDeclaredMethod("requestPersistentGroupInfo", new Class[]{Channel.class, persistentInterface});

requestPersistentGroupMethod.invoke(wifiP2pManager, channel, persitentInterfaceObject);

} catch (Exception ex) {
ex.printStackTrace();
}
}

关于android - Wi-Fi 直连安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372724/

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