項目中有個需求,將搜索到的藍(lán)牙外設(shè)列表,按信號強度進(jìn)行排序展示出來,本著面向產(chǎn)品的編程思想,刀山火海也要上。。。
思路就是:
1 - 建立一個模型類,類中有外設(shè)名稱、信號強度、服務(wù)數(shù)、廣播等屬性。
2 - 用經(jīng)典的tableview展示外設(shè)列表。就涉及到一個數(shù)據(jù)的數(shù)組了,這個數(shù)組中存放的就是上面藍(lán)牙模型類對象。
3 - 每次搜索到一個藍(lán)牙外設(shè),一般做法都是將其添加進(jìn)數(shù)組中,然后刷tableview。在添加進(jìn)去之前,要判斷下是否已經(jīng)添加過該設(shè)備,如有添加,則不再添加,而是將數(shù)組中的對應(yīng)的外設(shè)模型對象刷新下即可。
4 - 然后,將數(shù)組中模型對象,按信號屬性排序,最后刷tableview即可實現(xiàn)列表按信號強度展示了
下面是具體代碼實現(xiàn):
1、創(chuàng)建藍(lán)牙模型類
---.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"
//就是空的,按信號排序的優(yōu)化方法可以封裝進(jìn)去,還沒研究
@implementation JCBlutoothInfoModel
@end
2.控制器中
主要是發(fā)現(xiàn)外設(shè)的方法中(我自己將系統(tǒng)的API簡單的做了個封裝,下面這個接口是發(fā)現(xiàn)新外設(shè)時會回調(diào),和系統(tǒng)的API一樣的功能)
//發(fā)現(xiàn)設(shè)備
- (void)foundPeripheral:(JCBluetoothManager *)manager peripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// 1 - 創(chuàng)建外設(shè)模型
JCBlutoothInfoModel *model = [[JCBlutoothInfoModel alloc]init];
model.peripheral = peripheral;
model.RSSI = RSSI;
model.advertisementData = advertisementData;
// 2 -解析廣播數(shù)據(jù)
NSObject *value = [advertisementData objectForKey:@"kCBAdvDataManufacturerData"];
NSString *macStr = nil;
if (![value isKindOfClass: [NSArray class]]){
const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];
if (valueString == NULL) {//如果為空,則跳過,解決出現(xiàn)空指針bug
return;
}
macStr = [[NSString alloc]initWithCString:valueString encoding:NSUTF8StringEncoding];
JCLog(@"發(fā)現(xiàn)的設(shè)備廣播中:%@",macStr);
model.adverMacAddr = macStr;
}
// 3 - 第一次掃描到的設(shè)備,添加進(jìn)數(shù)組中
if (_allBlutoothModel.count == 0) {
[_allBlutoothModel addObject:model];
}
else{
// 4 - 遍歷數(shù)組中的藍(lán)牙模型,更新原有的數(shù)據(jù)(主要是更新信號強度)
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];//更新數(shù)組中的數(shù)據(jù)
// 5 - 數(shù)組數(shù)據(jù)源中的模型 按信號值排序
_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 - 若未有包含過此設(shè)備,則將其添加進(jìn)數(shù)組中
if (![_allBlutoothModel containsObject:model]) {
[_allBlutoothModel addObject:model];
}
}
}
剛剛實現(xiàn)出來,沒來得及整理,很多地方可以優(yōu)化,精簡的都還沒,希望看到的大神不吝賜教,感謝感謝。
代碼:https://github.com/chan106/BluetoothListDemo