问题 为什么使用coreBluetooth connectPeripheral没有在IOS8中调用委托方法


我想用 CoreBluetooth.framework 在 iOS8上 为了实现数据传输,我确实在以下方法中发现了外设并尝试连接外设。

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discover name : %@", peripheral.name);
    [self.centralManager connectPeripheral:peripheral options:nil];
}

但它没有调用委托方法 didFailToConnectPeripheral 要么 didConnectPeripheral  ,我对它有什么问题,是代码错误还是IOS8需要一些额外的东西?如何处理,谢谢!

这是我的代码 github上 ,我写了一个程序作为服务器,另一个是中央。


12199
2018-01-27 10:07


起源

您是否尝试将外围物体存放在ivar中然后连接到它?即 self.discoveredPeripheral = peripheral; [self.centralManager connectPeripheral:self.discoveredPeripheral options:nil]; - Sj.
可能重复 iOS CoreBluetooth:centralManager:didConnectPeripheral / didFailToConnectPeripheral:没有被调用 - Paulw11
它工作,非常感谢你! - Clavis


答案:


@Sj评论为我解决了这个问题,将其复制成答案以防万一有人会错过它:

您是否尝试将外围物体存放在ivar中然后连接到它?即self.discoveredPeripheral = peripheral; [self.centralManager connectPeripheral:self.discoveredPeripheral options:nil];


9
2017-10-13 10:35





我在用 TemperatureSensor iOS示例应用 哪个发现了外围没有打过电话。通过参考流程 Heart Rate Monitor Mac示例应用程序。请确保代码的运行顺序是:

1. centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
2. - (void) centralManagerDidUpdateState:(CBCentralManager *)central
3. [centralManager scanForPeripheralsWithServices:... 

- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:... 之后会被召唤。


3
2017-08-12 09:37





仅调用connect方法一次并等待连接委托响应。您发布的上述代码将更频繁地调用connect方法(当它发现外围设备时)

if(!self.detectedBLE){ [self.centralManager stopScan]; self.detectedBLE = peripheral; [self.centralManager connectPeripheral:self.detectedBLE options:nil]; }


0
2017-09-08 14:32