質問

I'm trying to create an application using the Wi-Fi Direct API. i 'am using the code bellow with 2 class :

1- the class 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 - the class 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
        }
    }
}

i'am beginner in using this API, and i want to know how to test if Wi-Fi is enabled or no..( what should i do in my Activity to see if that works or no ) , thanks in advence.

役に立ちましたか?

解決

You can check if wifi is on or not by this method

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

if (wifi.isWifiEnabled()){

//wifi is enabled

}

and dont forgot to have permission

uses-permission android:name="android.permission.ACCESS_WIFI_STATE"

他のヒント

Your code is incomplete. So, the option is to follow these steps to make your own complete code:

  1. Android Sdk Manager
  2. Download Samples for Android 4.2.2 (API 17) (if already installed)
  3. Go to C:\android-sdk-windows\android-sdk-windows\samples\android-17\WiFiDirectDemo or where your sdk is located

Now, make your demo by copying all the files one by one in eclipse. You can not import this code directly because this is not functional code as it doesn't contain project.properties.

All the best to newbies....

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top