如何在Android中自动接受Wi-Fi Direct连接请求

时间:2012-05-11 03:07:50

标签: android wifi-direct wifip2p

我有2台使用WiFi Direct的Android设备。在一台设备上,我可以使用WifiP2pManager类获取有关其他设备的信息,并请求连接到另一台设备。但是,当我请求连接时,另一个设备弹出一个小窗口并询问用户是否要接受连接请求。

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

4 个答案:

答案 0 :(得分:5)

可以在Xposed framework的帮助下轻松完成。你只需要替换一个android java类中的单个方法(参见snihalani的答案)。但是当然要使用Xposed,你的设备必须扎根。主要思想可以用以下代码表示(使用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);
    }
}

我在SGS4 stock 4.2.2 ROM上测试了它并且它有效。 我想在Substrate for android。

的帮助下也可以这样做

答案 1 :(得分:3)

根据我目前对API的理解,如果没有用户干预,您无法真正接受连接。您可以启动连接,不需要用户干预。如果您的两个设备都是移动设备,则必须在一端接受连接请求。

我已将此作为功能请求放在android项目托管中。 您可以在此处监控他们的回复:https://code.google.com/p/android/issues/detail?id=30880

答案 2 :(得分:2)

根据评论,如果您只想跟踪和记录身边的车辆,是否真的需要连接设备?

我不知道项目的范围,但您可以简单地使用WifiP2pDeviceList时获得的request the peers in the WifiP2pManager。你可以得到你周围的设备列表(〜=车辆)并记录下来。

如果您想发送更详细的信息,连接很有用。

答案 3 :(得分:1)

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

基于Android 5.0,“frameworks / opt / net / wifi / service / java / com / android / server / wifi / p2p / WifiP2pServiceImpl.java”。

您必须找到“notifyInvitationReceived”,并修改为...

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