本文介绍了iOS CoreBluetooth:centralManager:didConnectPeripheral/didFailToConnectPeripheral:没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在摆脱这个问题.我正在尝试连接到 BLE 设备,在下面的代码中看不到我做错了什么.

I'm pulling my hair out of this problems. I'm trying to connect to BLE devices, can't see what I've done wrong in my code below.

- (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 :
%@", 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 连接不需要它,对吧?

These devices are not my own. I don't know its proximity UUID, but as far as I know, It won't be needed in connecting via CoreBluetooth right?

所有设备都在 didDiscoverPeripheral: 中发现,在选择器中我尝试连接它们.但在那之后什么都没有发生.

All of the devices are discovered in didDiscoverPeripheral:, in the selector I tried to connect them. But there's nothing comes after that.

当我调用 didDiscoverPeripheral: 时,我会期待一个包含配对密码请求的对话框吗?如果是这样,我没有看到任何对话框,这是为什么呢?

Am I to expect a dialog with Pairing Password Request when I called to didDiscoverPeripheral:?If so I don't see any dialog, why is that?

从苹果文档中,它明确指出,在尝试连接到设备后,您应该得到对 didConnectPeripheraldidFailToConnectPeripher 的调用,但我没有得到.

From apple documents, It clearly stated that after trying to connect to a device you should get a called to either didConnectPeripheral or didFailToConnectPeripher but I got none.

有什么想法吗?我已经尝试了将近一个星期.感谢每一个帮助,谢谢.

Any thoughts? I've been trying for almost a week now.Appreciate every helps, thanks.

推荐答案

如果你不以某种方式保留传递给 didDiscoverPeripheralperipheral 对象,那么它就会被释放一旦此委托方法退出,您将无法获得连接.

If you don't somehow retain the peripheral object that is delivered to didDiscoverPeripheral then it is released once this delegate method exits and you won't get a connection.

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

I suggest adding a property to track discovered peripherals

@property (strong,nonatomic) NSMutableArray *peripherals;

viewDidLoadinit 中初始化这个

initialise this in viewDidLoad or 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];
}

这篇关于iOS CoreBluetooth:centralManager:didConnectPeripheral/didFailToConnectPeripheral:没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 08:42