Giter Site home page Giter Site logo

anitaa1990 / wifi-connect Goto Github PK

View Code? Open in Web Editor NEW
108.0 7.0 51.0 1.47 MB

A library project to connect two devices using Wifi-Direct

License: Apache License 2.0

Java 100.00%
android-library android wifi-direct java share data receiver wifi-connection sender broadcastreceiver wrapper wifip2pconnectioncallback-interface android-sdk

wifi-connect's Introduction

152px

Wifi-Connect

Android Arsenal

A simple wrapper module to connect two devices and share data using Wifi-Direct.

This library provides an easy way to access the wifi direct api provided by Android without having to deal with all the boilerplate stuff going on inside.

Sample App

Get it on Google Play

Download the sample app on the Google Play Store and check out all the features

API

How to integrate the library in your app?

Gradle Dependecy - Java
dependencies {
    implementation 'com.wifiscanner:wifiscanner:0.1.2'
}

Maven Dependecy - Java

<dependency>
  <groupId>com.wifiscanner</groupId>
  <artifactId>wifiscanner</artifactId>
  <version>0.1.2</version>
  <type>pom</type>
</dependency>

Usage

Step 1:Define the AndroidManifest.xml class:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Declare the following service:

<service android:enabled="true" android:name="com.wifiscanner.server.ServerDataService" />

Note: Currently the app does not support runtime permissions.


Step 2: Define the wrapper class in the onCreate() method of the activity

WifiP2PService wifiP2PService = new WifiP2PServiceImpl.Builder()
                .setSender(this)
                .setWifiP2PConnectionCallback(this)
                .build();
        wifiP2PService.onCreate();

We need to initialize the wrapper class and declare the activity as either the Sender or the Receiver. For instance, in order to declare the activity as a Sender class, we use .setSender(this). In order to declare the activity as a Receiver class, we use .setReceiver(this)

The Receiver - will act as the server. It will fetch the list of available nearby devices and select a device to connect with. The Sender - will act as the client. Once a connection request is made from the server, the sender will accept the connection and pass data to the Receiver


Step 3: Add the following method in the onResume method of the activity

wifiP2PService.onResume();

This method will register the broadcast receiver that will listen for the changes in Wifi network.


Step 4: Add the following method in the onStop method of the activity

wifiP2PService.onStop();

This method will unregister the broadcast receiver since the activity is no longer at the top of the activity.


Step 5: Implement the WifiP2PConnectionCallback interface in your activity

public class MainActivity extends AppCompatActivity implements WifiP2PConnectionCallback {

Functions in the WifiP2PConnectionCallback interface

Function Name Usage
initiateDiscovery() This callback method is called when the scan is first initiated
onDiscoverySuccess() This callback method is called the discovery is successful
onDiscoveryFailure() This callback method is called the discovery is not successful
onPeerAvailable() This callback method is called with a list of all peer available devices
onPeerStatusChanged() This status of the peers that are discovered is changing
onPeerConnectionSuccess() called when a connection to a nearby device is successful
onPeerConnectionFailure() called when a connection to a nearby device is not successful
onPeerDisconnectionSuccess() called when a disconnection to a nearby device is successful
onPeerDisconnectionFailure() called when a disconnection to a nearby device is not successful
onDataTransferring() called when data transfer has been initiated
onDataTransferredSuccess() called when data transfer has completed & is successful
onDataTransferredFailure() called when data transfer has completed & is not successful
onDataReceiving() Called when the receiver has started to receive the data from sender
onDataReceivedSuccess(String message) Called when the receiver has received the data successfully
onDataReceivedFailure() Called when the data transferred is completed but data is not received


Methods available in the Receiver class: Method to connect to an available device: - you need to pass the device that is to be connected wifiP2PService.connectDevice(wifiP2pDevice);


Method to start data transfer - you need to pass the message that needs to be shared to the client wifiP2PService.startDataTransfer("Helllooo");


And that's it folks!

Created and maintained by:

Anitaa Murthy [email protected]

Twitter Google Plus Linkedin

wifi-connect's People

Contributors

anitaa1990 avatar chintanrathod avatar hafizahmmed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

wifi-connect's Issues

Failed to discover peers: WifiP2pDeviceList from onPeerAvailable has size of 0

Hi,
I am using two Google Pixel 2 phones to connect Wifi-direct to each other using your Wifi-Connect. If I manually check in Settings -> Network & Internet -> Wifi -> Wifi Preferences -> Advanced -> WiFi-Direct, I can discover peers and connect them.

Using your repo in my project, I followed the instructions and have all the required permissions granted. However, I cannot discover nearby peers and always see WifiP2pDeviceList returned from onPeerAvailable with the size of 0. FYI, the interfaces - onInitiateDiscovery() and onDiscoverySuccess() - are also called back.

Peer connection failure

My code keeps going to onPeerConnectionFailure() calback.
Here it is -

` private WifiP2PServiceImpl wifiP2PService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wifiP2PService = new WifiP2PServiceImpl.Builder()
            .setSender(this)
            .setWifiP2PConnectionCallback(this)
            .build();
    wifiP2PService.onCreate();
}

@Override
public void onInitiateDiscovery() {
    Log.d("*****", "initiate discovery");
}

@Override
public void onDiscoverySuccess() {
    Log.d("*****", "discovery success");

}

@Override
public void onDiscoveryFailure() {
    Log.d("*****", "discovery failure");

}

@Override
public void onPeerAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
    Log.d("*****", "peer available");

    ArrayList<WifiP2pDevice> devices = new ArrayList<>(wifiP2pDeviceList.getDeviceList());

    for(int i=0; i<devices.size(); i++){
        if(devices.get(i).deviceName.equals("Smart TV"))
            wifiP2PService.connectDevice(devices.get(i));
    }
}

@Override
public void onPeerStatusChanged(WifiP2pDevice wifiP2pDevice) {
    Log.d("*****", "peer status change");

}

@Override
public void onPeerConnectionSuccess() {
    wifiP2PService.startDataTransfer("Helllooo");
    Log.d("*****", "peer connection success");

}

@Override
public void onPeerConnectionFailure() {
    Log.d("*****", "peer connection failure");

}

@Override
public void onPeerDisconnectionSuccess() {
    Log.d("*****", "peer disconnection success");

}

@Override
public void onPeerDisconnectionFailure() {
    Log.d("*****", "peer disconnection failure");

}

@Override
public void onDataTransferring() {
    Log.d("*****", "data transferring");

}

@Override
public void onDataTransferredSuccess() {
    Log.d("*****", "data transferring success");

}

@Override
public void onDataTransferredFailure() {
    Log.d("*****", "data transferring failure");

}

@Override
public void onDataReceiving() {
    Log.d("*****", "data receiving");

}

@Override
public void onDataReceivedSuccess(String s) {
    Log.d("*****", "data receiving success");

}

@Override
public void onDataReceivedFailure() {
    Log.d("*****", "data receiving failure");

}

@Override
protected void onResume() {
    super.onResume();
    wifiP2PService.onResume();
}

@Override
protected void onStop() {
    super.onStop();
    wifiP2PService.onStop();
}`

Here is my receiver app code -

` private WifiP2PServiceImpl wifiP2PService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wifiP2PService = new WifiP2PServiceImpl.Builder()
            .setReceiver(this)
            .setWifiP2PConnectionCallback(this)
            .build();
    wifiP2PService.onCreate();
}

@Override
public void onInitiateDiscovery() {
    Log.d("*****", "initiate discovery");
}

@Override
public void onDiscoverySuccess() {
    Log.d("*****", "discovery success");

}

@Override
public void onDiscoveryFailure() {
    Log.d("*****", "discovery failure");

}

@Override
public void onPeerAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
    Log.d("*****", "peer available");

}

@Override
public void onPeerStatusChanged(WifiP2pDevice wifiP2pDevice) {
    Log.d("*****", "peer status change");

}

@Override
public void onPeerConnectionSuccess() {
    Log.d("*****", "peer connection success");

}

@Override
public void onPeerConnectionFailure() {
    Log.d("*****", "peer connection failure");

}

@Override
public void onPeerDisconnectionSuccess() {
    Log.d("*****", "peer disconnection success");

}

@Override
public void onPeerDisconnectionFailure() {
    Log.d("*****", "peer disconnection failure");

}

@Override
public void onDataTransferring() {
    Log.d("*****", "data transferring");

}

@Override
public void onDataTransferredSuccess() {
    Log.d("*****", "data transferring success");

}

@Override
public void onDataTransferredFailure() {
    Log.d("*****", "data transferring failure");

}

@Override
public void onDataReceiving() {
    Log.d("*****", "data receiving");

}

@Override
public void onDataReceivedSuccess(String s) {
    Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    Log.d("*****", "data receiving success");
}

@Override
public void onDataReceivedFailure() {
    Log.d("*****", "data receiving failure");

}

@Override
protected void onResume() {
    super.onResume();
    wifiP2PService.onResume();
}

@Override
protected void onStop() {
    super.onStop();
    wifiP2PService.onStop();
}

`
Can you help me with the reason behind it?

Logo/icon for Wifi-Connect

Hello,
Good Day, i make a logo icon for your project. If you want to use this icon in your project, then comment below, I will give you the source file. And if you want a change, then tell me I will change.

wifi-connect-01

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.