use*_*345 9 android device-name wifi-direct wifip2p
是否有可能通过代码直接更改WiFi的设备名称?我试过:
private WifiP2pDevice wDevice;
wDevice.deviceName = "newName";
但是,显然它不起作用.任何的想法?!
小智 10
下面的代码使用Java的Reflection api,由于缺乏效率而不太喜欢,但是Android没有提供另一种方式,所以你可以使用它的魅力:
    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();
    }
这段代码适合我.
WifiP2pManager manager;
WifiP2pManager.Channel channel;
    try {
       manager = (WifiP2pManager)getSystemService(Context.WIFI_P2P_SERVICE);
        channel = manager.initialize(this, getMainLooper(), new WifiP2pManager.ChannelListener() {
            @Override
            public void onChannelDisconnected() {
                manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
            }
        });
        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() {
                Log.d("setDeviceName succeeded", "true");
            }
            @Override
            public void onFailure(int reason) {
                Log.d("setDeviceName failed", "true");
            }
        };
        setDeviceName.invoke(manager, arglist);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
| 归档时间: | 
 | 
| 查看次数: | 4043 次 | 
| 最近记录: |