1

WiFi Directを使用するアプリを作成しているため、Androidの WiFi Direct ガイドに従っています。また、2つのクラスがあります。1 つは ClientReceive と呼ばれ、もう 1 つは HostSend と呼ばれます。クライアントがホストへの接続を開始すると、ホストはデータをクライアントにプッシュします。

WiFi Direct セットアップの開始時に状態の変化をリッスンするブロードキャスト レシーバーを作成しました。

package com.mgamerzproductions.wifidirecttest;

import android.app.Activity;
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;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.util.Log;

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

    private WifiP2pManager manager;
    private Channel channel;
    private WiFiActivity activity;

    /**
     * @param manager WifiP2pManager system service
     * @param channel Wifi p2p channel
     * @param Host class (HostSend) for this constructor.
     */
    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
            HostSend activity) {
        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
    }

    /**
     * @param manager WifiP2pManager system service
     * @param channel Wifi p2p channel
     * @param activity activity associated with the receiver
     */
    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
            ClientReceive activity) {
        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
    }

    /*
     * (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
                activity.setIsWifiP2pEnabled(true);
            } else {
                activity.setIsWifiP2pEnabled(false);
                //activity.resetData();

            }
            Log.d(Launch.APPTAG, "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(channel, (PeerListListener) activity.getFragmentManager()
                        .findFragmentById(R.id.frag_list));*/
            }
            Log.d(Launch.APPTAG, "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

                HostSend connectionListener = activity;
                manager.requestConnectionInfo(channel, connectionListener);
            } else {
                // It's a disconnect
                activity.resetData();
            }
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            /*DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                    .findFragmentById(R.id.frag_list);
            fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                    WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));*/

        }
    }
}

ただし、両方のデバイスが WiFi ダイレクトをセットアップし、コールバック (アクティビティへのコールバックと呼ばれるものだと思いますか?) を使用して接続をセットアップする必要があるため、別のクラス ClientReceive にもレシーバーが必要です。どちらのクラスも WiFi アクティビティのサブクラスです。これは基本的に、Activity のいくつかのメソッドを super.methodname() だけでオーバーライドする空のクラスであり、子がアクセスできるようにします。IT には、WiFi に関連する追加の方法がいくつかあります。キャストを避けるためにこれを作成しましたが、子クラスのメソッドが異なるため機能しません。サブクラスの代わりに WiFiActivity を使用すると、サブクラスのメソッドを使用できません。

これを適切に行う方法はありますか?私のコードは進行中の作業です (それがちょっと醜い理由です) が、アイデアがありません。各デバイスの各アクティビティから 1 つずつ、2 つのコンストラクターがあります (1 つはホスト デバイス、もう 1 つはクライアント デバイスです)。私がやりたいのは、このクラスを両方のクラスで共有することですが、このようなアイテムをキャストする方法がわからないため、HostSend connectionListener (HostSend) アクティビティを適切にキャストする方法がわかりません。

ありがとう。説明が長くなってしまい申し訳ありません。

4

0 に答える 0