IOS BLE连接断开连接

时间:2016-02-18 04:33:19

标签: ios iphone bluetooth-lowenergy core-bluetooth

IOS应用程序正在使用Core-Bluetooth框架与BLE外围设备进行通信。该应用程序已注册葡萄糖特征,并在每分钟后从外围设备接收数据。

据观察,当应用程序处于空闲状态时,不会与BLE外设断开连接,而如果我在应用程序中存在的ViewControllers之间导航,则会发生与BLE外设的连续断开连接

连接参数设置在Apple的Core-Bluetooth编程指南中提到的给定范围内。任何想法为什么连接断开连接?

我还使用了苹果开发者网站上的BTLE传输源代码。并用葡萄糖服务UUID替换了Transfer服务UUID。我在iOS版本8.3的iPhone 6上测试了这个。我仍然面临同样的断线问题。

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

   if (self.discoveredPeripheral != peripheral) {
       self.discoveredPeripheral = peripheral;

        [self.centralManager connectPeripheral:peripheral options:nil];
    }
}


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

    [self.centralManager stopScan];
    [self.data setLength:0];

    peripheral.delegate = self;
    [peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]];

}

2 个答案:

答案 0 :(得分:0)

检查您是否明确使用weak属性来管理您的外围设备(默认类型为strong),请检查此issue

然而,如果没有任何代码,我可以建议抓住BLE packet sniffer并查看谁,何时以及为什么它发送断开连接命令。在这个过程中要注意通道跳跃,你有1/3的机会用嗅探器捕获通道,所以只需重试直到你在屏幕上看到数据流。如果不是现在,如果您经常使用Bluetooth Low Energy,我们总是欢迎拥有这样的设备。

答案 1 :(得分:0)

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    self.myPeripheral = peripheral;
    self.myPeripheral.delegate = self;
    NSString *connected = [NSString stringWithFormat:@"Connected: %@", peripheral.state == CBPeripheralStateConnected ? @"YES" : @"NO"];
    NSLog(@"%@", connected);
}

把这个方法, 注意:最好将发现的外设分配给保留的CBPeripheral到centralManager:didConnectPeripheral

//this one line code stops disconnecting device automatically.
[[Manager sharedManager].connected_PeripheralDevice discoverServices:nil];