简体   繁体   English

BLE设备保持连接/断开连接

[英]BLE device keeps getting connected/disconnected

I am using my app to connect with a BLE device and keeping track of the connection status as follows 我正在使用我的应用程序连接BLE device并跟踪连接状态,如下所示

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

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

//Device found, request connection
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)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];
     }
}

The problem that I face with an iPhone 5 running 7.1.1 is that the status on the label keeps switching between connected and disconnected. 我在运行7.1.1iPhone 5上遇到的问题是,标签上的状态始终保持在已连接和已断开连接之间切换。 The Bluetooth icon on the status bar confirms this (keeps showing connected and disconnected status). 状态栏上的蓝牙图标可以确认这一点(提示音会显示已连接和已断开状态)。 I tried turning the device on/off (both the BLE device and a hard reset of the phone). 我尝试打开/关闭设备(包括BLE设备和电话的硬重置)。 Also turned Bluetooth on/off. 还打开/关闭了蓝牙。 But I still get this problem. 但是我仍然遇到这个问题。

Also I'm following the suggestion provided here in this question but no luck. 我也遵循此问题中此处提供的建议但是没有运气。

Can someone let me know if there's anything I am doing wrong or if this is an OS issue? 有人可以让我知道我做错了什么吗,或者这是操作系统问题?

Thanks. 谢谢。

Write [activePeripheral retain]; 写[activePeripheral reserved]; inside the if condition in didDiscoverPeripheral. 在didDiscoverPeripheral中的if条件内。 Hope it will help 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM