How do you read parameters in a callback function?

Open
#37 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Active
Tech stack
objective-c, zig
Domain
mobile-dev

Research direction

Start with the didDiscoverPeripheral callback and its class.addMethod registration, then read the referenced src/msg_send.zig implementation around line 283. Document how the Objective-C selector maps to callback parameters and how the discovered values can be accessed. Done means a clear example answers the parameter and object-extraction questions.

Written by the indexing model from the issue text.

Description

I've been poking around the code and struggling to work out how to read the parameters in a callback function handler. Given I cant find an example, I am not sure where to start. I am at the point where I am able to get the objc bluetooth api to report back discovery of bluetooth devices. But how do I extract the information?

(My long term goal is to eventually have a zig cross platform bluetooth api, just working on it on the side when I have time.)

//- (void)centralManager:(CBCentralManager *)central
// didDiscoverPeripheral:(CBPeripheral *)peripheral
//     advertisementData:(NSDictionary<NSString *,id> *)advertisementData
//                  RSSI:(NSNumber *)RSSI {
fn didDiscoverPeripheral(
    _: objc.c.id, // What are these for?
    _: objc.c.SEL, // What are these for?
    centralId: objc.c.id, // Do you add parameters like this?
    peripheralId: objc.c.id, // Do you add parameters like this?
) callconv(.c) void {
    //    NSLog(@"Discovered device: %@ (RSSI: %@)", peripheral.name ?: @"Unknown", RSSI);
    info("found device ", .{});

    info("   id: {any} {any} ", .{ centralId, peripheralId });

    // Just guessing
    const central = objc.Object.fromId(centralId);
    const peripheral = objc.Object.fromId(peripheralId);
    info("   obj: {any} {any} ", .{ central, peripheral });

    // Just guessing
    const name = peripheral.msgSend(objc.Object, "name", .{});
    info("   name: {any} ", .{name});
}

For reference, here is how I am setting up this callback handler to be called:

        // Register a CBResponder to handle bluetooth callback/events.
        const class = objc.allocateClassPair(objc.getClass("NSObject"), "CBResponder") orelse return error.ObjCFailure;

        // https://github.com/mitchellh/zig-objc/blob/c8de82ff80281215ad92900866dab7103a8efa8b/src/msg_send.zig#L283
        _ = class.addMethod("centralManagerDidUpdateState:", updateState);
        _ = class.addMethod("centralManager:didDiscoverPeripheral:advertisementData:RSSI:", didDiscoverPeripheral);
        const protocol = objc.getProtocol("CBCentralManagerDelegate") orelse return error.ObjCFailure;
        _ = objc.c.class_addProtocol(class.value, protocol.value);
        objc.registerClassPair(class);
        const class_obj = class.msgSend(objc.Object, "alloc", .{});

        //_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
        const bluetoothManager = objc.getClass("CBCentralManager") orelse return error.ObjCFailure;
        const bluetoothManagerAlloc = bluetoothManager.msgSend(objc.Object, "alloc", .{});
        bt_global = bluetoothManagerAlloc.msgSend(objc.Object, "init", .{});
        bt_global.?.setProperty("delegate", class_obj);
Dominant language
Zig
Stars
351
Forks
48
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More Mobile Dev issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.