1

我必须创建一个 WifiP2p 应用程序。该活动有效,但我在 onPeersAvailable 中没有收到回调。我认为我的广播接收器有问题(我在 Logcat 中只能看到 2 个 Log.v,但它应该包含其他 Log.v...)

这是我的广播接收器代码

import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;

public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
    private WifiP2pManager manager;
    private Channel channel;
    private Setting activity;
    private PeerListListener myPeerListListener;
    private static final String TAG = "MyActivity";

    public WifiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, Setting activity){

        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
    }

    @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) {
                  //TO BE COMPLETED 
             } else {
                //TO BE COMPLETED
             }      

        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers

            if (manager != null) {
                manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {


                @Override
                public void onPeersAvailable(WifiP2pDeviceList arg0) {
                    // TODO Auto-generated method stub
                    Log.v(TAG,"Ici!!!!!!!!!!!!!!!!!!!!!");
                    // DO WHATEVER YOU WANT HERE
                    // YOU CAN GET ACCESS TO ALL THE DEVICES YOU FOUND FROM peers OBJECT
                }
            });
            }



        } 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
        }
    }
}

这是我的活动代码(活动名为“设置”)

import java.util.ArrayList;
import java.util.Collection;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class Setting extends FragmentActivity {
    private WifiDirectBroadcastReceiver mReceiver;
    private WifiP2pManager mManager;
    private Channel mChannel;
    private IntentFilter mIntentFilter=new IntentFilter();
    private static final String TAG = "MyActivity";
     private Collection <WifiP2pDevice> peers;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        mReceiver = new WifiDirectBroadcastReceiver(mManager, mChannel, this);

        final Button btnScan = (Button)findViewById(R.id.btnScan);

        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        Log.v(TAG,"Le wifi est activé");
        registerReceiver(mReceiver, mIntentFilter);


         mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

             public void onSuccess() {
                 Log.v(TAG,"Peers trouvés");
             }
             public void onFailure(int reasonCode) {
                 Log.v(TAG,"Pas de Peers");
             }
         });    
    }

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

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

}

报告调试

    05-27 19:52:04.877: I/ActivityManager(883): START u0{act=com.antony.projetjava.SETTING cmp=com.antony.projetjava/.Setting} from pid 26622
05-27 19:52:04.887: D/alsa_ucm(322): Setting mixer control: RX3 MIX1 INP2, value: RX2
05-27 19:52:04.887: D/alsa_ucm(322): Setting mixer control: LINEOUT1 Volume, value: 100
05-27 19:52:04.887: D/alsa_ucm(322): Set mixer controls for HiFi enable 1
05-27 19:52:04.887: D/alsa_ucm(322): Setting mixer control: SLIMBUS_0_RX Audio Mixer MultiMedia1, value: 1
05-27 19:52:04.887: D/ALSADevice(322): close: handle 0xb7b92558 h 0x0
05-27 19:52:04.887: D/ALSADevice(322): open: handle 0xb7b92558, format 0x2
05-27 19:52:04.887: V/ALSADevice(322): Music case
05-27 19:52:04.887: D/ALSADevice(322): Device value returned is hw:0,0
05-27 19:52:04.887: V/ALSADevice(322): flags 0, devName hw:0,0
05-27 19:52:04.887: V/ALSADevice(322): pcm_open returned fd 52
05-27 19:52:04.887: D/ALSADevice(322): handle->format: 0x2
05-27 19:52:04.887: D/ALSADevice(322): setHardwareParams: reqBuffSize 2048 channels 2 sampleRate 48000
05-27 19:52:04.897: D/ALSADevice(322): setHardwareParams: buffer_size 16384, period_size 2048, period_cnt 8
05-27 19:52:04.957: V/MyActivity(26622): Le wifi est activé
05-27 19:52:04.987: V/MyActivity(26622): Peers trouvés
05-27 19:52:05.017: I/ActivityManager(883): Process com.sonyericsson.android.addoncamera.artfilter (pid 26847) has died.
05-27 19:52:05.027: V/ConfigFetchTask(23571): ConfigFetchTask getDeviceDataVersionInfo(): ABFEt1U5oPLeaz5n1CwCqvFpZkxG4WwfQxYxP2KRhpltKe4LJBInvGq8E0LpRLgN9_vz-WYcA_6MyGGEFaYKMwV--02nqJtaeAGQ1T8T4P7y6CcIAahCVWwRA90itazfifg03YCOUVKNuWdV2trRG6IWUI2h1tUSCwPtvAtkT4zjQQf1882yGf8mF7_irgoCR7iwIATVhiPLNm53WQXPmhGT2Mj2cE5BM0VmYV_SujciqV2JF0Bmncw
05-27 19:52:05.027: I/GoogleURLConnFactory(23571): Using platform SSLCertificateSocketFactory
05-27 19:52:05.037: I/sysmon-tsens_tz_sensor3(315): sensor_work - read value = 340
05-27 19:52:05.037: I/sysmon-tsens_tz_sensor0(315): sensor_work - read value = 350
05-27 19:52:05.067: I/ActivityManager(883): Displayed com.antony.projetjava/.Setting: +131ms
4

1 回答 1

0

您的广播接收器应该只调用 request peers 。

public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
    private WifiP2pManager manager;
    private Channel channel;
    private Setting activity;
    private PeerListListener myPeerListListener;
    private static final String TAG = "MyActivity";

    public WifiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, Setting activity){

        super(); 
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
    } 

    @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) {
                  //TO BE COMPLETED  
             } else { 
                //TO BE COMPLETED 
             }       

        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers 

            if (manager != null) {
                manager.requestPeers(channel,peerlistlistener)// here pass your activity instance as listener
            } 



        } 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 
        } 
    } 
} 

您的活动需要实现 PeerListListener

public class Setting extends FragmentActivity implements PeerListListener{
    private WifiDirectBroadcastReceiver mReceiver;
    private WifiP2pManager mManager;
    private Channel mChannel;
    private IntentFilter mIntentFilter=new IntentFilter();
    private static final String TAG = "MyActivity";
     private Collection <WifiP2pDevice> peers;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        mReceiver = new WifiDirectBroadcastReceiver(mManager, mChannel, this);

        final Button btnScan = (Button)findViewById(R.id.btnScan);

        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        Log.v(TAG,"Le wifi est activé");
        registerReceiver(mReceiver, mIntentFilter);


         mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

             public void onSuccess() { 
                 Log.v(TAG,"Peers trouvés");
             } 
             public void onFailure(int reasonCode) {
                 Log.v(TAG,"Pas de Peers");
             } 
         });     
    } 

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

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

    @Override
     public void onPeersAvailable(WifiP2pDeviceList peers) {
     WifiP2pDevice device;
            for (int i = 0; i < peerList.getDeviceList().size(); i++) 
            {

            device  = (WifiP2pDevice) (peerList.getDeviceList().toArray())[i];

            }
    }

}

于 2015-05-29T06:20:09.217 回答