OGeek|极客世界-中国程序员成长平台

标题: ios - BLE 设备不断连接/断开连接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:26
标题: ios - BLE 设备不断连接/断开连接

我正在使用我的应用程序连接 BLE 设备 并跟踪连接状态,如下所示

//Called when device is connected
- (void)centralManagerCBCentralManager *)central didConnectPeripheralCBPeripheral *)peripheral
{
    [self.activePeripheral discoverServices:nil];
    if (peripheral.isConnected)
    {
        currentStatusLbl.text = [NSString stringWithFormat"Connected"];
    }
}

//Called if device disconnects
-(void)centralManagerCBCentralManager *)central didDisconnectPeripheralCBPeripheral *)peripheral errorNSError *)error
{
    currentStatusLbl.text = [NSString stringWithFormat"Disconnected"];
    [self.centralManager connectPeripheral:peripheral options:nil]; //Connect again
}

//Device found, request connection
- (void)centralManagerCBCentralManager *)central didDiscoverPeripheralCBPeripheral *)peripheral advertisementDataNSDictionary *)advertisementData RSSINSNumber *)RSSI
{
     NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
     devieName.text = [localName stringByAppendingString" Found"];
     if (localName && ![localName isEqual""])
     {
         [self.centralManager stopScan];
         self.activePeripheral = peripheral;
         self.activePeripheral.delegate = self;
         [self.centralManager connectPeripheral:peripheral options:nil];
     }
}

运行 7.1.1iPhone 5 遇到的问题是标签上的状态不断在连接和断开之间切换。状态栏上的蓝牙图标确认了这一点(持续显示连接和断开状态)。我尝试打开/关闭设备(BLE 设备和手机的硬重置)。还打开/关闭蓝牙。但我仍然遇到这个问题。

我也遵循 here in this question 提供的建议但没有运气。

如果我做错了什么或者这是操作系统问题,有人可以告诉我吗?

谢谢。



Best Answer-推荐答案


写[主动外设保留];在 didDisconnectPeripheral 的 if 条件内。 希望对你有帮助

关于ios - BLE 设备不断连接/断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23909284/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4