Captuvo iPod条形码扫描仪在睡眠后显示不兼容的固件

时间:2014-05-08 16:35:37

标签: ios barcode-scanner

我正在尝试使用Captuvo条码扫描底座,使用Captuvo SDK 22和iOS 7来实现小型iPod应用程序。该应用程序会收到一个条形码并将其发送到webview。

应用程序类型的工作。我能够连接到设备并扫描条形码。

然而,在应用程序休眠(转到后台)后,在下次唤醒时,扫描程序出现错误并显示“不兼容的固件”。

奇怪的是,我可以退出应用程序,再次启动它,设备将连接而不会出错。

我到目前为止最接近的方法是在应用程序通过实施预先启动时尝试重新启动扫描程序:

UIApplicationWillEnterForegroundNotification

它疯狂地接近工作。这是我第一次尝试iOS开发,所以也许我遗漏了一些关于应用程序生命周期的基本知识。在官方SDK之外,关于如何编程Captuvo的文档相对较少,所以我对任何建议都感兴趣。

以下是完整的代码:

#import "MyViewController.h"
#import "Captuvo.h"

@interface MyViewController ()

@property (nonatomic, copy) NSString *barcode;    
@property (nonatomic, weak) IBOutlet UIWebView *webView;

@end

@implementation MyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *strURL = @"http://myurl.com/scan.php";
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
}

- (void)viewWillAppear:(BOOL)animated
{
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleForeground)
                                                 name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)handleForeground {
    [self.webView reload];
    [self initCaptuvoSDK];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)initCaptuvoSDK
{
    [[Captuvo sharedCaptuvoDevice] removeCaptuvoDelegate:self];
    [[Captuvo sharedCaptuvoDevice] addCaptuvoDelegate:self];
    [[Captuvo sharedCaptuvoDevice] startDecoderHardware];
    [[Captuvo sharedCaptuvoDevice] stopDecoderHardware];
    ProtocolConnectionStatus connectionStatus = [[Captuvo sharedCaptuvoDevice] startDecoderHardware];

    NSString *message;
    switch (connectionStatus) {
        case ProtocolConnectionStatusConnected:
            message = @"Connected!";
            break;
        case ProtocolConnectionStatusAlreadyConnected:
            message = @"Already Connected!";
            break;
        case ProtocolConnectionStatusBatteryDepleted:
            message = @"Battery depleted!";
            break;
        case ProtocolConnectionStatusUnableToConnect:
            message = @"Error connecting!";
            break;
        case ProtocolConnectionStatusUnableToConnectIncompatiableSledFirmware:
            message = @"Incompatible firmware!";
            break;
        default:
            message = @"Other!";
            break;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:message
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"Okay"
                                          otherButtonTitles:@"Okay", nil];
    [alert show];
}

- (void)decoderReady
{
    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [[Captuvo sharedCaptuvoDevice] enableDecoderBeeperForGoodRead:YES persistSetting:NO] ;
        [[Captuvo sharedCaptuvoDevice]setDecoderSerialTriggerTimeoutInMilliSeconds:5000 persistSetting:NO];
    });
}

- (void)decoderDataReceived:(NSString *)data
{
    NSLog(@"decoderDataReceived");
    if(data != nil) {
        // send barcode to the webview
        NSString *str = [NSString stringWithFormat:@"processBarcode('%@');", data];
        [self.webView stringByEvaluatingJavaScriptFromString:str];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

1 个答案:

答案 0 :(得分:1)

我遇到了与你类似的问题。我修改了AppDelegate.m文件中的方法,如下所示。它对我有用。

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    [[Captuvo sharedCaptuvoDevice] stopDecoderHardware];
}
-(void)applicationDidBecomeActive:(UIApplication *)application
{
    [[Captuvo sharedCaptuvoDevice] startDecoderHardware];
}