如何使用WiFi Direct API测试应用程序

时间:2014-02-28 19:04:54

标签: wifi-direct

我正在尝试使用Wi-Fi Direct API创建应用程序。我正在使用2类的代码:

1- WifiDirect课程:


package com.example.wifi_direct;

import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.view.Menu;

public class WifiDirect extends Activity {


    WifiP2pManager mManager;
    Channel mChannel;
    BroadcastReceiver mReceiver;

    IntentFilter mIntentFilter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //To register the BroadastReceiver
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel =  (Channel) mManager.initialize(this, getMainLooper(), null); //It was necessary to make a cast (Channel)
        mReceiver = new WiFiBroadcastReceiver(mManager, mChannel, this, this);


        //To define the filter in the BroadcastReceiver
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //


    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, mIntentFilter);
    }

   // unregister the broadcast receiver
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

}

2 - 类WiFiBroadcastReceiver:


package com.example.wifi_direct;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.widget.Toast;



/**
 * A BroadcastReceiver that notifies of important Wi-Fi p2p events.
 */

public class WiFiBroadcastReceiver extends BroadcastReceiver {

    private WifiP2pManager manager;
    private Channel channel;
    private WifiDirect activity;
    //For toast, add also context
    private Context context;

    public WiFiBroadcastReceiver(WifiP2pManager manager, Channel channel, WifiDirect activity, Context context) {
        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
        this.context= context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();


        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

            // Check to see if Wi-Fi is enabled and notify appropriate activity
             int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
             if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {

                Toast.makeText(context, "Wi-Fi Direct is enable", Toast.LENGTH_LONG).show();
                System.out.print("ddddddddddddddd");

             } else {
                 System.out.print("ddddvvvvvvvdddd");
                Toast.makeText(context, "Wi-Fi Direct is not enable", Toast.LENGTH_LONG).show();
             }      

        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing
        }
    }
}

我是初学者使用这个API,我想知道如何测试Wi-Fi是否启用..(我应该在我的活动中做什么,看看是否有效),谢谢你的普罗旺斯

2 个答案:

答案 0 :(得分:0)

您可以通过此方法检查wifi是否开启

WifiManager wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);

if(wifi.isWifiEnabled()){

//启用wifi

}

并且不要忘记获得许可

uses-permission android:name =“android.permission.ACCESS_WIFI_STATE”

答案 1 :(得分:0)

您的代码不完整。因此,选项是按照以下步骤制作您自己的完整代码:

  1. Android Sdk Manager
  2. 下载 Android 4.2.2(API 17)(如果已安装)
  3. 的示例
  4. 转到C:\ android-sdk-windows \ android-sdk-windows \ samples \ android-17 \ WiFiDirectDemo或你的sdk  位于
  5. 现在,通过在eclipse中逐个复制所有文件来制作演示。 您无法直接导入此代码,因为它不是功能代码,因为它不包含 project.properties

    对新手来说一切顺利......