Skip to content
Advertisement

Android WebRTC DataChannel is always null and ICE candidates are not sent

I’m trying to create an Android app that uses WebRTC data channels for data exchange. The data that I want to send is basic strings. Admittedly, this is my first time looking at WebRTC, and so I am a bit fuzzy on the details. My problem is that whenever I try to create a data channel, it is always null, and ICE candidate requests do not seem to be exchanged with the signalling server. I started from this example that creates a connection to exchange video between two devices and modified it to not exchange video but instead create a data channel.

I looked through a lot of other answers but the vast majority have to do with WebRTC in the browser, and data channel examples are rare to begin with. I also looked through the google chromium source code implementation of WebRTC in c++ to see if anything could be learned but had no luck.

My code is as follows

WebRtcActivity.kt

JavaScript

RTCClient.kt

JavaScript

SignallingClient.kt

JavaScript

CustomWebSocketListener.java (forgive me for the java + kotlin mixup, I am more comfortable with java)

JavaScript

DataChannelObserver.java

JavaScript

and finally, the signalling server

JavaScript

(I believe that the signalling server works, as it just forwards the requests to the other, and currently I am just testing with two devices. I modeled it off the example signalling server)

The SignallingClientListener and PeerConnectionObserver classes can be found in the example linked above, I did not modify them.

I tried creating the data channel before creating the offer, after creating it, after the connection has been established, and it is always null. I use the negotiated connection since I just want something really basic, and will be able to agree on the ID beforehand.

I’ve not set up my own STUN/TURN servers as my devices worked with the video calling example and so I imagine they should work with this, but let me know if I’m wrong.

Any help or tips about other possibly easier solutions would be greatly appreciated.

For reference, I am testing on two android 10 devices, and the webrtc version is 1.0.32006.

Advertisement

Answer

Answering my own question, not entirely but will post what worked for me. Ultimately I could not figure out what was wrong with the code above, I suspect somewhere I was not doing something right with the initialization or requests.

Some of the files are from above and have not been modified, but I’ll post them anyways. Also, I had used this article as a starting point.

CustomPeerConnection.java

JavaScript

CustomDataChannelObserver.java

JavaScript

CustomPeerConnectionObserver.java

JavaScript

CustomSdpObserver.java

JavaScript

CustomWebSocketListener.java

JavaScript

SignallingClient.kt

JavaScript

WebRtc2Activity.java

JavaScript

signalling server is the exact same as the one in the question.

Truth be told, the above code is decent starting code, and needs improvements. The way it’s run in the activity is not done well, and there really isn’t any error handling or dropped connection checks. I only did so to be able to make sure the mechanism works. So to anyone who may find this useful, do not run it the way I do here, modify it or extend.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement