【发布时间】:2015-04-17 22:49:48
【问题描述】:
我实现了使用 WebRTC 的DataChannel 在两部安卓手机之间传输数据:
一方面,我发送数据:
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 为 true 时,我收到以下错误:
Warning(rtpdataengine.cc:317): Not sending data because binary type is unsupported.
看完this,貌似需要用SCTP DataChannels,但是不知道怎么用!
【问题讨论】:
-
你是如何注册
onMessage回调的? -
对不起,我迟到了。只需
implements DataChannel.Observer。 -
你能帮忙解决这个stackoverflow.com/questions/29499725/… 吗?或者您能否分享一个将文件从一个android发送到另一个android的示例代码。只是一个有助于理解方式的示例代码......
标签: android file webrtc file-transfer rtcdatachannel