1

I achieved to transfer data between two android phones using WebRTC's DataChannel :

On one side, I send the data:

boolean isBinaryFile = false;
File file = new File(path); // let's assume path is a .whatever file's path (txt, jpg, pdf..) 
ByteBuffer byteBuffer = ByteBuffer.wrap(convertFileToByteArray(file));
DataChannel.Buffer buf = new DataChannel.Buffer(byteBuffer, isBinaryFile); 
dataChannel.send(buf);

On the other side, this callback should be called regardless of isBinaryFile value :

public void onMessage( final DataChannel.Buffer buffer ){
    Log.e(TAG, "Incomming file on DataChannel");

    ByteBuffer data = buffer.data;
    byte[] bytes = new byte[ data.capacity() ];
    data.get(bytes);

    // If it's not a binary file (text)
    if( !buffer.binary ) {
        String strData = new String( bytes );
        Log.e(TAG, "Text file is : " + strData);
    } else {
        Log.e(TAG, "Received binary file ! :)");
    }
}

In the case of any file, when isBinaryFile is false, the callback is called and I'm able to print the text, or even reconstruct the file (images, pdf, whatever).

When isBinaryFile is true I get the following error:

Warning(rtpdataengine.cc:317): Not sending data because binary type is unsupported.

After reading this, looks like I need to use SCTP DataChannels, but I don't know how !

lmo
  • 497
  • 5
  • 23
  • How did you register `onMessage` callback? – Incerteza Apr 05 '15 at 03:08
  • Sorry I'm late. Just `implements DataChannel.Observer`. – lmo Apr 07 '15 at 08:19
  • Can you help with this http://stackoverflow.com/questions/29499725/new-peerconnectionfactory-gives-error-on-android ? Or can you please share a sample code which sends file from one android to other.. Just an example code which helps in understanding the way... – SamFast Apr 07 '15 at 21:15

1 Answers1

1

Finally found a solution !

Before, I constructed my PeerConnection with RtpDataChannels constraint to true; but to use SCTP DataChannels, you must let it to default (or set it to false), like this:

MediaConstraints pcConstraints = signalingParameters.pcConstraints;
// pcConstraints.optional.add(new KeyValuePair("RtpDataChannels", "false"));
pcConstraints.optional.add(new KeyValuePair("DtlsSrtpKeyAgreement", "true"));
pc = factory.createPeerConnection(signalingParameters.iceServers,
            pcConstraints, pcObserver);

Simple ! :-)

lmo
  • 497
  • 5
  • 23