美文网首页
iOS按信号强度对蓝牙列表排序

iOS按信号强度对蓝牙列表排序

作者: ___吉 | 来源:发表于2017-02-24 20:43 被阅读0次

    项目中有个需求,将搜索到的蓝牙外设列表,按信号强度进行排序展示出来,本着面向产品的编程思想,刀山火海也要上。。。
    思路就是:
    1 - 建立一个模型类,类中有外设名称、信号强度、服务数、广播等属性。
    2 - 用经典的tableview展示外设列表。就涉及到一个数据的数组了,这个数组中存放的就是上面蓝牙模型类对象。
    3 - 每次搜索到一个蓝牙外设,一般做法都是将其添加进数组中,然后刷tableview。在添加进去之前,要判断下是否已经添加过该设备,如有添加,则不再添加,而是将数组中的对应的外设模型对象刷新下即可。
    4 - 然后,将数组中模型对象,按信号属性排序,最后刷tableview即可实现列表按信号强度展示了

    下面是具体代码实现:
    1、创建蓝牙模型类

    ---.h---
    #import <Foundation/Foundation.h>
    #import <CoreBluetooth/CoreBluetooth.h>
    
    @interface JCBlutoothInfoModel : NSObject
    
    @property (nonatomic, strong) CBPeripheral *peripheral;
    @property (nonatomic, strong) NSDictionary *advertisementData;
    @property (nonatomic, strong) NSNumber *RSSI;
    @property (nonatomic, copy) NSString *adverMacAddr;
    
    @end
    
    ---.m---
    #import "JCBlutoothInfoModel.h"
    //就是空的,按信号排序的优化方法可以封装进去,还没研究
    @implementation JCBlutoothInfoModel
    
    @end
    

    2.控制器中

    
    主要是发现外设的方法中(我自己将系统的API简单的做了个封装,下面这个接口是发现新外设时会回调,和系统的API一样的功能)
    
    //发现设备
    - (void)foundPeripheral:(JCBluetoothManager *)manager peripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
    {
        // 1 - 创建外设模型
        JCBlutoothInfoModel *model = [[JCBlutoothInfoModel alloc]init];
        model.peripheral = peripheral;
        model.RSSI = RSSI;
        model.advertisementData = advertisementData;
        
        // 2 -解析广播数据
        NSObject *value = [advertisementData objectForKey:@"kCBAdvDataManufacturerData"];
        NSString *macStr = nil;
        
        if (![value isKindOfClass: [NSArray class]]){
            const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];
            if (valueString == NULL) {//如果为空,则跳过,解决出现空指针bug
                return;
            }
            
            macStr = [[NSString alloc]initWithCString:valueString encoding:NSUTF8StringEncoding];
            JCLog(@"发现的设备广播中:%@",macStr);
            model.adverMacAddr = macStr;
        }
        // 3 - 第一次扫描到的设备,添加进数组中
        if (_allBlutoothModel.count == 0) {
            [_allBlutoothModel addObject:model];
        }
        else{
            // 4 - 遍历数组中的蓝牙模型,更新原有的数据(主要是更新信号强度)
            for (NSInteger i = 0; i < _allBlutoothModel.count; i++) {
                JCBlutoothInfoModel *primaryModel = _allBlutoothModel[i];
                CBPeripheral *per = primaryModel.peripheral;
                
                if ([peripheral.identifier.UUIDString isEqualToString:per.identifier.UUIDString]) {
                    [_allBlutoothModel replaceObjectAtIndex:i withObject:model];//更新数组中的数据
                    
                    // 5 - 数组数据源中的模型 按信号值排序
                    _sortArray = [NSMutableArray arrayWithArray:[_allBlutoothModel sortedArrayUsingComparator:^NSComparisonResult(JCBlutoothInfoModel *p1, JCBlutoothInfoModel *p2){
                        
                        return [p2.RSSI compare:p1.RSSI];
                    }]];
                    
                    // 6 - 刷新列表
                    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
                }
            }
            // 7 - 若未有包含过此设备,则将其添加进数组中
            if (![_allBlutoothModel containsObject:model]) {
                [_allBlutoothModel addObject:model];
            }
        }
    }
    

    刚刚实现出来,没来得及整理,很多地方可以优化,精简的都还没,希望看到的大神不吝赐教,感谢感谢。
    代码:https://github.com/chan106/BluetoothListDemo

    相关文章

      网友评论

          本文标题:iOS按信号强度对蓝牙列表排序

          本文链接:https://www.haomeiwen.com/subject/meawwttx.html