我正在尝试使用Wifi Direct连接两个设备,但我想以编程方式实现,而不是由用户启动.
为此,我必须更改设备的WifiDirect的名称,如下图所示:
现在使用以下方法发现对等体:
wifiP2pManager.discoverPeers(channel,new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG,"onSuccess");
}
@Override
public void onFailure(int reason) {
Log.d(TAG,"onFailure");
}
});
通过以下代码连接到特定对等体:
public static void connectPeer(WifiP2pDevice device,WifiP2pManager manager,Channel channel,final Handler handler) {
WifiP2pConfig config = new WifiP2pConfig();
config.groupOwnerIntent = 15;
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
manager.connect(channel,config,new ActionListener() {
@Override
public void onSuccess() {
}
@Override
public void onFailure(int reason) {
}
});
}
但我不知道如何更改Wi-Fi Direct的设备名称?
最佳答案
这对我有用,即使我不建议使用反射来访问WifiP2pManager中的隐藏API.
原文链接:https://www.f2er.com/android/431179.htmlpublic void setDeviceName(String devName) {
try {
Class[] paramTypes = new Class[3];
paramTypes[0] = Channel.class;
paramTypes[1] = String.class;
paramTypes[2] = ActionListener.class;
Method setDeviceName = manager.getClass().getMethod(
"setDeviceName",paramTypes);
setDeviceName.setAccessible(true);
Object arglist[] = new Object[3];
arglist[0] = channel;
arglist[1] = devName;
arglist[2] = new ActionListener() {
@Override
public void onSuccess() {
LOG.debug("setDeviceName succeeded");
}
@Override
public void onFailure(int reason) {
LOG.debug("setDeviceName Failed");
}
};
setDeviceName.invoke(manager,arglist);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}