更改android中的Wifi Direct名称

时间:2014-12-30 07:23:40

标签: android device wifi-direct

我想在Wifi直接更改设备名称....到目前为止,我已经尝试了

 try {
    Method m = wpm.getClass().getMethod(
            "setDeviceName",
            new Class[] { WifiP2pManager.Channel.class, String.class,
                    WifiP2pManager.ActionListener.class });

    m.invoke(WifiP2pManager wifimngr,WifiP2pManager.Channel wifichannel, new_name, new WifiP2pManager.ActionListener() {
        public void onSuccess() {
            //Code for Success in changing name
        }

        public void onFailure(int reason) {
            //Code to be done while name change Fails
        }
    });
} catch (Exception e) {

    e.printStackTrace();
}

但它对我不起作用.....有什么想法我怎么能实现这个目标?

2 个答案:

答案 0 :(得分:1)

看看https://github.com/spawrks/WifiDirectNameChanger我已经在wiko darkfull上做过测试了

答案 1 :(得分:0)

更改WifiDirect名称-Android Java

这是对我有用的方法,无论如何,我不建议您使用反射来访问WifiP2pManager中的隐藏API。

可用的Android操作系统:5.1.1

public void setDeviceName(String devName) {
    try {
        Class[] paramTypes = new Class[3];
        paramTypes[0] = WifiP2pManager.Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = WifiP2pManager.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 WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                updateLog("setDeviceName: onSuccess");
            }

            @Override
            public void onFailure(int reason) {
                updateLog("setDeviceName: onSuccess");
            }
        };
        setDeviceName.invoke(manager, arglist);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

引用:https://gist.github.com/aslamanver/39aa668b3073bc4f52aeb09b5a7ea3be