Android rename device's name for wifi-direct

前端 未结 1 760
死守一世寂寞
死守一世寂寞 2021-01-05 15:25

I am trying to connect two devices using Wifi Direct, but i want to implement programmatically not by user initiated.

And for that i have to change Device\'s WifiDi

1条回答
  •  花落未央
    2021-01-05 15:46

    this is what worked for me, even though I don't recommend using reflection to access hidden APIs in the WifiP2pManager.

    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();
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题