Home | History | Annotate | Download | only in discovery
      1 
      2 package com.example.android.wifidirect.discovery;
      3 
      4 /*
      5  * Copyright (C) 2011 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 import android.app.Activity;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.net.NetworkInfo;
     25 import android.net.wifi.p2p.WifiP2pDevice;
     26 import android.net.wifi.p2p.WifiP2pManager;
     27 import android.net.wifi.p2p.WifiP2pManager.Channel;
     28 import android.net.wifi.p2p.WifiP2pManager.ConnectionInfoListener;
     29 import android.util.Log;
     30 
     31 /**
     32  * A BroadcastReceiver that notifies of important wifi p2p events.
     33  */
     34 public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
     35 
     36     private WifiP2pManager manager;
     37     private Channel channel;
     38     private Activity activity;
     39 
     40     /**
     41      * @param manager WifiP2pManager system service
     42      * @param channel Wifi p2p channel
     43      * @param activity activity associated with the receiver
     44      */
     45     public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
     46             Activity activity) {
     47         super();
     48         this.manager = manager;
     49         this.channel = channel;
     50         this.activity = activity;
     51     }
     52 
     53     /*
     54      * (non-Javadoc)
     55      * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
     56      * android.content.Intent)
     57      */
     58     @Override
     59     public void onReceive(Context context, Intent intent) {
     60         String action = intent.getAction();
     61         Log.d(WiFiServiceDiscoveryActivity.TAG, action);
     62         if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
     63 
     64             if (manager == null) {
     65                 return;
     66             }
     67 
     68             NetworkInfo networkInfo = (NetworkInfo) intent
     69                     .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
     70 
     71             if (networkInfo.isConnected()) {
     72 
     73                 // we are connected with the other device, request connection
     74                 // info to find group owner IP
     75                 Log.d(WiFiServiceDiscoveryActivity.TAG,
     76                         "Connected to p2p network. Requesting network details");
     77                 manager.requestConnectionInfo(channel,
     78                         (ConnectionInfoListener) activity);
     79             } else {
     80                 // It's a disconnect
     81             }
     82         } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
     83                 .equals(action)) {
     84 
     85             WifiP2pDevice device = (WifiP2pDevice) intent
     86                     .getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
     87             Log.d(WiFiServiceDiscoveryActivity.TAG, "Device status -" + device.status);
     88 
     89         }
     90     }
     91 }
     92