使用串行蓝牙连接设备时出现问题

2024-03-03

我面临两个与常规蓝牙相关的问题。这是我的代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self    selector:@selector(showElements) userInfo:nil repeats:NO]; 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(accessoryConnected:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(accessoryDisconnected:) name:EAAccessoryDidConnectNotification object:nil];    
    [[EAAccessoryManager sharedAccessoryManager]registerForLocalNotifications];
}

-(void)showElements{
    [[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:nil completion:^(NSError *error) {
        if (error) {
            NSLog(@"error :%@", error);
        }
        else{
            NSLog(@"Its Working");
        }
    }];    
}

- (void)accessoryConnected:(NSNotification *)notification
{    
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];

}

1)建立连接后出现此错误。

error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"

这是完整的日志:-

BTM: attaching to BTServer
BTM: setting pairing enabled
BTM: found device "ESGAA0010" 00:04:3E:95:BF:82
BTM: disabling device scanning
BTM: connecting to device "ESGAA0010" 00:04:3E:95:BF:82
BTM: attempting to connect to service 0x00000080 on device "ESGAA0010" 00:04:3E:95:BF:82
BTM: connection to service 0x00000080 on device "ESGAA0010" 00:04:3E:95:BF:82 succeeded
BTM: setting pairing disabled
error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"

您可以看到日志的最后一行,它显示错误。当我搜索并发现苹果文档显示错误意味着找不到设备(EABluetoothAccessoryPickerResultNotFound),但是如果未找到,它如何在日志中显示其已连接。

2) accessoryConnected:方法没有被调用。这很可能是因为第一个问题。但我认为这里值得一提。

我添加了ExternalAccessory 框架并且设备符合MFI 标准。 帮我解决这些问题。谢谢


我今天遇到了同样的问题。解决方案很简单,您需要在 .plist 文件中添加额外的行。

<key>UISupportedExternalAccessoryProtocols</key>
<array>
    <string>YOUR_DEVICE_PROTOCOL</string>
</array>

如果设备添加到MFi计划 https://en.wikipedia.org/wiki/MFi_Program它应该有自己的协议。检查您的设备文档或询问设备创建者。

Edit

[[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:nil completion:^(NSError *error) {
    if (error) {
        NSLog(@"error :%@", error);
    }
    else{
        NSLog(@"Its Working");
    }
}]; 

错误是实例EABluetoothAccessoryPickerError。存在一个可能性值:

public enum Code : Int {
    public typealias _ErrorType = EABluetoothAccessoryPickerError

    case alreadyConnected
    case resultNotFound
    case resultCancelled
    case resultFailed
}

你的错误代码是1所以resultNotFound。请注意,当您修复 .plist 文件时showBluetoothAccessoryPickerWithNameFilter有时返回错误代码 = 0。那么就没有错误,因为您的设备是case alreadyConnected。我添加此信息是因为在发现这一点之前我浪费了很多时间。 :)

祝你好运。

编辑(斯威夫特 3.0)

EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil) { (error) in
    if let error = error {
        switch error {
        case EABluetoothAccessoryPickerError.alreadyConnected:
            break
        default:
            break
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用串行蓝牙连接设备时出现问题 的相关文章

随机推荐