简体   繁体   English

连接的蓝牙模块未在EAAccessoryManager的connectedAccessory中显示

[英]Connected Bluetooth Module not shown in EAAccessoryManager's connectedAccessory

I'm working on an app use Bluetooth 2.1. 我正在使用蓝牙2.1的应用程序。 Each time the app becomes active, it will open an EASession. 每次应用程序变为活动状态时,它都会打开一个EASession。 Each time the app go to background or terminate, EASession closed. 每次应用程序进入后台或终止时,EASession都会关闭。

If the bluetooth module is connected, and app is started fresh, it opens alright, send/get data all working. 如果已连接蓝牙模块,并且应用程序已启动,则打开正常,发送/获取数据全部正常工作。

But if bluetooth module lose communication when app is running, after it's reconnected and app is opened from background. 但是,如果蓝牙模块在应用程序运行时失去通信,则在重新连接并从后台打开应用程序之后。 EASession open fails. EASession打开失败。

And there are no connected accessory listed. 并且没有列出连接的配件。

  • (EAAccessory *)selectedAccessory (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;

} }

Here [_accessoryList count] is 0. 这里[_accessoryList count]为0。

Does anyone know what's going on? 有谁知道发生了什么? Thanks! 谢谢!

EDIT: 编辑:

I have run more tests, and this time it looks different. 我已经运行了更多测试,这次看起来不同了。

- (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);
}

This time selectedAccessory seems fine. 这次选择的配件看起来很好。 But it cannot init session. 但它无法初始化会话。

After initWithAccessory line, it prints out ERROR: 在initWithAccessory行之后,它打印出ERROR:

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(支持的外部附件协议部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM