ios - 连接的蓝牙模块未显示在 EAAccessoryManager 的连接附件中

标签 ios objective-c eaaccessory

我正在开发一个使用蓝牙 2.1 的应用程序。每次应用程序激活时,它都会打开一个 EASession。每次应用程序进入后台或终止时,EASession 都会关闭。

如果蓝牙模块已连接,并且应用程序重新启动,它可以正常打开,发送/获取数据一切正常。

但如果蓝牙模块在应用程序运行时失去通信,则在重新连接并从后台打开应用程序后。 EASession 打开失败。

并且没有列出连接的配件。

  • (EAAccessory *)selectedAccessory

{

 if (_selectedAccessory == nil)

 {

      _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];

      NSLog(@"accessory count: %d", [_accessoryList count]);

      if ([_accessoryList count])

      {

           _selectedAccessory = [_accessoryList objectAtIndex:0];

           NSArray *protocolStrings = [_selectedAccessory protocolStrings];

           self.protocolString = [protocolStrings objectAtIndex:0];

      }else{

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"You don't have any machine connected.\nPlease go to Settings->Bluetooth to set up connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

        [alert show];





    }

 }

 return _selectedAccessory;

此处 [_accessoryList count] 为 0。

有人知道这是怎么回事吗? 谢谢!

编辑:

我跑了更多的测试,这次看起来不一样。

- (BOOL)openSession
{
    if (_session == nil)
    {
        NSLog(@"EAController::openSession");
        [_selectedAccessory setDelegate:self];
        _session = [[EASession alloc] initWithAccessory:[self selectedAccessory] forProtocol:_protocolString];

        if (_session)
        {
            [[_session inputStream] setDelegate:self];
            [[_session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [[_session inputStream] open];

            [[_session outputStream] setDelegate:self];
            [[_session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [[_session outputStream] open];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setBool:YES forKey:Unit_Has_Connection];
            NSLog(@"opened the session");
        }
        else
        {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setBool:NO forKey:Unit_Has_Connection];
            NSLog(@"creating session failed");
        }
    }

    return (_session != nil);
}

这次 selectedAccessory 看起来不错。但是它不能初始化 session 。

在 initWithAccessory 行之后,打印出错误:

2013-06-17 15:03:39.967 IceMachine[770:60b] ERROR - opening session failed
2013-06-17 15:03:39.968 IceMachine[770:60b] ERROR - /SourceCache/ExternalAccessory/ExternalAccessory-237/EASession.m:-[EASession dealloc] - 139 unable to close session for _accessory=0x15543fe0 and sessionID=65536

最佳答案

您必须手动将 _protocolString 的值添加到应用的 info.plist(支持的外部附件协议(protocol)部分)。

关于ios - 连接的蓝牙模块未显示在 EAAccessoryManager 的连接附件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17155610/

相关文章:

objective-c - EAAccessoryManager notifyObserversThatAccessoryDisconnectedWithUserInfo 崩溃

javascript - 在 iOS 的 UIAutomation UIATarget.onAlert 中处理自定义 javascript 异常

ios - 使用 .txt 文件在 iOS 中存储数据?

objective-c - React Native 如何读取/观察原生 Objective-C 模型数据?

ios - UIDocumentPickerViewController:从 Dropbox 打开文件夹

IOS:如何检测配对的蓝牙 3.0 设备的品牌/型号?

ios - 要通过蓝牙与设备通信,我必须使用哪个配置文件

ios - 如何为不同状态的 Storyboard中的 UIButton 设置布局?

ios - 多个 URL 的数组 NSFileManager Swift

ios - 打开控制中心时CGImageCreate : invalid image size 0 x 0,