Android Open Source - WFDAndroid Wi Fi Direct Broadcast Receiver






From Project

Back to project page WFDAndroid.

License

The source code is released under:

Apache License

If you think the Android project WFDAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011 The Android Open Source Project
 */* w w  w  .ja  va  2s. c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.flyingbuffalo.wfdmanager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;

/**
 * A BroadcastReceiver that notifies of important wifi p2p events.
 */
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

    private WFDManager manager;

    /**
     * @param manager WifiP2pManager system service
     */
    public WiFiDirectBroadcastReceiver(WFDManager m) {
        super();
        this.manager = m;
    }

    /*
     * (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
     * android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        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
                manager.setIsWifiP2pEnabled(true);
            } else {
                manager.setIsWifiP2pEnabled(false);
              // TODO - data reset
                manager.wfdDiscoveredListener.onDevicesDiscoverFailed(WFDManager.DEVICES_RESET);                
            }
            //Log.d(Activity.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) {
                manager.requestPeers();
            }
            //Log.d(Activity.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();
            } else {
                // It's a disconnect
              // TODO - data reset
              manager.wfdDiscoveredListener.onDevicesDiscoverFailed(WFDManager.DEVICES_RESET);
            }
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            
          WFDDevice d = new WFDDevice((WifiP2pDevice) intent.getParcelableExtra(
                    WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
            manager.wfdConnectedListener.onDeviceConnectFailed(WFDManager.UPDATE_THIS_DEVICE);
            manager.mydevice = d;
        }
    }
    
    
}




Java Source Code List

com.example.wfdmanagersample.MainActivity.java
com.flyingbuffalo.wfdmanager.WFDDevice.java
com.flyingbuffalo.wfdmanager.WFDManager.java
com.flyingbuffalo.wfdmanager.WFDPairInfo.java
com.flyingbuffalo.wfdmanager.WiFiDirectBroadcastReceiver.java