Android programming realizes modifying device WiFi name

The specific code is as follows:

public void ChangeDeviceName(final String NewDeviceName){
    
    
        try {
    
    
            Log.d(TAG, "进入修改名字");
            Method method = mManager.getClass().getMethod("setDeviceName",
                    WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);

            method.invoke(mManager, mChannel, "", new WifiP2pManager.ActionListener() {
    
    
                public void onSuccess() {
    
    
                    Log.d(TAG, "Change DeviceName Success!");
                    try {
    
    
                        Log.d(TAG, "进入修改名字");
                        Method method = mManager.getClass().getMethod("setDeviceName",
                                WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);

                        method.invoke(mManager, mChannel, NewDeviceName, new WifiP2pManager.ActionListener() {
    
    
                            public void onSuccess() {
    
    
                                Log.d(TAG, "Change DeviceName Success!");
                            }

                            public void onFailure(int reason) {
    
    
                                Log.d(TAG, "Change DeviceName Failed!");
                            }
                        });
                    } catch (Exception e)   {
    
    
                        Log.d(TAG, "错误:"+e);
                    }
                }

                public void onFailure(int reason) {
    
    
                    Log.d(TAG, "Change DeviceName Failed!"+reason);
                }
            });
        } catch (Exception e)   {
    
    
            Log.d(TAG, "错误:"+e);
        }
    }

Note:
1. In theory, it is enough to call the function once, but I can only change the name of the WiFi device by nesting the function twice. The specific reason is still learning.
2. The mManager in the code is the WifiP2pManager instantiated in the main function:

mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);

3. The mChannel in the code is the Channel instantiated in the main function:

mChannel = mManager.initialize(this, getMainLooper(), null);

——————————————————————————————
Finally, post my personal public account: WeChat search "Chaqian" or scan the picture below. Usually, some programming-related articles will be updated, and everyone is welcome to pay attention~
tea move

Guess you like

Origin blog.csdn.net/weixin_46269688/article/details/110442010