![](/img/trans.png)
[英]CoreBluetooth Central Manager cannot discover peripheral in background
[英]Store UUID of discover peripheral in CoreBluetooth LE iOS
我嘗試下面的代碼將已發現設備的UUID存儲到Array,然后在此陣列中選擇UUID進行連接但不運行。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog (@"Discovered peripheral: %@", [peripheral name]);
[foundArray addObject:peripheral.name];
NSLog (@"UUID peripheral: %@", [peripheral UUID]);
CFUUIDRef uuid = [peripheral UUID];
[discoverUUIDArray addObject: (__bridge id)(uuid)];
NSLog(@"Before = %@", uuid);
NSLog (@"peripheral services before connected: %@", [peripheral services]);
NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]);
self.activePeripheral = peripheral;
NSLog(@"foundArray is %@", foundArray);
NSLog(@"discoverUUIDArray is %@", discoverUUIDArray);
[self.tblFound reloadData];
}
然后我將這些Discover設備加載到tableview,當用戶選擇要連接的設備時,我的應用程序掛起。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.manager.state == CBCentralManagerStatePoweredOn)
{
NSLog(@"You selected %@ to connect",[foundArray objectAtIndex:indexPath.row]);
CFUUIDRef uuidGet = (__bridge CFUUIDRef) [discoverUUIDArray objectAtIndex:indexPath.row];
NSLog(@"UUID is %@",[discoverUUIDArray objectAtIndex:indexPath.row]);
[self.manager connectPeripheral:[discoverUUIDArray objectAtIndex:indexPath.row] options:nil];
}
}
選擇要連接的設備時,會顯示錯誤:
[__NSCFType state]: unrecognized selector sent to instance 0x17d70950
2013-12-25 15:17:40.699 TestPeripheral[213:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType state]: unrecognized selector sent to instance 0x17d70950'
discoverUUIDArray存儲發現設備的UUID。 我不知道為什么。 請給我一些建議。 非常感謝。
CFUUIDRef uuid = [peripheral UUID];
在iOS7中不推薦使用,如果需要,可以使用peripheral.identifier
作為NSUUID
存儲在數組中
如果意圖是存儲UUID's
以供將來檢索,請將其保存在NSUserDefaults
而不是運行時數組中
您可以創建一個NSMutableArray來存儲所有已發現設備的列表,然后使用此列表填充UITableView並連接(如果用戶選擇)。
例如
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if (![localName isEqual:@""]) {
[self.BTLEDevices addObject:peripheral];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *tableIdentifier = @"BLEDeviceList";
CBPeripheral *peripheral = [self.BTLEDevices objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier forIndexPath:indexPath];
cell.textLabel.text = peripheral.name;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CBPeripheral *peripheral = [self.BTLEDevices objectAtIndex:[indexPath.row integerValue]];
peripheral.delegate = self;
[self.BTLECentralManager connectPeripheral:peripheral options:nil];
}
不要忘記創建UITableView對象並將其添加到視圖中,我也遺漏了其他CBPeripheral方法。 希望有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.