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

Shi*_*aay 11 objective-c external-accessory ios mfi

我面临着与常规蓝牙相关的2个问题.这是我的代码.

- (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];

}
Run Code Online (Sandbox Code Playgroud)

1)连接建立后我收到此错误.

error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"
Run Code Online (Sandbox Code Playgroud)

这是完整的日志: -

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)"
Run Code Online (Sandbox Code Playgroud)

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

2)accessoryConnected:方法没有被调用.它最有可能是因为第一期.但我认为这里值得一提.

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

Kam*_*icz 10

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

<key>UISupportedExternalAccessoryProtocols</key>
<array>
    <string>YOUR_DEVICE_PROTOCOL</string>
</array>
Run Code Online (Sandbox Code Playgroud)

如果设备被添加到MFi程序,它应该有自己的协议.检查设备文档或询问设备创建者.

编辑

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

错误是实例EABluetoothAccessoryPickerError.有可能值:

public enum Code : Int {
    public typealias _ErrorType = EABluetoothAccessoryPickerError

    case alreadyConnected
    case resultNotFound
    case resultCancelled
    case resultFailed
}
Run Code Online (Sandbox Code Playgroud)

您的错误代码是1 resultNotFound.请注意,当您修复.plist文件时,showBluetoothAccessoryPickerWithNameFilter有时会返回错误代码= 0.然后没有错误,因为您的设备是case alreadyConnected.我添加此信息是因为在检测到此信息之前我丢失了很多时间.:)

祝好运.

编辑(Swift 3.0)

EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil) { (error) in
    if let error = error {
        switch error {
        case EABluetoothAccessoryPickerError.alreadyConnected:
            break
        default:
            break
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


gon*_*ins 1

尝试进入 iOS 蓝牙设置并取消设备配对,然后再次配对。我之前遇到过“305”错误,问题是我已经配对了设备,然后更新了设备的固件。此后,直到我将设备从 iPhone 上移除并在设备固件更新重新配对后,它才会再次连接。

这可能对你不起作用,但互联网上没有太多关于 305 错误的信息,所以希望这至少能帮助别人。