使用串行蓝牙连接设备时出现问题 [英] Getting Issues while connecting device with serial Bluetooth

查看:1201
本文介绍了使用串行蓝牙连接设备时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am facing 2 problems related to regular Bluetooth.Here is my code.

- (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)连接建立后我收到此错误。

1) I am getting this error after connection got established.

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

这是完整的日志: -

Here is the full log:-

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 ),但如果没有找到它,如何显示它的连接。

you can see the last line of log, its showing error. As i searched and found that apple documentation says the error means device not found(EABluetoothAccessoryPickerResultNotFound), but how come in log it shows its connected if its not found.

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

2) accessoryConnected: method not getting called. Its most probably because of first issue. But i thought its worth mentioning here.

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

I have added ExternalAccessory framework and device is MFI compliant. Help me to fix these. Thanks

推荐答案

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

I met the same problem today. Solution is easy, you need to add extra row to your .plist file.

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

如果将设备添加到 MFi计划它应该有自己的协议。检查您的设备文档或询问设备创建者。

If device is added to MFi Program it should have own protocol. Check your device documentation or ask device creators.

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

错误是 EABluetoothAccessoryPickerError 的实例。有可能值:

The error is instance of EABluetoothAccessoryPickerError. There is a possibility values:

public enum Code : Int {
    public typealias _ErrorType = EABluetoothAccessoryPickerError

    case alreadyConnected
    case resultNotFound
    case resultCancelled
    case resultFailed
}

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

Your error code is 1 so resultNotFound. Note that when you fix .plist file showBluetoothAccessoryPickerWithNameFilter sometimes return error code = 0. Then there is no error because your device is case alreadyConnected. I add this information because I lost a lot of time before I detected this. :)

祝你好运。

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

这篇关于使用串行蓝牙连接设备时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆