gpt4 book ai didi

ios - 无法连接到 iOS 中的 BLE 设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:50 25 4
gpt4 key购买 nike

我实现了代码,但是当连接到 BLE 设备时,它显示警告:

2013-12-20 16:58:57.975 TestPeripheral[1054:60b] CoreBluetooth[WARNING] <CBPeripheral: 635 identifier = Addfew23-424-E653-8E58-424343, Name = "BLE", state = connecting> is being dealloc'ed while connecting

我使用了以下代码:

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


NSLog (@"Discovered peripheral: %@", [peripheral name]);

NSLog (@"UUID peripheral: %@", [peripheral UUID]);

NSLog (@"peripheral services before connected: %@", [peripheral services]);


deviceName.text = [NSString stringWithFormat:@"Device Name: %@", [peripheral name]];
deviceUUID.text = [NSString stringWithFormat:@"Device UUID: %@",[peripheral UUID]];

[self.manager stopScan];
NSLog(@"Scanning stopped");

NSLog (@"start connecting to device");
[self.manager connectPeripheral:peripheral options:nil];
self.activePeripheral = peripheral;
}

- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
for (CBPeripheral *peripheral in peripherals) {
NSLog(@"Retrieved Peripheral %@", peripheral.name);
[foundArray addObject:peripheral.name];
}
}

我将外围设备添加到 foundarray 并将它们显示到 tableview,如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [foundArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

cell.selectionStyle = UITableViewCellSelectionStyleGray;
}


cell.textLabel.text = [foundArray objectAtIndex:indexPath.row];

return cell;
}

当用户选择要连接的设备时,它会显示消息等待:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = indexPath.row;
UIAlertView *alertPair = [[UIAlertView alloc] initWithTitle:@"Connecting" message:@"Waiting to connect" delegate:nil cancelButtonTitle:@"OK", nil];

[alertPair show];
}

请给我一些建议。非常感谢。

最佳答案

CoreBluetooth[WARNING] is being dealloc'ed while connecting

当您不保留新连接的外围设备时会出现此警告。连接到外围设备后,您必须保留对它的引用(保留在 mrc 中或在 arc 中保持强)。否则,系统只会清理本地范围并假设您不关心外围设备。所以基本上,只要保留对它的引用就可以了。

关于ios - 无法连接到 iOS 中的 BLE 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com