How do you read parameters in a callback function?
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
client-controller-update ta-bot-triage team-money-movement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MetaMask/metamask-mobile#36594 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mozilla-mobile/firefox-ios#35743 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
hemilabs/ui-monorepo#2323 ·
-
Needs: Author Feedback Needs: Repro
Difficulty 1/5 Under an hour Newbie friendliness 92/100
react/react-native#58621 · 1 comment ·