11

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 to the other device. However when I request a connection, the other device pops up a little window and asks the user if they want to accept the connection request.

Is it possible to auto-accept these connection requests? I.E to be able to connect to the other device without user confirmation?

4

4 に答える 4

5

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.

于 2013-09-05T06:57:15.357 に答える
3

APIについての私の現在の理解から、ユーザーの介入なしに接続を自動的に受け入れることはできません。ユーザーの介入を必要としない接続を開始できます。両方のデバイスがモバイルデバイスである場合は、一方の端で接続要求を受け入れる必要があります。

私はこれをAndroidプロジェクトホスティングの機能リクエストとして入れました。あなたはここで彼らの反応を監視することができます:https ://code.google.com/p/android/issues/detail?id = 30880

于 2012-05-12T11:13:55.890 に答える
2

Based on the comments, do you really need to connect to the devices if you just want to track and log the vehicles around you ?

I don't know the scope of the project, but you could simply use the WifiP2pDeviceList that you get when you request the peers in the WifiP2pManager. You could get the list of the devices (~= vehicles) around you and could log this.

Connection is useful if you want to send more detailed information I guess.

于 2012-05-11T08:32:06.217 に答える
2

If you can modify the framework, you can ignore the accept window and direct send the "PEER_CONNECTION_USER_ACCEPT".

Base on Android 5.0, "frameworks/opt/net/wifi/service/java/com/android/server/wifi/p2p/WifiP2pServiceImpl.java".

You must find the "notifyInvitationReceived", and modify to ...

private void notifyInvitationReceived() {
    /*Direct sends the accept message.*/
    sendMessage(PEER_CONNECTION_USER_ACCEPT);
/*
... old code
*/
}
于 2016-05-13T10:36:32.877 に答える