Android重命名设备的名称为wifi-direct

exc*_*ion 6 android wifi-direct

我正在尝试使用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");
                    }
                });
Run Code Online (Sandbox Code Playgroud)

通过以下代码连接到特定对等体:

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) {

        }
    });
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何更改Wi-Fi Direct的设备名称?

Sil*_*a H 6

即使我不建议使用反射来访问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();
    }

}
Run Code Online (Sandbox Code Playgroud)

  • Manager是WifiP2pManager,渠道是WifiP2pManager.Channel,我们使用它将应用程序连接到Wifi p2p框架。顺便说一句,如果您试图了解有关WifiP2p的基础知识,或者甚至阅读了有关如何开始的单个教程,那么您将知道什么是经理和渠道,请参考:http://developer.android.com/training/ connect-devices-wirelessly / wifi-direct.html (4认同)