iOS:使用corebluetooth库从不同的视图控制器进行通信 [英] iOS:Communicate from different view controllers using corebluetooth library

查看:291
本文介绍了iOS:使用corebluetooth库从不同的视图控制器进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做corebluetooth应用程序。并使用corebluetooth框架开发了库。我有4个视图控制器。在第一个视图控制器中,当我单击扫描按钮时,我具有扫描按钮。第二个屏幕出现带有扫描设备的设备,并且这些设备出现在表格视图中。当我单击表格视图外围设备时,它会连接并提供其信息,例如服务和特性。当我回到第一个视图控制器时,我有一个名为电池状态的按钮,当我单击该按钮时,它应该从连接的第二个视图控制器中调用电池信息外围设备。
以下是我在第二个视图控制器中的代码

i am doing corebluetooth application. And developed the library using corebluetooth framework.i have 4 view controllers .In the first view controller i have scan button when i click scan button second screen appears with scanning devices and those devices are appeared in the tableview. when i click the table view peripheral connects and provides its info like services and charactestics.When i came back to the first view controller i had a button called battery status when i click that button it should call the battery information from the second view controller connected peripheral. Following is my code in second view controller

     -(void)viewdidload
     { coreBle = [[CoreBLE alloc] init];   
     coreBle.delegate = self;
      }

并用于连接设备

        - (IBAction)Go:(id)sender
      {
     if (periperal==nil) {
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil    
     message:@"Select watch to connect"
                                                delegate:self   
     cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
      }
    else{

   [coreBle connectToPeripheralAt:[periperal integerValue]];
    NSLog(@"peripheral connected is %@",periperal);
    HUD.labelText = @"Connecting...";

    [HUD showWhileExecuting:@selector(connectingtask) onTarget:self 
    withObject:nil animated:YES];

     }

     }

连接外围设备设备从以下位置选择了索引路径值的行

conncectng the peripheral device from did select row at index path value as below

    - (void)tableView:(UITableView *)tableView   
   didSelectRowAtIndexPath:(NSIndexPath   *)indexPath

    {

   deviceselected= [NSString stringWithFormat:@"Table cell pressed.   
   (%d)", indexPath.row];
   periperal=[NSString stringWithFormat:@"%d",indexPath.row];
    }

上面的代码用于在第二个视图控制器中连接外设。外设应该连接到第一个视图控制器中。以便我可以从中获取值。请帮助我

The above code is for connecting the peripheral in the second view controller .The same peripheral should connected in the first view controller .So that i can retrive values from it.Please help me

推荐答案

为什么不移动将CoreBLE代码单独添加到新类中的单个实例。在这四个视图控制器中,只需调用相关方法即可获取有关CoreBLE的信息。因为在ui操作中,您必须遵循四个视图控制器序列。

Why not move the CoreBLE code to a single instance alone in a new class. And in the four viewcontrollers just call the relative methods to get the information about the CoreBLE. Because in your ui operation, you must do followering the four view controllers sequence.

这篇关于iOS:使用corebluetooth库从不同的视图控制器进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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