iOS CoreBluetooth:centralManager:didConnectPeripheral / didFailToConnectPeripheral:没有被调用

我把头发拉出了这个问题。我试图连接到BLE设备,看不到我在下面的代码中做错了什么。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (NSString*)UUIDString:(CFUUIDRef)uuid {
    CFStringRef string = CFUUIDCreateString(NULL, uuid);
    return (__bridge_transfer NSString*)string;
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBCentralManagerStatePoweredOn) {
        [self scanForPeripherals];
    }
}

- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI {
//    NSLog(@"Received peripheral : \n%@", peripheral);
//    NSLog(@"Adv data : %@", advertisementData);

    [peripheral setDelegate:self];
    [central connectPeripheral:peripheral options:nil];
    [peripheral readRSSI];
}

- (int)scanForPeripherals {
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
                             nil];

    [_cm scanForPeripheralsWithServices:nil options:options];
    return 0;
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"didConnectPeripheral");
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"didDisconnectPeripheral");
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"failed to connect");
}

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
    NSLog(@"didReadRSSI");
}

这些设备不是我自己的。我不知道它的接近度UUID,但据我所知,通过CoreBluetooth连接不需要吗?

所有设备都在didDiscoverPeripheral中发现:在选择器中我尝试连接它们。但是之后没有什么事情。

当我打电话给DiscoverPeripheral时,我期待一个配对密码请求的对话:?
如果是这样,我没有看到任何对话,为什么呢?

从苹果文档,它清楚地表明,尝试连接到设备后,您应该得到一个调用或者didConnectPeripheral或didFailToConnectPeripher,但我没有。

有什么想法吗?我一直在尝试近一个星期。
感谢各位的帮助,谢谢。

如果您不以某种方式保留发送到didDiscoverPeripheral的外设对象,那么一旦此委托方法退出就会被释放,您将无法获取连接。

我建议添加一个属性来跟踪发现的外设

@property (strong,nonatomic) NSMutableArray *peripherals;

在viewDidLoad或init中初始化

self.peripherals=[NSMutableArray new];

然后将其添加到didDiscoverPeripheral中

-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
    [self.peripherals addObject:peripheral];
    [central connectPeripheral:peripheral options:nil];
}
http://stackoverflow.com/questions/26377470/ios-corebluetooth-centralmanagerdidconnectperipheral-didfailtoconnectperiph

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iOS CoreBluetooth:centralManager:didConnectPeripheral / didFailToConnectPeripheral:没有被调用