1

我正在构建几个 BLE 应用程序,并且在我的 BLE 协议以及外部供应商中遇到了这个问题。在大多数情况下,BLE 会按预期工作,但有时它会无法发现特定外围设备的服务,并且在我重新启动或重置手机之前不会再次工作。

有没有其他人遇到过这个问题?

当我用完外围设备后,经理将调用该closePeripheralConnection方法。相关代码:

// Disconnect from peripheral
- (void)closePeripheralConnection
{
    NSLog(@"== closePeripheralConnection ==");
    // If the peripheral is connected
    if (self.peripheral.state == CBPeripheralStateConnected && self.peripheral != nil) {
        // Cancel connection
        [self.manager cancelPeripheralConnection:self.peripheral];
    } else {
        [self clearPeripheralSettings];
    }
}

// Clear peripheral settings
- (void)clearPeripheralSettings
{
    // Clear peripheral variables
    self.peripheral = 0;
    // Clear services
    self.service = 0;
    // Clear characteristic values
    self.data = 0;
    self.notifyCharacteristic = 0;
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    [self clearPeripheralSettings];
}
4

2 回答 2

2

根据代码和您的评论,您没有做任何坏事。阻碍您工作的仍然是 Core Bluetooth 框架的不稳定性。在 iOS 6 中,此类问题比在 iOS 7 中更常见,但严重的压力仍然可能导致问题严重。Apple Developer Forum 和 bluetooth-dev 邮件列表中充满了类似的报告。如果您在测试时所做的那种使用方式在日常使用中并不常见,那么问题可能与最终用户无关。(虽然肯定还是很烦人。)

我建议你尝试收集统计数据。记录您调用每个 API 的次数,并尝试为 Apple 建立一些证据,以表明该框架仍然不稳定。将统计信息存储在 NSUserDefaults 中可能是最简单的解决方案。

无论如何,如果您有强大的数据可以作为故障的证据,那么将错误报告给 Apple,在开发者论坛上发帖,发送邮件到蓝牙开发列表。让尽可能多的人听到您的声音。

于 2013-10-30T13:11:49.077 回答
0

你什么时候调用这个方法?- 关闭外围连接

您可能希望添加它以要求外围设备在中央管理器回调委托之一中发现其服务

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

    NSLog(@"peripheral: %@ state: %@", peripheral.name, peripheral.state == CBPeripheralStateConnected ? @"Connected" : @"Disconnected");

    if (peripheral.state == CBPeripheralStateConnected) {
        peripheral.delegate = self;
        [peripheral discoverServices:nil];
    }

}
于 2015-06-04T11:24:32.460 回答