iOS CoreBluetooth:centralManager:didConnectPeripheral / didFailToConnectPeripheral:没有被调用 [英] iOS CoreBluetooth : centralManager:didConnectPeripheral / didFailToConnectPeripheral: not getting called

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

问题描述

我把头发摆脱了这个问题。我正在尝试连接到BLE设备,看不到我在下面的代码中所做的错误。

   - (void)viewDidLoad {
[super viewDidLoad];
//加载视图后,通常从笔尖进行任何其他设置。

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

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


- (void)didReceiveMemoryWarning {
[super doReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

+(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 *)外设
advertisingData:(NSDictionary *)广告数据
RSSI:(NSNumber *)RSSI {
// NSLog(@Received peripheral:\\\
%@,peripheral);
// NSLog(@Adv data:%@,advertisementData);

[peripheral setDelegate:self];
[中央连接外设:外设选项: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 *)外设{
NSLog(@didConnectPeripheral);
}

- (void)centralManager:(CBCentralManager *)中心didDisconnectPeripheral:(CBPeripheral *)外设错误:(NSError *)错误{
NSLog(@didDisconnectPeripheral) ;


- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral :( CBPeripheral *)外设错误:(NSError *)错误{
NSLog(@连接失败);


- (void)外设:(CBPeripheral *)外设didReadRSSI :( NSNumber *)RSSI错误:(NSError *)错误{
NSLog(@didReadRSSI) ;
}

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



所有的设备都被发现在 didDiscoverPeripheral:,在选择器中我尝试连接它们。但是没有任何事情发生。



当我打电话给 didDiscoverPeripheral时,我希望有一个配对密码请求的对话框:
如果是这样,我没有看到任何对话框,为什么会这样?



从苹果文档,它清楚地表明,在尝试连接到设备后,你应该调用$ code> didConnectPeripheral 或 didFailToConnectPeripher ,但我没有。


$ b $任何想法?我一直在尝试近一个星期。
欣赏每一个帮助,谢谢。

解决方案

如果你不以某种方式保留 对象被传递到 didDiscoverPeripheral 然后一旦这个委托方法退出就会被释放,你不会得到一个连接。



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

  @property(strong,nonatomic)NSMutableArray *外设; 

viewDidLoad init

  self.peripherals = [NSMutableArray new]; 

然后将其添加到 didDiscoverPeripheral

   - (void)centralManager:(CBCentralManager *)中心didDiscoverPeripheral :( CBPeripheral *)外围广告数据:(NSDictionary *)advertisingData RSSI:(NSNumber *)RSSI 
{
NSLog(@发现的外设%@,peripheral.identifier.UUIDString);
[self.peripherals addObject:peripheral];
[中央连接外设:外设选项:nil];
}


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 : \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");
}

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?

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

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?

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.

解决方案

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;

initialise this in viewDidLoad or init

self.peripherals=[NSMutableArray new];

And then add the peripheral to it in 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:没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆