GithubHelp home page GithubHelp logo

direct's Introduction

Direct

Direct is a library that provides a simplified interface to wrap around the Wi-Fi Peer-to-Peer API for building applications with a client-server architecture. In essence, this interface acts as a facade around the Wi-Fi Peer-to-Peer API by hiding its implementation details. This library uses service discovery exclusively.

Table of Contents

Initial Setup

The following must be added to the Android Manifest XML. As this library deals exclusively with service discovery, an API level of 16 is required.

<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

Instantiation

Following from a client-server architecture, the library provides functionality for both a client and a server. This library will refer to a server as a host.

Instantiating a Host

This instance is very important and must persist throughout the lifecycle of your application so long as you are maintaining connections with clients.

WifiDirectHost host = new WifiDirectHost(getApplication(), "UNIQUE_SERVICE_TAG", "UNIQUE_INSTANCE_TAG");

The service tag is important is it is used as a filter for those clients who are looking for the specific service hosted by the implementing application. The service tag must be unique in order to no conflict with other applications using the Wi-Fi Peer-to-Peer API. The instance tag is used to indentify the specific device hosting the service rather than the service itself.

Instantiating a Client

This instance is very important and must persist throughout the lifecycle of your application so long as you are maintaining connection with a host.

WifiDirectHost host = new WifiDirectClient(getApplication(), "UNIQUE_SERVICE_TAG");

The service tag must be a direct match of the host device in order for the client to discover the given service.

Hosting a Service

Starting a Service

host.startService(new ObjectCallback() {
    @Override
    public void onReceived(Object object) {
        // Invoked when an object has been received from a client
    }
}, new ClientCallback() {
    @Override
    public void onConnected(WifiP2pDevice clientDevice) {
        // Invoked when a Client has connected
    }

    @Override
    public void onDisconnected(WifiP2pDevice clientDevice) {
        // Invoked when a Client has disconnected
    }
}, new ServiceCallback() {
    @Override
    public void onServiceStopped() {
        // Invoked when the service is no longer available
    }
}, new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the request to the Wi-Fi P2P Framework to start a service was successful
    }

    @Override
    public void onFailure() {
        // Invoked when the request to the Wi-Fi P2P Framework to start a service was unsuccessful
    }
});

Stopping a Service

The result of this function call does not guarantee that the service has stopped, it is simply a request to the Wi-Fi Peer-to-Peer Framework; however, it is possible to capture this event through the ServiceCallback passed as an argument when starting a service.

host.stopService(new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the request to the Wi-Fi P2P Framework to stop the service was successful
    }

    @Override
    public void onFailure() {
        // Invoked when the request to the Wi-Fi P2P Framework to stop the service was unsuccessful
    }
});

Service Discovery

Starting Service Discovery

Prior to connecting to a service, the client must discover said service.

client.startDiscovery(new DiscoveryCallback() {
    @Override
    public void onDiscovered(WifiP2pDevice hostDevice) {
        // Invoked when a new service has been discovered
    }

    @Override
    public void onLost(WifiP2pDevice hostDevice) {
        // Invoked when a previously discovered service is no longer available
    }
}, new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the request to the Wi-Fi P2P Framework to start the service discovery was successful
    }

    @Override
    public void onFailure() {
        // Invoked when the request to the Wi-Fi P2P Framework to start the service discovery was unsuccessful
    }
});

Stopping Service Discovery

If for whatever reason, the client would like to stop discovering services.

client.stopDiscovery(new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the request to the Wi-Fi P2P Framework to stop service discovery was successful
    }

    @Override
    public void onFailure() {
        // Invoked when the request to the Wi-Fi P2P Framework to stop service discovery was unsuccessful
    }
});

Connecting to a Service

Connecting to the Host Device

After the client has discovered a service that they wish to make a connection with, that client may now connect to that service with the respective host WifiP2pDevice. The physical connection with the host is captured in the ConnectionCallback, the ResultCallback only captures the success of the request to the Wi-Fi Peer-to-Peer Framework.

client.connect(hostDevice, new ObjectCallback() {
    @Override
    public void onReceived(Object object) {
        // Invoked when an object has been received from the host
    }
}, new ConnectionCallback() {
    @Override
    public void onConnected() {
        // Invoked when a connection with the host has been successfully made
    }

    @Override
    public void onDisconnected() {
        // Invoked when a connection with the host has been lost
    }
}, new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the request to the Wi-Fi P2P Framework to connect to the host was successful
    }

    @Override
    public void onFailure() {
        // Invoked when the request to the Wi-Fi P2P Framework to connect to the host was successful
    }
});

Disconnecting from the Host Device

The result of this function call does not guarantee that the client has disconnected from the host, it is simply a request to the Wi-Fi Peer-to-Peer Framework; however, it is possible to capture this event through the ConnectionCallback passed as an argument when connecting to the host.

client.disconnect(new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the request to the Wi-Fi P2P Framework to disconnect from the host was successful
    }

    @Override
    public void onFailure() {
        // Invoked when the request to the Wi-Fi P2P Framework to disconnect from the host was unuccessful
    }
});

Data Transfer

Sending an Object to a Client

host.send(clientDevice, text, new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the object was successfully sent to the respective client
    }

    @Override
    public void onFailure() {
        // Invoked when the object was unable to be sent to the respective client
    }
});

Sending an Object to the Host

client.send(text, new ResultCallback() {
    @Override
    public void onSuccess() {
        // Invoked when the object was successfully sent to the host
    }

    @Override
    public void onFailure() {
        // Invoked when the object was unable to be sent to the host
    }
});

Cleaning Up

When the application is finished with the Wi-Fi Peer-to-Peer Framework, it is useful to clean up after ourselves as there are resources that will linger otherwise. This library will register a broadcast receiver which will need to be unregistered.

Cleaning Up Host Resources

In addition to unregistering the broadcast receiver, the host will attempt to clear all local services.

host.cleanUp();

Cleaning Up Client Resources

In addition to unregistering the broadcast receiver, the client will attempt to disconnect from a potential connection.

client.cleanUp();

direct's People

Contributors

tylerjmcbride avatar aaronkmcbride avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.