How to auto-accept Wi-Fi Direct connection requests in Android

前端 未结 4 717
醉话见心
醉话见心 2020-12-08 05:57

I have 2 Android devices using WiFi Direct. On one device I can get information about the other device using the WifiP2pManager class, and request a connection

4条回答
  •  萌比男神i
    2020-12-08 06:28

    It can be easily done with the help of Xposed framework. You just need to replace the single method inside one of android java classes (see the link from snihalani's answer). But of course to use Xposed your device must be rooted. The main idea can be expressed in the following code (using Xposed)

    @Override
    public void handleLoadPackage(LoadPackageParam lpparam) {
        try {
            Class wifiP2pService = Class.forName("android.net.wifi.p2p.WifiP2pService", false, lpparam.classLoader);
            for (Class c : wifiP2pService.getDeclaredClasses()) {
                //XposedBridge.log("inner class " + c.getSimpleName());
                if ("P2pStateMachine".equals(c.getSimpleName())) {
                    XposedBridge.log("Class " + c.getName() + " found");
                    Method notifyInvitationReceived = c.getDeclaredMethod("notifyInvitationReceived");
                    final Method sendMessage = c.getMethod("sendMessage", int.class);
    
                    XposedBridge.hookMethod(notifyInvitationReceived, new XC_MethodReplacement() {
                        @Override
                        protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
                            final int PEER_CONNECTION_USER_ACCEPT = 0x00023000 + 2;
                            sendMessage.invoke(param.thisObject, PEER_CONNECTION_USER_ACCEPT);
                            return null;
                        }
                    });
    
                    break;
                }
            }
        } catch (Throwable t) {
            XposedBridge.log(t);
        }
    }
    

    I tested it on SGS4 stock 4.2.2 ROM and it worked. I guess the same could be done with the help of Substrate for android.

提交回复
热议问题