如何自动接受Android中的Wi-Fi直连请求

时间:2022-10-06 20:07:22

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.

我有两个使用WiFi的Android设备。在一个设备上,我可以使用WifiP2pManager类获得关于另一个设备的信息,并请求到另一个设备的连接。然而,当我请求连接时,另一个设备弹出一个小窗口,询问用户是否愿意接受连接请求。

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

是否可以自动接受这些连接请求?我。E能够在没有用户确认的情况下连接到其他设备?

4 个解决方案

#1


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.

我不知道这个项目的范围,但是您可以使用WifiP2pDeviceList,这是您在请求WifiP2pManager中的对等节点时获得的。你可以得到你周围的设备(~=车辆)的列表,并记录下来。

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

如果你想发送更详细的信息,我猜连接是有用的。

#2


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)

借助xpose框架可以很容易地完成。您只需要在android java类中替换单个方法(参见snihalani的答案链接)。但是当然,要使用xpose,你的设备必须是根的。主要思想可以用以下代码表示(使用xpose)

@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.

我在SGS4库存4.2.2 ROM上测试了它,它成功了。我想同样的事情也可以通过android的承载量来实现。

#3


3  

From my current understanding of the API, You cannot really accept connections automatically without user's intervention. You can initiate a connection, that doesn't require user intervention. If both of your devices are mobile devices, you will have to accept connection request on one end.

根据我目前对API的理解,如果没有用户的干预,您不能真正地自动接受连接。您可以启动一个连接,不需要用户干预。如果您的两个设备都是移动设备,那么您将不得不在一端接受连接请求。

I have put this as a feature request in android project hosting. You can monitor their response here: https://code.google.com/p/android/issues/detail?id=30880

我把它作为android项目托管的一个功能请求。您可以在这里监视他们的响应:https://code.google.com/p/android/issues/detail?id=30880

#4


1  

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

如果可以修改框架,可以忽略accept窗口,直接发送“PEER_CONNECTION_USER_ACCEPT”。

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

在Android 5.0基础上,“框架/opt/ net/wifi/service/java/service/server/wifi/p2p/wifip2pserviceimpl.java”。

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

您必须找到“notify邀请信”,并修改为…

private void notifyInvitationReceived() {
    /*Direct sends the accept message.*/
    sendMessage(PEER_CONNECTION_USER_ACCEPT);
/*
... old code
*/
}

#1


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.

我不知道这个项目的范围,但是您可以使用WifiP2pDeviceList,这是您在请求WifiP2pManager中的对等节点时获得的。你可以得到你周围的设备(~=车辆)的列表,并记录下来。

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

如果你想发送更详细的信息,我猜连接是有用的。

#2


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)

借助xpose框架可以很容易地完成。您只需要在android java类中替换单个方法(参见snihalani的答案链接)。但是当然,要使用xpose,你的设备必须是根的。主要思想可以用以下代码表示(使用xpose)

@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.

我在SGS4库存4.2.2 ROM上测试了它,它成功了。我想同样的事情也可以通过android的承载量来实现。

#3


3  

From my current understanding of the API, You cannot really accept connections automatically without user's intervention. You can initiate a connection, that doesn't require user intervention. If both of your devices are mobile devices, you will have to accept connection request on one end.

根据我目前对API的理解,如果没有用户的干预,您不能真正地自动接受连接。您可以启动一个连接,不需要用户干预。如果您的两个设备都是移动设备,那么您将不得不在一端接受连接请求。

I have put this as a feature request in android project hosting. You can monitor their response here: https://code.google.com/p/android/issues/detail?id=30880

我把它作为android项目托管的一个功能请求。您可以在这里监视他们的响应:https://code.google.com/p/android/issues/detail?id=30880

#4


1  

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

如果可以修改框架,可以忽略accept窗口,直接发送“PEER_CONNECTION_USER_ACCEPT”。

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

在Android 5.0基础上,“框架/opt/ net/wifi/service/java/service/server/wifi/p2p/wifip2pserviceimpl.java”。

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

您必须找到“notify邀请信”,并修改为…

private void notifyInvitationReceived() {
    /*Direct sends the accept message.*/
    sendMessage(PEER_CONNECTION_USER_ACCEPT);
/*
... old code
*/
}