Skip to content

API Usage

Karuppiah edited this page Nov 4, 2016 · 1 revision

For now, for connectivity, the receiver is the one who creates a WiFi hotspot and then sender connects to it. Soon Android-File-Share will have tools to automate the process of "Hotspot Creation" and "WiFi Scanning and Connection" programmatically. Or we will recommend some Open Source tools for the same.

Note : The sender and receiver don't need Internet for the library to work. We advise both to switch off Mobile data to avoid unnecessary usage of Internet by background Apps due to the WiFi hotspot connection.

API

The FileShare API uses Services and Threads to do the networking operations. The API takes up a Handler to give it to the Thread, so that the Thread can send messages to it and the Handler can consequently get them and this way we get to know the status of the Thread's work and know when the Thread has finished working etc.

Receiver Side

In your Activity, create a Handler to handle messages from the Thread. We will do the message handling later.

private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            
        }
    };

And then, in you Activity create a new FileReceiver Object by giving it the Context and the just created Handler.

fileReceiver = new FileReceiver(this,mHandler);

Now, to get the file, just use this line of code.

fileReceiver.getFile();

The above line of code does few operations and gives out a Passcode and finally waits for a connection from the sender. Sender uses the Passcode to connect to the receiver. When the sender connects, the receiver will receive the file that the sender sends and then close the connection once done. To know what is happening behind the scenes, the Thread will send messages to the Handler. Let's use the messages to know what's going on.

Constants relating to messages of receiver are defined under FileReceiver class.

private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case FileReceiver.CODE :
                    Toast.makeText(ReceiverActivity.this,"Code is " + (int)msg.obj,Toast.LENGTH_SHORT).show();
                    break;

                case FileReceiver.LISTENING :
                    Toast.makeText(ReceiverActivity.this,"Listening...",Toast.LENGTH_SHORT).show();
                    break;

                case FileReceiver.CONNECTED:
                    Toast.makeText(ReceiverActivity.this,"Connected!",Toast.LENGTH_SHORT).show();
                    break;

                case FileReceiver.RECEIVING_FILE :
                    Toast.makeText(ReceiverActivity.this,"Receiving File!",Toast.LENGTH_SHORT).show();
                    break;

                case FileReceiver.FILE_RECEIVED :
                    File file = (File) msg.obj;
                    Toast.makeText(ReceiverActivity.this,file.getName() + " Received!",Toast.LENGTH_SHORT).show();
                    Toast.makeText(ReceiverActivity.this,"Stored in " + file.getAbsolutePath(),Toast.LENGTH_SHORT).show();
                    fileReceiver.close();
                    break;

                case FileReceiver.RECEIVE_ERROR :
                    Toast.makeText(ReceiverActivity.this,"Error occured : " + (String)msg.obj,Toast.LENGTH_SHORT).show();
                    fileReceiver.close();
                    break;
            }
        }
    };

All constants are meaningful. Some of them have objects inside them, others don't have any objects inside them. Only the CODE, FILE_RECEIVED and RECEIVE_ERROR will have objects. CODE has a Passcode, a number , that the sender needs to connect to the receiver and send a file. FILE_RECEIVED has a File object reference of the received file and RECEIVE_ERROR has a String object to give information about the error that has occurred.

In FILE_RECEIVED and RECEIVE_ERROR case, we have called another method of FileReceiver.

fileReceiver.close();

It is used to stop the Service that is running in the background. This method Must be called.

And note the scope of fileReceiver. It's scope should be such that it can be accessed from user defined function and from the Handler code too.

Note :

For now, the API supports receiving only one file per connection. And with each FileReceiver object a connection can be made. So, to receive many files, you need to create many FileReceiver objects one by one after a file is received, that is after the FILE_RECEIVED message comes from the Thread.

Soon, we will support receiving many files using just one connection, that is using just one FileReceiver object. See Roadmap and Progress to know our future plans for features. And suggestions are welcome!

Sender Side

In your Activity, create a Handler to handle messages from the Thread. We will do the message handling later. Constants relating to sender are defined under FileSender class.

private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            
        }
    };

And then, in you Activity create a new FileSender Object by giving it the Context and the just created Handler.

fileSender = new FileSender(this,mHandler);

Now, to send the file, just use this line of code.

fileSender.sendFile(file,code);

Where file is a File object referring to the file to be sent. And code is an integer, the Passcode that is needed to connect to the receiver.

The above line of code does few operations and finally connects to the receiver and sends the file and then close the connection once done. To know what is happening behind the scenes, the Thread will send messages to the Handler. Let's use the messages to know what's going on.

Constants relating to messages of sender are defined under FileSender class.

private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case FileSender.CONNECTING :
                    Toast.makeText(SenderActivity.this,"Connecting...",Toast.LENGTH_SHORT).show();
                    break;

                case FileSender.CONNECTED :
                    Toast.makeText(SenderActivity.this,"Connected!",Toast.LENGTH_SHORT).show();
                    break;

                case FileSender.SENDING_FILE :
                    Toast.makeText(SenderActivity.this,"Sending File!",Toast.LENGTH_SHORT).show();
                    break;

                case FileSender.FILE_SENT :
                    Toast.makeText(SenderActivity.this,"File Sent!",Toast.LENGTH_SHORT).show();
                    fileSender.close();
                    break;

                case FileSender.SEND_ERROR :
                    Toast.makeText(SenderActivity.this,"Error occured : " + (String)msg.obj,Toast.LENGTH_SHORT).show();
                    fileSender.close();
                    break;
            }
        }
    };

We have meaningful constants here too. Here only SEND_ERROR message has an object inside it, others don't have any objects inside them. SEND_ERROR has a String object to give information about the error that has occurred.

Similar to FileReceiver code, here, in FILE_SENT and SEND_ERROR case, we have called another method of FileReceiver.

fileSender.close();

It is used to stop the Service that is running in the background. Again, this method Must be called. And yeah, note the scope of fileSender.

To look at a full code, refer FileSharerDemoApp source code, which can send one file only.

Note :

The receiver device displaying the Passcode in a Toast or say TextView, must be shown to the sender(person) so that the sender can type in the Passcode, which can then be passed to the function. This is just for security purposes and for some other technical reasons. Since the whole library is Open Source, anyone can misuse the code and send malicious stuff to the receiver if there's no Passcode. But soon we will try to provide a choice to the user to enable and disable Passcode feature.

And again, to send many files, you need to create many FileSender objects one by one, after FILE_SENT message comes from the Thread. Soon, we will support sending many files per connection.


The main aim of the FileShare API is to abstract the user from all the complex internal networking operations. So, we are trying to make the API as simple as possible. For any API changes or suggestions, you can create an issue in the issues page.

Clone this wiki locally