Search code examples
androidandroid-wifi

WiFi setDeviceName can only store 22 chars?


So here is my question, I succeeded in changing device name but I noticed it can only store 22 chars. Is that accurate or have I made a mistake? If yes can I increase that size by some means or it is set? I am retrieving devices using a PeerListListener.

edit:

Actually I think I just noticed my mistake, I set the device name after I find peers here is the code:

public void onPeersAvailable(WifiP2pDeviceList peers) {

            try {
                Method method = p2p.getClass().getMethod("setDeviceName",
                        WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);

                method.invoke(p2p, channel, var, new WifiP2pManager.ActionListener() {
                    public void onSuccess() {
                        debug_print(var);
                    }

                    public void onFailure(int reason) {
                        debug_print(var);
                    }
                });
            } catch (Exception e)   {}

Solution

  • Here's the source for the method I guess you're calling (line 1305 of this source file):

    /**
     * Set p2p device name.
     * @hide
     * @param c is the channel created at {@link #initialize}
     * @param listener for callback when group info is available. Can be null.
     */
    public void setDeviceName(Channel c, String devName, ActionListener listener) {
        checkChannel(c);
        WifiP2pDevice d = new WifiP2pDevice();
        d.deviceName = devName;
        c.mAsyncChannel.sendMessage(SET_DEVICE_NAME, 0, c.putListener(listener), d);
    }
    

    deviceName is just a public String in WifiP2pDevice, so I don't see that there is any length limit applied there. Similarly, if we look at the alternative constructor for WifiP2pDevice (line 171), which takes a single String argument and splits it using a matcher pattern, we see that the regex for the device name also does not imply a length limit:

    /** Detailed device string pattern with WFD info
     * Example:
     *  P2P-DEVICE-FOUND 00:18:6b:de:a3:6e p2p_dev_addr=00:18:6b:de:a3:6e
     *  pri_dev_type=1-0050F204-1 name='DWD-300-DEA36E' config_methods=0x188
     *  dev_capab=0x21 group_capab=0x9
     */
    private static final Pattern detailedDevicePattern = Pattern.compile(
        "((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) " +
        "(\\d+ )?" +
        "p2p_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) " +
        "pri_dev_type=(\\d+-[0-9a-fA-F]+-\\d+) " +
        "name='(.*)' " + // notice no length limit here
        "config_methods=(0x[0-9a-fA-F]+) " +
        "dev_capab=(0x[0-9a-fA-F]+) " +
        "group_capab=(0x[0-9a-fA-F]+)" +
        "( wfd_dev_info=0x([0-9a-fA-F]{12}))?"
    );
    

    I also don't see any truncation taking place when devices are provided to an onPeersAvailable callback (source code).