Android和Linux之间的Wi-Fi Direct

时间:2016-11-16 09:55:56

标签: android c linux wifi-direct

使用Wi-Fi Direct将Linux与Android连接有点问题。

在我的Linux上:我使用:

p2p_find
p2p_peers
p2p_prov_disc <ANDROID:MAC> keypad

我收到了回复: P2P-PROV-DISC-SHOW-PIN 96:35:0a:d5:83:e8 44779443

在Android上我使用此代码:

bsend.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            System.out.println("Click Connection :");
            WifiP2pConfig config = new WifiP2pConfig();
            if (peers.size() == 0)
            {
                System.out.println("No peers found , MAC test : 00:e0:4c:61:0f:d0");
                config.deviceAddress = "00:e0:4c:61:0f:d0";
            }
            else 
            {
                WifiP2pDevice device = (WifiP2pDevice) peers.get(0);
                config.deviceAddress = device.deviceAddress;
                System.out.println("Connection to: " + config.deviceAddress);
            }
            config.wps.setup = WpsInfo.DISPLAY;
            config.wps.pin = pin.getText().toString();
            config.groupOwnerIntent = 0;
            mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    System.out.println("Connection Suceess");
                }
                @Override
                public void onFailure(int i) {
                    Toast.makeText(MainActivity.this, "Connect failed. Retry.",
                            Toast.LENGTH_SHORT).show();
                    System.out.println(i);
                    System.out.println("Connection failed");
                }
            });
        }
    });

在我的Logcat上,我得到了:

Click Connection :
code :44779443
Connection to: 00:e0:4c:61:0f:d0
2
Connection failed

我的Linux上的ifconfig:HWaddr 00:e0:4c:61:0f:d0

我是Android的新手,我不知道是否无法打印出与我的失败代码相对应的“错误”。我在类WifiP2pManager中没有看到回调函数失败连接的接口。

编辑:我也尝试了config.wps.setup = WpsInfo.KEYPAD;,我觉得它更准确。

编辑:我很蠢,工作得很好,只是我按了两次连接按钮(是的,我以为我错了某个事件管理的地方)。
所以我现在用PBC方法:

config.wps.setup = WpsInfo.PBC;
config.groupOwnerIntent = 0;

之后我创建了一个Socket:

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
        Socket socket = new Socket("192.168.0.1", 4242);
        if (socket.isConnected())
        {
            System.out.println("Connection ok");
        }
        else
            System.out.println("connection failed");
        }catch (IOException e)
        {
             System.out.println(e);
        }

    }
}).start();

但我得到了:java.net.ConnectException: failed to connect to /192.168.0.1 (port 4242): connect failed: ENETUNREACH (Network is unreachable)
为什么我的c程序基本上是我做的:

-> connection to wpa_supplicant using wpa_ctrl.c
-> PING REQUEST to wpa_supplicant, reponse : PONG
-> P2p find
-> socket, bind , listen... No error return
-> p2p peer FIRST ( i dont manage all device for now)
-> p2p_prov_disc MAC::address pbc
-> using Select for checking Connection for accept

如果我连接成功,为什么我的网络无法访问? 当我点击连接时,我进入了callack函数OnpeersAvailable而没有OnConnectionInfoAvailable ...

0 个答案:

没有答案