IOS应用程序使用Core-Bluetooth框架与BLE外设进行通信。该应用程序已经注册了葡萄糖特性,并且每隔一分钟就会从外围设备接收数据。
可以观察到,当应用程序处于空闲状态时,不存在与BLE外围设备的断开,而如果我在应用程序中存在的ViewControllers之间导航,则恰好存在与BLE外围设备的连续断开。
连接参数设置在Apple的Core-Bluetooth编程指南中提到的给定范围内。你知道为什么连接一直断开吗?
我还使用了苹果开发者网站上的BTLE Transfer源代码。并且已经用葡萄糖服务UUID替换了转账服务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]]];
}发布于 2016-02-18 15:17:42
检查您没有显式地使用weak属性来管理您的外围设备(默认类型为strong),检查此issue。
然而,在没有任何代码的情况下,我所能做的就是抓取一个BLE packet sniffer,看看它发送断开命令的是谁、什么时候以及为什么。在此过程中,请注意通道跳跃,您有1/3的机会使用嗅探器捕获通道,因此只需重试,直到您在屏幕上看到数据流。如果不是现在,如果你经常使用蓝牙Low Energy,那么拥有这样的设备总是很受欢迎的。
发布于 2016-10-27 22:51:25
// 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);
}使用此方法,注意:将发现的外围设备分配给centralManager中保留的CBPeripheral是一种很好的做法: didConnectPeripheral
//this one line code stops disconnecting device automatically.
[[Manager sharedManager].connected_PeripheralDevice discoverServices:nil];https://stackoverflow.com/questions/35472918
复制相似问题