BT Socket Factory creation and connecting with java code#457
BT Socket Factory creation and connecting with java code#457Aniket392 wants to merge 3 commits intowip/p2p_btfrom
Conversation
Aniket392
commented
Mar 11, 2026
- Add C4BTSocketFactory, registers C4SocketFactory with LiteCore and handles all JNI callbacks (open, write, close, requestClose, dispose, attached)
- Add LiteCoreBTSocket, bridges BluetoothSocket L2CAP channel with LiteCore's C4Socket; implements SocketFromCore with read loop, write queue, and flow control
- Add NativeC4BTSocketFactory, JNI implementation for nRegisterFactory and nFromNative
pasin
left a comment
There was a problem hiding this comment.
There are lot of detail and setup in this PR, and I think we will need to setup an person review.
However, I have looked up where we put the current WebSocketFactory and CBLWebSocket classes. I think we should move LiteCoreBTSocket and C4BTSocketFactory to the EE repo under android's com.couchbase.lite.internal.. We may need to rename it without C4 and Core prefix (e.g. BluetoothSocketFactory and BluetoothSocket : Use the names that are consistent with others).
As a reference, where CBLWebSocket / SocketFactory are :
| cls_C4BTSocketFactory, m_open, | ||
| (jlong) socket, | ||
| (jlong) context, // factoryToken = context set at registration | ||
| jPeerID); |
There was a problem hiding this comment.
Based on https://developer.android.com/ndk/guides/jni-tips, it's a good practice to call ExceptionCheck() and ExceptionClear() to handle both caught and runtime exception when calling Java code such as
if (env->ExceptionCheck()) {
env->ExceptionDescribe(); // Log to logcat or stderr
env->ExceptionClear();
}
You may check if we have don't this anywhere else or have any helper function for this. If not, I suggest to create a helper function to do this or create a ticket to take care this later if the method doesn't declare throws.
There was a problem hiding this comment.
Currently in code base i can't see similar checks
I will create ticket for same
| // Heap-allocate a mutable copy so context can be set to itself. | ||
| auto* factory = new C4SocketFactory(kBTSocketFactory); | ||
| factory->context = factory; // token == pointer to the factory copy | ||
| c4socket_registerFactory(*factory); |
There was a problem hiding this comment.
This will cause the default c4socket factory to be a Bluetooth factory and I don't think we would want that. The correct way to return the socket factory for bluetooth is via C4PeerDiscoveryProvider's getSocketFactory() function.
There was a problem hiding this comment.
So what i can understand is, we can override the getSocketFactory in c4PeerDiscovery.hh in BluetoothProvider
and then we don't need to register it, right?
There was a problem hiding this comment.
That is right. We don't need to register.
| factory.context = context; | ||
|
|
||
| C4Socket* c4socket = c4socket_fromNative(factory, context, &addr); | ||
| c4socket_retain(c4socket); |
There was a problem hiding this comment.
The socket needs to be wrapped with TLS enabled socket to get the TLS encryption. This is what is done in iOS but currently c4SocketFromNativeWithTLS () is in the TLSCodec.hh, and the header is not included in the package as a public header.
Ref<C4Socket> C4PeerDiscoveryProvider::createIncomingSocketWithTLS(C4SocketFactory const& factory,
void* nativeHandle,
C4Address const& address)
{
return net::c4SocketFromNativeWithTLS(factory, nativeHandle, address, _discovery.tlsContext().get());
}