iOS蓝牙--多设备连接与后台运行

iOS蓝牙--多设备连接与后台运行_第1张图片

一、同时连接多个设备,并读取信号强度(RSSI)

iOS蓝牙支持一个中心连接多个蓝牙设备,最多7个。根据CoreBluetooth框架中读取RSSI是由外设读取的

iOS蓝牙--多设备连接与后台运行_第2张图片

即 只有当中心与外设连接成功之后,并且调用readRSSI才会执行CBPeripheral的代理peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error;


iOS蓝牙--多设备连接与后台运行_第3张图片

就是说当你保存了所有已连接的peripheral,然后需要刷新的时候,每一个peripheral发送readRSSi,在从这个回调里头返回RSSI值,然后进行一步操作。

因此需要先连接起设备:如果是确定的两个设备就需要定义两个peripheral 

CBPeripheral *_peripheralA;

CBPeripheral *_peripheralB;

如果peripheral数量比较多,或者确定个数,可以用一个数组来表示

连接设备的方法是 

[self.centralManager connectPeripheral:peripheral options:nil]; 

每一个peripheral对应一个设备,每一个设备都有一个标识,即peripheral.identifier 需要保存下来。根据这个标识,匹配到对应的设备

1、扫描搜索

//蓝牙开启状态时搜索周边设备

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:UUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]}];

还有另外一个情况,就是如果uuid不确定,只知道蓝牙名称,该怎么处理呢? 这时候需要对搜索到的周围所有的设备进行过滤处理。注意 此时扫描搜索就改为周围所有设备均可搜索到

[self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]}];

在扫描到设备的方法回调centralManager: didDiscoverPeripheral:l advertisementData: RSSI:中处理

//如果外设数组数量为0则直接将该model添加到外设数组中

//如果外设数组数量不为0则用遍历数组用外设的名称进行判断是否存在于该数组中

//如果外设名称相同则只修改该外设所对应的rssi

//如果外设名称不同则将此外设加入到外设数组中

if(peripheral ==nil) {

      [_peripheralMarr removeAllObjects];

}

if(peripheral.name!=nil) {

     Model* model = [[Modelalloc]init];

     model.peripheral= peripheral;

     model.num= [RSSI intValue];

    if(_peripheralMarr.count==0) {

       [_peripheralMarr addObject:model];

      }else{

      BOOL  ishave =NO;

      for(Model* mo in_peripheralMarr) {

       if([mo.peripheral.nameisEqualToString:model.peripheral.name]) {

       mo.num= model.num;

        ishave =YES;

        break; }}

 if(ishave ==NO) {  [_peripheralMarr  addObject:model];}}}

此时 找到了所需要找的设备,保存在_peripheralMarr中

需要连接的时候

//遍历_peripheralMarr

for(Model *model in _peripheralMarr) {

CBPeripheral  *perip = mo.peripheral;

[self.centralManager connectPeripheral:perip options:nil];

perip.delegate =self;

[self.perip discoverServices:nil];//告诉外围设备,谁与外围设备连接

}

已经连接蓝牙成功后的设备的rssi在readRSSI后会自动执行下面的协议方法-(void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error

在开始扫描设备结束之后,执行分别执行readRSSI

-(void)scanForPeripherals{

if(self.centralManager.state == CBCentralManagerStatePoweredOn) {

//蓝牙开启状态时搜索周边设备

[self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]}];

}

if(_peripheralMarr.count!=0) {

for(Model *mo in _peripheralMarr) {

CBPeripheral *peri = mo.peripheral;

[peri readRSSI];}}

     2、读取RSSI值

最后是最关键的部分,在代理中读取rssi的值

#pragma mark--------连接成功后执行以下协议--------------

-(void)peripheral:(CBPeripheral*)peripheral didReadRSSI:(NSNumber*)RSSI error:(NSError*)error{

        if(peripheral.name!=nil) {

              Model* model = [[Modelalloc]init];

              model.peripheral= peripheral;

              model.num= [RSSIintValue];

              for(Model* mo in _peripheralMarr) {

                    if([mo.peripheral.name isEqualToString:model.peripheral.name]) {

                    mo.num= model.num;

                   NSLog(@"已连接的外设当前rssi==%d",mo.num);

                  break;}}}}


3、持续监测RSSI信号

     对于持续监测,可以选择一个定时器进行操作

_scanPeripheralTimer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(readRSSI)userInfo:nilrepeats:YES];

[[NSRunLoopcurrentRunLoop]addTimer:_scanPeripheralTimerforMode:NSDefaultRunLoopMode];

  这样处理有一个弊端,就是当设备连接不稳定的时候,会断开,这样,就不能读取到稳定的型号。因此,需要重复扫描,当设备断开连接时。也可以重新扫描连接。继续读取RSSI。

_scanPeripheralTimer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(scanForPeripherals)userInfo:nilrepeats:YES];

[[NSRunLoopcurrentRunLoop]addTimer:_scanPeripheralTimerforMode:NSDefaultRunLoopMode];

二、后台长时间执行

后台长时间执行需要开启Background Modes 


iOS蓝牙--多设备连接与后台运行_第4张图片

然后在Appdelegate.m中的- (void)applicationDidEnterBackground:(UIApplication*)application方法中执行相关操作

UIDevice*device = [UIDevice currentDevice];

BOOL  backgroundSupported =NO;

       if([devicerespondsToSelector:@selector(isMultitaskingSupported)]) {

                backgroundSupported =YES;

          }

__block   UIBackgroundTaskIdentifier  bgTaskId = [applicationbeginBackgroundTaskWithExpirationHandler:^{

[applicationendBackgroundTask:bgTaskId];

bgTaskId =UIBackgroundTaskInvalid;

}];

if(backgroundSupported) {

__block int  i =0;//这个值是用来测试后台用运行情况,

timer= [NSTimerscheduledTimerWithTimeInterval:1repeats:YESblock:^(NSTimer*_Nonnulltimer) {

//执行蓝牙相关操作...

NSLog(@"%d",i ++);

}];

[[NSRunLoopcurrentRunLoop]addTimer:timerforMode:NSDefaultRunLoopMode];

[timerfire];

}

然后该如何让蓝牙设备在后台运行的时候,同样搜索扫描设备呢,我自己的做法是用通知中心,

注册通知中心的方法在执行蓝牙设备的页面调用

[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(a_scanForPeripherals) name:@"scanForPeripherals"object:nil];

在-applicationDidEnterBackground:的定时器的方法中 执行

[[NSNotificationCenterdefaultCenter]postNotificationName:@"scanForPeripherals"object:nil];

就可以得到长时间在后台运行蓝牙了。

这里需要注意的是,如果蓝牙在前台没有连接成功,在后台是无法读取到RSSI值的。

你可能感兴趣的:(iOS蓝牙--多设备连接与后台运行)