【问题标题】:iOS CoreBluetooth : centralManager:didConnectPeripheral / didFailToConnectPeripheral: not getting callediOS CoreBluetooth:centralManager:didConnectPeripheral / didFailToConnectPeripheral:没有被调用
【发布时间】:2014-12-10 05:31:45
【问题描述】:

我正在摆脱这个问题。我正在尝试连接到 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: 中发现,在我尝试连接它们的选择器中。但在那之后什么都没有发生。

当我打电话给didDiscoverPeripheral: 时,我会期待与配对密码请求对话吗? 如果是这样,我没有看到任何对话框,这是为什么呢?

从苹果文档中,它清楚地指出,在尝试连接到设备后,您应该得到didConnectPeripheraldidFailToConnectPeripher 的调用,但我没有。

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

【问题讨论】:

  • 你接到didDiscoverPeripheral的电话了吗?您是否尝试过在您的连接请求之后立即删除对 [peripheral readRSSI] 的呼叫?在您连接之前,您不应该发出该请求。我总是建议人们尝试从应用商店下载免费的 LightBlue 应用来测试他们的设备是否有广告并且是否可以连接
  • 是的,我试过了,这个代码的第一个版本,它什么也不做,只是发现并尝试连接,但在connectPeripheral 执行之后什么也没发生。
  • 试试 LightBlue - 这样您至少可以确认硬件是可连接的。还可以尝试将要连接的外围设备存储在一个属性中,这样它就不会被释放
  • 试过了,所有这些都显示在 LightBlue 中,没有为任何设备显示任何服务。有 2 个 Estimotes BLE 和另外 2 个我的 国家定制 BLE*。询问 estimotes BLE 导致连接成功,但几秒钟后,LightBlue 说“断开连接警报”,并且有一个红色文本说“数据已过时”,另外 2 个设备询问显示另一个页面,如 estimotes,仅此而已跨度>

标签: ios objective-c delegates bluetooth-lowenergy core-bluetooth


【解决方案1】:
var peripherals = [CBPeripheral]()

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    peripherals.append(peripheral)
    bleManager.connectPeripheral(peripheral, options: nil)
}

这是 Swift 版本。

【讨论】:

    【解决方案2】:

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

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

    @property (strong,nonatomic) NSMutableArray *peripherals;
    

    viewDidLoadinit 中初始化它

    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];
    }
    

    【讨论】:

    • 是的!!!这就是诀窍,非常感谢@Paulw11。我不知道从didDiscoverPeripheral 传入的外围设备被弱引用。我在苹果文档中也没有找到任何关于此的内容,或者是我跳过了那部分?无论如何,非常感谢!!!。
    • 我不知道我是如何找到它的——我想我必须通过艰难的方式来解决它。无论如何,我将我的应用程序与您的代码进行了比较,这就是区别
    • 好收获。在您回答之后,我刚刚还通知说,我在继续参考连接的设备后一直在工作的所有教程。一件小事,但肯定非常感谢,再次。 @Paulw11
    • @Paulw11 当我关闭蓝牙时,我的应用程序没有在后台启动。我在这里问了一个问题:stackoverflow.com/questions/48030480/…
    • @Paulw11 当用户在几个小时后打开蓝牙时,是否会调用 centralManagerDidUpdateState 方法?如果我关闭收音机并在几个小时后打开它,我会卡在没有调用 centralManagerDidUpdateState 的地方。
    猜你喜欢
    • 2011-10-03
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 2015-03-18
    • 2013-10-18
    相关资源
    最近更新 更多