经过整个下午的调试,总算清楚何时会触发 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 方法。
当调用手机连接外设的方法: - (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;,作为中心设备的手机会和硬件之间建立连接,但是建立连接也是需要一定时间的,具体的时间是多少我并没有测算出来,肯定是很短暂的,但也不是微乎其微。
就在这短暂的时间内,如果调用手机和外设断开的代理方法: - (void)cancelPeripheralConnection:(CBPeripheral *)peripheral; 那么就会进入连接失败的代理方法之中: - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
/*!
* @method centralManager:didFailToConnectPeripheral:error:
*
* @param central The central manager providing this information.
* @param peripheral The <code>CBPeripheral</code> that has failed to connect.
* @param error The cause of the failure.
*
* @discussion This method is invoked when a connection initiated by {@link connectPeripheral:options:} has failed to complete. As connection attempts do not
* timeout, the failure of a connection is atypical and usually indicative of a transient issue.
*
*/
苹果官方给出的解释是由{@link connectPeripheral:options:}发起的连接没有完成,就会调用。