iOS 蓝牙扫描枪功能

前面有一篇有关蓝牙扫描枪实现原理的笔记 iOS 蓝牙扫描枪
这里用UITextField这个控件来实现蓝牙扫描的功能

注意

这篇有问题,并非所有的扫码枪扫描结果最后都会携带 /n ,所以使用- (BOOL)textFieldShouldReturn:(UITextField *)textField 来监听扫描结果是有疏漏的,建议在实时输入回调的监听里做延迟接收扫描结果,然后处理。

话不多说,直接上代码

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *scanerTextField;

@end

@implementation ViewController

- (UITextField *)scanerTextField {
    if (!_scanerTextField) {
        _scanerTextField = [[UITextField alloc] init];
        _scanerTextField.delegate = self;
        _scanerTextField.spellCheckingType = UITextSpellCheckingTypeNo;
        _scanerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        // > iOS 9.0,隐藏键盘工具栏
        #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
            if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 9.0) {
                _scanerTextField.inputAssistantItem.leadingBarButtonGroups = @[];
                _scanerTextField.inputAssistantItem.trailingBarButtonGroups = @[];
            }
        #endif
    }
    return _scanerTextField;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.scanerTextField.frame = CGRectZero;      // 可设置frame为(0,0,0,0)
    [self.scanerTextField becomeFirstResponder];    // 设置成第一响应中,才会接收外接设备的输入
    [self.view addSubview:self.scanerTextField];    // 如果不添加到试图层的话,不会相应外接的输入
}

#pragma mark - UITextFieldDelegate

// 蓝牙扫描结果会通过此方法回调
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    NSLog(@"%@",textField.text);
    
    return YES;
}

@end

思路很简单,就是使用UITextField这个控件来接收外接输入,蓝牙扫描枪相当于一个外接键盘。

之所以使用UITextField而不使用UITextView的原因时,使用UITextView的话,当iPad外接键盘和蓝牙扫码枪同时使用时,会有冲突,为了避免冲突使用UITextField,在- textFieldShouldReturn: 这个回调里处理扫描结果。

  • 关于系统键盘

有时候使用蓝牙扫描枪的时候,需要隐藏系统键盘,只是不显示系统键盘,可以做如下处理

 UIView *inputView = [[UIView alloc] initWithFrame:CGRectZero];
 _scanerTextField.inputView = inputView;

这样当激活 scanerTextField 成为第一响应者时,就不会弹出键盘了。

代码如下:

- (UITextField *)scanerTextField {
    if (!_scanerTextField) {
        _scanerTextField = [[UITextField alloc] init];
        _scanerTextField.delegate = self;
        _scanerTextField.spellCheckingType = UITextSpellCheckingTypeNo;
        _scanerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        // > iOS 9.0,隐藏键盘工具栏
        #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
            if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 9.0) {
                _scanerTextField.inputAssistantItem.leadingBarButtonGroups = @[];
                _scanerTextField.inputAssistantItem.trailingBarButtonGroups = @[];
            }
        #endif
        // 不显示系统键盘
        UIView *inputView = [[UIView alloc] initWithFrame:CGRectZero];
        _scanerTextField.inputView = inputView;
        [inputView release];
    }
    return _scanerTextField;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.text && textField.text.length) {
    	// 处理扫描事件
    	...
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(setScannerKeyboardEditable) withObject:nil afterDelay:0.8];
}

- (void)setScannerKeyboardEditable {
    if (_scanerEnable) {
    	self.scanerTextField.enabled = YES;
    	if (self.scanerTextField != nil && [self.scanerTextField canBecomeFirstResponder] && ![self.scanerTextField isFirstResponder]) {
        	[self.scanerTextField becomeFirstResponder];
    	}
    }
    else {
    	self.scanerTextField.enabled = NO;
    }
}

目前测试并没发现什么其他问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值