CodenameOne ObjectiveC桥接:Honeywell Captuvo SL22扫码回调未触发问题
decoderDataReceived: Callback in Codename One Objc Bridge for Honeywell Captuvo SL22 Let’s walk through the most likely causes and fixes for your issue—since the scanner connects and responds, the problem is almost certainly tied to how the delegate is set up in the Codename One bridge:
1. Mismatched Method Parameter Type
Your original Objective-C method expects an NSString* parameter, but you’re using Method.ArgType.CString in the bridge setup. CString maps to a raw char* in C, which doesn’t match the NSString* type the SDK is trying to pass. This signature mismatch means the runtime can’t find your callback method.
Fix this by switching to Method.ArgType.Object (since NSString is an Objective-C object) and converting it to a Java string with Objc.toString():
Objc.DelegateObject delegate = Objc.makeDelegate() .add("decoderDataReceived:", Method.create(Method.ArgType.Void, new Method.ArgType[]{Method.ArgType.Object}, args -> { String scanData = Objc.toString(args[0]); Log.p("Received " + scanData); return null; })); Objc.eval(captuvoShared, "addCaptuvoDelegate:", delegate);
2. Delegate Object Lifecycle (GC Recycling)
If your delegate variable is declared locally (e.g., inside a method), Codename One’s garbage collector might reclaim it right after you register the proxy. When the scanner tries to trigger the callback, the proxy object no longer exists.
Fix this by:
- Declaring the
delegateas a class-level member variable (so it stays in scope for the app’s lifecycle), or - Explicitly retaining the delegate with
Objc.retain(delegate)after registration:// Example using a class member variable private Objc.DelegateObject captuvoDelegate; // In your initialization code captuvoDelegate = Objc.makeDelegate() .add("decoderDataReceived:", Method.create(Method.ArgType.Void, new Method.ArgType[]{Method.ArgType.Object}, args -> { String scanData = Objc.toString(args[0]); Log.p("Received " + scanData); return null; })); Objc.eval(captuvoShared, "addCaptuvoDelegate:", captuvoDelegate);
3. Missing Protocol Declaration
Honeywell’s Captuvo SDK likely requires delegates to conform to the CaptuvoDelegate protocol. If you don’t specify this when creating the delegate, the Objective-C runtime might not recognize your proxy as a valid delegate.
Add the protocol name to Objc.makeDelegate():
Objc.DelegateObject delegate = Objc.makeDelegate("CaptuvoDelegate") .add("decoderDataReceived:", Method.create(Method.ArgType.Void, new Method.ArgType[]{Method.ArgType.Object}, args -> { String scanData = Objc.toString(args[0]); Log.p("Received " + scanData); return null; }));
4. Test with a Secondary Delegate Method
To confirm your proxy is registered correctly, add a simpler delegate method that the SDK calls when the decoder is ready. For example:
Objc.DelegateObject delegate = Objc.makeDelegate("CaptuvoDelegate") .add("decoderReady:", Method.create(Method.ArgType.Void, new Method.ArgType[]{Method.ArgType.Object}, args -> { Log.p("Decoder is ready for scanning!"); return null; })) .add("decoderDataReceived:", Method.create(Method.ArgType.Void, new Method.ArgType[]{Method.ArgType.Object}, args -> { String scanData = Objc.toString(args[0]); Log.p("Received " + scanData); return null; }));
If the decoderReady: log shows up, your proxy is working, and the issue is isolated to the decoderDataReceived: setup.
Start with the parameter type fix first—it’s the most common culprit here, and should resolve the callback issue in most cases.
内容的提问来源于stack exchange,提问作者NvServices

