Mac IOBluetooth binding No delegate calls

I am trying to send and receive data through a Bluetooth RFCOMM connection.

Server side is working fine through bluer on a RaspberryPI. I tested it with an Ubuntu client also using bluer. It is a simple echo server anyway. Where the message length is the first four bytes.

Now I am trying to implement a client for MacOS using objc and some bindings made by apple-bindgen.

Making the RFCOMM connection succeeds, sending a message to the server works too. And it is received at the server. But I can't get to the echoed message that the server sends.

The server does send the message I am pretty sure because all works fine with the Ubuntu client. So my client implementation is at fault most likely.

There was a similar question here also about the delegate not getting called. I looked through that to make sure I tried all the things he had to do to make it work. I also looked at his working repo but I can't figure out what I am doing wrong.

Any help would be really appreciated!
Here is a GitHub link to my simplified client: GitHub - pYtoner/mac_bt

I managed to get the delegate to be called but only because I used cc to compile in some objective-c code. One step at a time..

Finally got it working:

extern "C" fn message_received(
    _: &Object,
    _cmd: Sel,
    _channel: *mut Object,
    data_ptr: *mut Object,
    len: size_t,
) {
    let data_ptr = data_ptr as *const u8;
    let received: Vec<u8> = (0..len)
        .map(|i| {
            let char = unsafe { data_ptr.add(i) };
            let value = unsafe { *char };
            value
        })
        .collect();

    println!("Received: {:?}", received);
}

fn get_delegate() -> *mut Object {
    let superclass = class!(NSObject);
    let mut decl = ClassDecl::new("CollectorDelegate2", superclass).unwrap();

    unsafe {
        decl.add_method(
            sel!(rfcommChannelData:data:length:),
            message_received
                as extern "C" fn(&Object, Sel, _: *mut Object, _: *mut Object, _: size_t),
        );
    }

    decl.register();

    let delegate: *mut Object = unsafe { msg_send![class!(CollectorDelegate2), alloc] };
    let delegate: *mut Object = unsafe { msg_send![delegate, init] };
    delegate
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.