Android Open Source - dissertation-project Wifi Direct Sd Helper






From Project

Back to project page dissertation-project.

License

The source code is released under:

MIT License

If you think the Android project dissertation-project listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.fyp.resilience.swarm.helper;
/*from w ww .  ja  va  2 s.  c  om*/
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.ChannelListener;
import android.net.wifi.p2p.WifiP2pManager.DialogListener;
import android.net.wifi.p2p.WifiP2pManager.DnsSdServiceResponseListener;
import android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfo;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest;
import android.util.Log;

import com.fyp.resilience.receiver.WiFiDirectBroadcastReceiver;
import com.fyp.resilience.service.PieceUploadService;

public class WifiDirectSdHelper implements SwarmHelperInterface {

    private static final String TAG = WifiDirectSdHelper.class.getSimpleName();
    private static final String TXTRECORD_PORT = "port";
    private static final String SERVICE_INSTANCE = "_resilwi";
    private static final String SERVICE_REG_TYPE = "_resil._tcp";

    private final PieceUploadService mService;
    private final WifiP2pManager mP2PManager;
    private final Channel mChannel;

    private WiFiDirectBroadcastReceiver mWifiReceiver;
    private final IntentFilter mWifiIntentFilter = new IntentFilter();

    private WifiP2pDnsSdServiceRequest mServiceRequest;
    private DnsSdServiceResponseListener mDnsListener;
    private DnsSdTxtRecordListener mDnsTxtRecordListener;
    private ActionListener mServiceAddListener;
    private ActionListener mDiscoverServiceListener;
    private ActionListener mLocalServiceListener;
    private WifiP2pDnsSdServiceInfo mServiceInfo;

    public WifiDirectSdHelper(final PieceUploadService service) {
        mService = service;
        mP2PManager = (WifiP2pManager) mService.getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mP2PManager.initialize(mService, mService.getMainLooper(), null);
    }

    private void initialiseDnsListener() {
        mDnsListener = new DnsSdServiceResponseListener() {

            @Override
            public void onDnsSdServiceAvailable(String instanceName,
                    String registrationType, WifiP2pDevice srcDevice) {

                // A service has been discovered. Is this our app?

                if (instanceName.equalsIgnoreCase(SERVICE_INSTANCE)) {
                    Log.d(TAG, "onBonjourServiceAvailable " + instanceName);
                }
            }
        };
    }

    private void initialiseDnsTxtRecordListener() {
        mDnsTxtRecordListener = new DnsSdTxtRecordListener() {

            @Override
            public void onDnsSdTxtRecordAvailable(String fullDomainName,
                    Map<String, String> txtRecordMap, WifiP2pDevice srcDevice) {

                if (fullDomainName.contains(SERVICE_INSTANCE)) {

                    WifiP2pConfig config = new WifiP2pConfig();
                    config.deviceAddress = srcDevice.deviceAddress;
                    config.groupOwnerIntent = 0;
                    config.wps.setup = WpsInfo.PBC;

                    mP2PManager.connect(mChannel, config, null);
                }

            }
        };
    }

    private void initialiseServiceAddListener() {
        mServiceAddListener = new ActionListener() {

            @Override
            public void onSuccess() {
                mP2PManager.discoverServices(mChannel, mDiscoverServiceListener);
                Log.d(TAG, "Added service discovery request");
            }

            @Override
            public void onFailure(int arg0) {
                Log.d(TAG, "Failed adding service discovery request " + arg0);
            }
        };
    }

    private void initialiseDiscoverServiceListener() {
        mDiscoverServiceListener = new ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "Service discovery initiated");
            }

            @Override
            public void onFailure(int arg0) {
                Log.d(TAG, "Service discovery failed " + arg0);
            }
        };
    }

    private void initialiseLocalServiceListener() {
        mLocalServiceListener = new ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "Added Local Service");
            }

            @Override
            public void onFailure(int error) {
                Log.d(TAG, "Failed to add a service " + error);
            }
        };
    }

    private void initialiseDummyDialogListener() {

        mP2PManager.setDialogListener(mChannel, new DialogListener() {

            @Override
            public void onShowPinRequested(String arg0) {
                Log.d(TAG, "PIN Requested");
            }

            @Override
            public void onDetached(int arg0) {
                Log.d(TAG, "onDetached has been called");
            }

            @Override
            public void onConnectionRequested(WifiP2pDevice arg0, WifiP2pConfig
                    arg1) {
                mP2PManager.connect(mChannel, arg1, new ActionListener() {

                    @Override
                    public void onSuccess() {
                        Log.d(TAG, "Added DialogListener");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.d(TAG, "Failed to add DialogListener");
                    }
                });
                Log.d(TAG, "Accepting Wi-Fi Direct with Overriden DialogListener");
            }

            @Override
            public void onAttached() {
                Log.d(TAG, "onAttached has been called");
            }
        });
    }

    @Override
    public void initialise() {
        mWifiReceiver = new WiFiDirectBroadcastReceiver(mP2PManager, mChannel, mService);

        mWifiIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mWifiIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mWifiIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mWifiIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

        initialiseDnsListener();
        initialiseDnsTxtRecordListener();
        initialiseServiceAddListener();
        initialiseDiscoverServiceListener();
        initialiseLocalServiceListener();
        initialiseDummyDialogListener();
    }

    @Override
    public void register(final int port, final boolean discover, Context context) {
        mP2PManager.initialize(context, context.getMainLooper(), new ChannelListener() {

            @Override
            public void onChannelDisconnected() {
                Log.d(TAG, "Channel disconnected...");
            }
        });

        mService.registerReceiver(mWifiReceiver, mWifiIntentFilter);

        final Map<String, String> record = new HashMap<String, String>();
        record.put(TXTRECORD_PORT, String.valueOf(port));

        mServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, SERVICE_REG_TYPE, record);

        mP2PManager.addLocalService(mChannel, mServiceInfo, mLocalServiceListener);

        if (discover) {
            discover();
        }
    }

    @Override
    public void discover() {
        mP2PManager.setDnsSdResponseListeners(mChannel, mDnsListener, mDnsTxtRecordListener);
        mServiceRequest = WifiP2pDnsSdServiceRequest.newInstance();
        mP2PManager.addServiceRequest(mChannel, mServiceRequest, mServiceAddListener);
    }

    @Override
    public void stopDiscovery() {
        mP2PManager.removeGroup(mChannel, null);
    }

    @Override
    public void tearDown() {
        stopDiscovery();
        mService.unregisterReceiver(mWifiReceiver);
        mP2PManager.removeLocalService(mChannel, mServiceInfo, new ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "Local services cleared");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "Local services failed to clear");
            }
        });

        mP2PManager.removeServiceRequest(mChannel, mServiceRequest, new ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "Service requests cleared");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "Failed to clear service requests");
            }
        });
    }
}




Java Source Code List

com.fyp.resilience.Constants.java
com.fyp.resilience.Flags.java
com.fyp.resilience.GCMIntentService.java
com.fyp.resilience.PreferenceConstants.java
com.fyp.resilience.ResilienceApplication.java
com.fyp.resilience.ResilienceController.java
com.fyp.resilience.activity.LicenceActivity.java
com.fyp.resilience.activity.ResilienceActivity.java
com.fyp.resilience.activity.SettingsActivity.java
com.fyp.resilience.adapter.ClientListAdapter.java
com.fyp.resilience.adapter.ConnectionListAdapter.java
com.fyp.resilience.adapter.FileListAdapter.java
com.fyp.resilience.connection.Connectable.java
com.fyp.resilience.connection.ServerDownloadConnectable.java
com.fyp.resilience.connection.ServerUploadConnectable.java
com.fyp.resilience.connection.UploadConnectable.java
com.fyp.resilience.connection.WifiDownloadConnectable.java
com.fyp.resilience.connection.WifiUploadConnectable.java
com.fyp.resilience.database.ResilienceDbHelper.java
com.fyp.resilience.database.ResilienceDbManager.java
com.fyp.resilience.database.model.DataPiece.java
com.fyp.resilience.database.model.DataWhole.java
com.fyp.resilience.event.ClientListChanged.java
com.fyp.resilience.event.ClientModified.java
com.fyp.resilience.event.ConnectionProgressChange.java
com.fyp.resilience.event.ConnectionStateChange.java
com.fyp.resilience.event.ConnectionsModified.java
com.fyp.resilience.event.PieceStateChange.java
com.fyp.resilience.event.ServerRegistrationChanged.java
com.fyp.resilience.event.ServerUploadFinished.java
com.fyp.resilience.event.WholeModified.java
com.fyp.resilience.event.WifiDownloadFinished.java
com.fyp.resilience.event.WifiUploadFinished.java
com.fyp.resilience.fragment.ClientsFragment.java
com.fyp.resilience.fragment.ConnectionsFragment.java
com.fyp.resilience.fragment.FilesFragment.java
com.fyp.resilience.interfaces.Messagable.java
com.fyp.resilience.interfaces.Partialable.java
com.fyp.resilience.proto.ProtoBuffSpecification.java
com.fyp.resilience.receiver.AbstractConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.BootReceiver.java
com.fyp.resilience.receiver.ConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.WiFiDirectBroadcastReceiver.java
com.fyp.resilience.register.RegisterRequestInitializer.java
com.fyp.resilience.register.RegisterRequest.java
com.fyp.resilience.register.RegisterScopes.java
com.fyp.resilience.register.Register.java
com.fyp.resilience.register.model.DeviceInfo.java
com.fyp.resilience.service.PieceUploadService.java
com.fyp.resilience.stream.PiecedRandomAccessFile.java
com.fyp.resilience.swarm.helper.NsdHelper.java
com.fyp.resilience.swarm.helper.SwarmHelperInterface.java
com.fyp.resilience.swarm.helper.WifiDirectSdHelper.java
com.fyp.resilience.swarm.model.SwarmClient.java
com.fyp.resilience.thread.ResilienceRunnable.java
com.fyp.resilience.thread.ResilienceThreadFactory.java
com.fyp.resilience.util.ConnectionUtils.java
com.fyp.resilience.util.Utils.java
com.fyp.resilience.view.ClientView.java
com.fyp.resilience.view.ConnectionView.java
com.fyp.resilience.view.FileView.java
com.fyp.resilience.view.PieceProgressIndicator.java
com.fyp.resilience.widerst.WiderstRequestInitializer.java
com.fyp.resilience.widerst.WiderstRequest.java
com.fyp.resilience.widerst.WiderstScopes.java
com.fyp.resilience.widerst.Widerst.java
com.fyp.resilience.widerst.model.DataPiecePartial.java
com.fyp.resilience.widerst.model.DataWholePartial.java
com.fyp.resilience.widerst.model.PostResponse.java
com.fyp.widerst.Constants.java
com.fyp.widerst.WiderstObjectifyService.java
com.fyp.widerst.backend.FileJoinerBackend.java
com.fyp.widerst.cron.CronJobServlet.java
com.fyp.widerst.endpoint.DataPieceEndpoint.java
com.fyp.widerst.endpoint.DeviceInfoEndpoint.java
com.fyp.widerst.entity.DataPiece.java
com.fyp.widerst.entity.DataWhole.java
com.fyp.widerst.entity.DeviceInfo.java
com.fyp.widerst.handler.BlobstoreUploadHandler.java
com.fyp.widerst.partial.DataPiecePartial.java
com.fyp.widerst.partial.DataWholePartial.java
com.fyp.widerst.response.PostResponse.java
com.fyp.widerst.servlet.WholeFileServer.java
com.fyp.widerst.util.DbHelper.java
com.fyp.widerst.util.GcmHelper.java