我实现了使用WebRTC DataChannel
在两部Android手机之间传输数据:
一方面,我发送数据:
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);
另一方面,无论isBinaryFile
值是什么,都应调用此回调:
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 ! :)");
}
}
如果是任何文件,当isBinaryFile
为 false 时,会调用回调并且我可以打印文本,甚至可以重建文件(图片, pdf,无论如何)。
当isBinaryFile
为真时,我收到以下错误:
Warning(rtpdataengine.cc:317): Not sending data because binary type is unsupported.
看完this后,看起来我需要使用SCTP DataChannels
,但我不知道怎么做!
答案 0 :(得分:1)
终于找到了解决方案!
在此之前,我将PeerConnection
RtpDataChannels
约束true
构建到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);
;但要使用 SCTP DataChannels ,您必须将其设为默认值(或将其设置为false),如下所示:
{{1}}
简单! : - )