如何使用wifi直接连接传输文件?

时间:2013-05-15 17:45:03

标签: android wifi file-transfer wifi-direct

我想使用wifi直接传输文件来构建应用程序,并且我使用NFC来减少配对时间。我已经按照http://developer.android.com/guide/topics/connectivity/wifip2p.htmlhttp://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html中的说明操作,但我的应用无法连接。我从android示例中获取了wifi直接演示的代码。

当我跟踪问题是当wifi直接广播接收器WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION时,网络信息不能与其他设备连接,因此,在我的实现ConnectionInfoListener的主类中有一个方法{ {1}}它永远不会被触发。

谁能帮助我吗?先于

代码就像这样

Wifi Direct BroadCast接收器 `

onConnectionInfoAvailable

我的主要课程

`

   public void onReceive(Context arg0, Intent intent) {

    // TODO Auto-generated method stub
    String action = intent.getAction();
    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
        // UI update to indicate wifi p2p status.
        int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
        if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
            // Wifi Direct mode is enabled
            activity.setIsWifiP2pEnabled(true);
        } else {
            activity.setIsWifiP2pEnabled(false);
            //activity.resetData();
        }
        //Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
        // request available peers from the wifi p2p manager. This is an
        // asynchronous call and the calling activity is notified with a
        // callback on PeerListListener.onPeersAvailable()
        if (manager != null) {

        }
        //Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

        if (manager == null) {
            return;
        }

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

        if (networkInfo.isConnected()) {
            // we are connected with the other device, request connection
            // info to find group owner IP

            manager.requestConnectionInfo(channel, activity);

        } else {
            // It's a disconnect

        }
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {

            WifiP2pDevice device = (WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
            activity.myname = device.deviceName + " " + device.deviceAddress + " " + device.primaryDeviceType + " " + device.secondaryDeviceType + " " + device.status;
    }
}`

`

onConnectionInfoAvailable将永远不会被执行,因为networkInfo.isConnected()永远不会为真。

请帮助我.. Thx ..

0 个答案:

没有答案