Giter VIP home page Giter VIP logo

sharethem's Introduction

SHAREthem

App_Icon

SHAREthem library facilitates P2P file sharing and transfers between devices using WiFi Hotspot.
Also an attempt to simulate popular SHAREit App functionality and improvise it by supporting multiple receivers at one go

Library also supports App (Android) to Web/Mobile Browser transfers if Receiver has no App installed.

Download the Working version from Play Store

[![PlayStore][playstore-image]][playstore-url] [playstore-image]: screens/google-play-badge.png [playstore-url]: https://play.google.com/store/apps/details?id=com.tml.sharethem.demo

Developed by

Srihari Yachamaneni (@srihari_y)

Features

  • File transfers and sharing.
  • Supports downloads on Browser if Receiver has no App installed.
  • Easy to Implement.
  • No permissions required. (library handles them based on targetSdkVersion)

Usage

UPDATE

Added Android-8, OREO support with default Android Hotspot functionality which comes with password protected Hotspot and so Receiver has to explicitly key-in password, inline with SHAREit functionality.

SHARE mode

To start SHARE mode, you need to pass an array of strings holding references of files you want to share along with port(optional) and sender name using an Intent to start SHAREthemActivity

Intent intent = new Intent(getApplicationContext(), SHAREthemActivity.class);
intent.putExtra(ShareService.EXTRA_FILE_PATHS, new String[]{[path to file1], [path to file2]}); // mandatory
intent.putExtra(ShareService.EXTRA_PORT, 52287); //optional but preferred. PORT value is hardcoded for Oreo and above since it's not possible to set SSID with which port info can be extracted on Receiver side.
intent.putExtra(ShareService.EXTRA_SENDER_NAME, "Sri"); //optional. Sender name can't be relayed to receiver for Oreo & above
startActivity(intent);

ShareService.EXTRA_FILE_PATHS: holds location references to local files on device.

ShareService.EXTRA_PORT (optional): in case you want to start SHAREserver on a specific port. Passing 0 or skipping this lets system to assign its own. One of the downside of letting system to do this is SSID may be not the same for a subsequent Sharing session. (PORT number is used in algorithm to generate SSID)

ShareService.EXTRA_SENDER_NAME (optional): used by Receiver to display connection information.

Receiver mode

Starting receiver mode is pretty simple as no intent extras needed. Receiver Activity starts scanning for senders automatically, you can turn off Receiver mode anytime though.

startActivity(new Intent(getApplicationContext(), ReceiverActivity.class));

End with an example of getting some data out of the system or using it for a little demo

Read more about implementation on my blog here

DEMO Built With

IMPORTANT NOTE

  • increasing targetSdkVersion version might impact behaviour of this library
    1. if targetSdkVersion >= 23
      • ShareActivity has to check for System Write permissions to proceed
      • Get Wifi Scan results method needs GPS to be ON and COARSE location permission.
    2. library checks the targetSdkVersion to take care of above scenarios if targetSdkVersion > 20
    3. If an application's targetSdkVersion is LOLLIPOP or newer, network communication may not use Wi-Fi even if Wi-Fi is connected with no interner. this might impact when Receiver connectivity to SHAREthem hotspot, library checks for this scenario and prompts user to disable data For more info: https://developer.android.com/reference/android/net/wifi/WifiManager.html#enableNetwork

Troubleshooting

Receiver Connected but not able to display Sender info:

As mentioned above, on API level 21 & above, n/w communication may not use Wifi with no internet access if MOBILE DATA has internet access. Library can prompt to disable MOBILE DATA if this scenario is met, but not yet implemented. So turn-off mobile to make Receiver work.

ReceiverActivity cannot connect to SHAREthem Wifi:

Receiver tries to connect to SHAREthem Wifi in every way possible, but throws an error dialog if it fails. Dialog says to manually select SHAREthem Wifi on WIfi Settings. As a last resort, you can always manually connect to Sender Wifi.

Known Issues

  • On some devices with API level 21 and above, connectToOpenHotspot() method on WifiUtils disables other Wifi networks in-order to connect to SHAREthem WIfi Hotspot. Ideally increasing priority should be enough for making WifiConfiguration but recent Android version are making it hard for Wifi n.ws with no internet connectivity.
  • Enabling SHARE mode when System Wifi Hotspot is already active (enabled by user via Settings) might not work.

Screenshots

Demo Activity File explorer SHAREthem Activity SHAREthem Service Notification Receiver Activity Android Download Manager Notification Web Receiver

License

Copyright 2017 Srihari Yachamaneni

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

see the LICENSE.md file for details

sharethem's People

Contributors

sriharia 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  avatar  avatar  avatar  avatar

sharethem's Issues

Getting socketTimeoutException when downloading data in receiver side

Connection between the devices is successful. For transferring data from the server to the client, I am using the below code:

 public static String downloadDataFromSender(String apiUrl) throws IOException {
    InputStream is = null;
    try {
        URL url = new URL(apiUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
        conn.getResponseCode();

        is = conn.getInputStream();

        return readIt(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

But I keep getting the following error:

java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:343)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:205)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:187)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:586)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
at com.android.okhttp.Connection.connectSocket(Connection.java:196)
at com.android.okhttp.Connection.connect(Connection.java:172)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.wifiscanner.utils.DownloadUtils.downloadDataFromSender(DownloadUtils.java:27)
at com.wifiscanner.tasks.SenderAPITask.doInBackground(SenderAPITask.java:29)
at com.wifiscanner.tasks.SenderAPITask.doInBackground(SenderAPITask.java:14)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)

The connection is successful as I said. So not sure why I am getting network is unreachable. This happens intermittently and not always. It retries and after 3 or 4 times it is successful. Sometimes after 15 tries it is successful. Not sure why.

Problem Creating Hotpost at Oreo Device

When I try to send files from Oreo device after selecting files when I press Ok, app crashes and gives the following error in Logcat
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)

Problem With The Connection For Oreo

There is a code in receiveractivity.java

line : 448

in wifiscanner class

if (WifiUtils.isShareThemSSID(result.SSID) && (isOreoOrAbove() || WifiUtils.isOpenWifi(result))) {
Log.d(TAG, "signal level: " + result.level);
connectToWifi(result.SSID);
foundSTWifi = true;
break;
}

but with oreo device everytime that condition is not calling and skipping the part
As you mantion in description about oreo and hotspot with password stuff .That's true but can't connect with other device please reply

Hotspot not enabled on Android Nougat and Oreo

Hi Sriharia,

First of all thank you for such an amazing piece of code. Your project helped me a lot in one of my Project. However, I am having some issue on Android Nougat and Oreo devices. I am not able to enable the Hotspot on Android N and O. Kindly help me out.

Shall I do some extra coding for the same?

Crash

Hi Sri,

I have installed and tested your application from play store. it was crashing . Then i downloaded a code and found an Exception while debugging. Actually you are checking isShareThemSSID() with detected networks. In that, you are splitting ssid by "-". My wifi name is "harry-wifi" . In isShareThemSSID(), it's spits ["harry", "wifi"] and tries to decode it. there it's giving an exception.

Existing logic:
public static boolean isShareThemSSID(String ssid) {
String[] splits = ssid.split("-");
if (splits.length != 2)
return false;
String[] names = new String(Base64.decode(splits[1], Base64.DEFAULT)).split("\|");
return names.length == 3 && names[1].equals(SENDER_WIFI_NAMING_SALT);
}

**Consider the change **
public static boolean isShareThemSSID(String ssid) {
String[] splits = ssid.split("-");
if (splits.length != 2)
return false;
try {
String[] names = new String(Base64.decode(splits[1], Base64.DEFAULT)).split("\|");
return names.length == 3 && names[1].equals(SENDER_WIFI_NAMING_SALT);
} catch (Exception e) {
return false;
}

}

This will avoid crashing in your application.

Problem Connection Hotpost created by Nouget

When I select files from Android version 7.1.1 , it creates hotpost after selecting files , but receiver device doesnot connect to the network , it always shows the toast "Sender Hotspot disconnected. retrying"

Unable to access location

Unable to access the location using runtime permission the location access is not prompted.
not able to get the SSID from the wifimanger, showing UNKNOWN SSID, need location acces
but location code is not working.

Sharing

Hi sri,
you are done excellent work. its very useful for me. but small modification need in your example. you are sharing file to all device.

but i need to share file by selecting the connected device. can you please help to do this in your example.

thanks!
suresh

How to connect to a device from the list of displayed devices?

Hey, I had a requirement, where the list of available devices would be displayed and then data would be sent to the selected device. Like shareIt, where the list of nearby devices are displayed and only when selecting a particular device, it is connected to that device.

Thanks for the really cool library. It has helped me a lot :)

issue in api level 26

since api level 26 not allowed setWifiApEnabled method so unable to create a hotspot

Cant Find Any Receiver

Tried on android 7 and 10. Everything was working fine but when I tried to send a file, it was unable to find my receiving device! and moreover its not ready for android 10, notification function needed to change!

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.