Closed vectorinkllc closed 2 weeks ago
Hi @vectorinkllc, Did you follow the wiki steps to integrate our SDK, and how did you connect the Lamy stylus?
Yes I followed the wiki steps. And my implementation does work, but only after I connect via the example App that came with the SDK and rebuild my app in xcode. Then when I press the button to connect the pen in my app, the LamyStylusManagerDidChangeConnectionStatus
event gets fired and I'm able to use all of the pens features in my app. But I can only get this to work if I open the example app first, connect, and then re-build my app in xcode. If I dont connect with the example app first LamyStylusManagerDidChangeConnectionStatus
never fires.
This issue is very strange, I know I sound totally mad just saying this but I figured this explanation might help you recognize exactly what my issue it. I don't thinks its a code issue, because it works (When it works), something it stopping LamyStylusManagerDidChangeConnectionStatus
from firing after I enable the SDK.
My code is pretty straight forward, I have delegate that calls the methods on this LamySDKWrapper and handles the UI stuff
Here's the LamySDKWrapper.m `// LamySDKWrapper.m
@implementation LamySDKWrapper
(instancetype)sharedInstance { static LamySDKWrapper *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; }
(BOOL)isEnabled { return [[LamyStylusManager sharedInstance] isEnabled]; }
(void)startSDK { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleButton1Down) name:@"LamyStylusButton1Down" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleButton1Up) name:@"LamyStylusButton1Up" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleButton2Down) name:@"LamyStylusButton2Down" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleButton2Up) name:@"LamyStylusButton2Up" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionChange:) name:LamyStylusManagerDidChangeConnectionStatus object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showRecommendString:) name:@"LamyStylusNotificationRecommend" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelNormal:) name:@"LamyStylusNotificationBatteryLevelNormal" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelLow:) name:@"LamyStylusNotificationBatteryLevelLow" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelCritical:) name:@"LamyStylusNotificationBatteryLevelCritical" object:nil];
[[LamyStylusManager sharedInstance] startDiscoveryAndImmediatelyConnect:YES withDiscoveryBlock:nil completionBlock:^(BOOL success, NSError *error){ NSLog(@"startDiscoveryAndImmediatelyConnect: Connected!"); }]; }
(BOOL)enableSDK { __block BOOL success = NO; dispatch_sync(dispatch_get_main_queue(), ^{ @try { [[LamyStylusManager sharedInstance] enable]; success = YES; } @catch (NSException exception) { // error = [NSError errorWithDomain:@"com.yourdomain.lamysdk" code:100 userInfo:@{NSLocalizedDescriptionKey: exception.reason}]; NSLog(@"Error enabling LAMY SDK: %@", exception.reason); success = NO; } }); return success; }
(void)disableSDK { @try { [[LamyStylusManager sharedInstance] disable]; } @catch (NSException exception) { // error = [NSError errorWithDomain:@"com.yourdomain.lamysdk" code:100 userInfo:@{NSLocalizedDescriptionKey: exception.reason}]; NSLog(@"Error enabling LAMY SDK: %@", exception.reason); } }
(void)handleButton1Down { [self.delegate stylusButton1Down]; }
(void)handleButton1Up { [self.delegate stylusButton1Up]; }
(void)handleButton2Down { [self.delegate stylusButton2Down]; }
(void)handleButton2Up { [self.delegate stylusButton2Up]; }
(void)connectionChange:(NSNotification )note { NSNumber statusNumber = note.userInfo[LamyStylusManagerDidChangeConnectionStatusStatusKey]; LamyConnectionStatus status = [statusNumber unsignedIntegerValue]; [self.delegate connectionStatusChanged:status]; }
(void)showRecommendString:(NSNotification )note { NSString recommend = note.userInfo[LamyStylusNotificationRecommendKey]; [self.delegate showRecommendString:recommend]; }
(void)batteryLevelNormal:(NSNotification *)note { [self.delegate batteryLevelChanged:@"normal"]; }
(void)batteryLevelLow:(NSNotification *)note { [self.delegate batteryLevelChanged:@"low"]; }
(void)batteryLevelCritical:(NSNotification *)note { [self.delegate batteryLevelChanged:@"critical"]; }
@end `
Ok, after re-installing the SDK framework, now I can't get it to work at all (even after connecting to the example app). So that tells me that the times where it did connect were probably some freak accidents that only happened at random chance? When I first implemented the SDK it worked every single time I tested with no problem. Now it doesn't work at all, with the same code in place. The bare bones of the integration is simple:
[[LamyStylusManager sharedInstance] enable];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionChange:) name:LamyStylusManagerDidChangeConnectionStatus object:nil];
Am I missing anything?
This was working flawlessly before. So I guess the questions is, what would cause [[LamyStylusManager sharedInstance] enable];
to fail at enabling the SDK and thus firing LamyStylusManagerDidChangeConnectionStatus events?
Ok after taking everything apart and putting back together piece by piece I was able to confirm that somehow my app was indeed piggy backing on the bluetooth connection from the example app this whole time.
I proved that by removing the Privacy - Bluetooth Always Usage Description
from my info.plist. Doing so causes the app to crash if I try to enable the SDK because bluetooth isn't allowed. Then I connected the pen with the example app, then I opened my app again and it connected successfully even though bluetooth is not allowed.
So no matter if I call enable this way
DispatchQueue.main.async { if let stylusManager = LamyStylusManager.sharedInstance() { stylusManager.enable() } }
or this way
[[LamyStylusManager sharedInstance] enable];
It doesn't work unless I connect in the example swift app first.
I was trying to use my own custom UI this whole time so what I'm going to try next is integrating the ViewController that comes with UI Bundle and see if that makes a difference.
Hi @vectorinkllc In your case, it may be caused by the Bluetooth reconnection mechanism.
Do your apps have a UI like the connect stylus in the example app?
when you call "startDiscoveryAndImmediatelyConnect" this function, you should tell the user to press the tip. and you should receive LamyStylusManagerDidChangeConnectionStatus event.
That did it! "startDiscoveryAndImmediatelyConnect" is all I needed. Thanks!
This is a very strange issue I'm running into.
Calling this:
[[LamyStylusManager sharedInstance] enable];
Allows this event to observer to communicate to my app the status of the Lamy hardware.
And it works, only if I first connect to the Example provided in the SDK download. If I reboot my iPad and run my app by itself, the
LamyStylusManagerDidChangeConnectionStatus
event never fires and the SDK never gets enabled.Any ideas on what I might be missing? Is there more to enabling the SDK than just calling
[[LamyStylusManager sharedInstance] enable];
?