我正在尝试使用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,这对我还是有用的。

public 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();
    }

}

关于android - Android重命名wifi-direct的设备名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27315198/

10-09 01:43