繁体   English   中英

corebluetooth 重新连接到外设并发现服务 ios

[英]corebluetooth reconnect to Peripheral and discoverServices ios

我正在使用 corebluetooth 框架将我的 iphone 连接到 BLE 设备。 我已经成功地实现了连接到外围设备并发现服务和特征以及更新特征的值。

当我的 BLE 设备断开连接时,我的问题就开始了。 发生这种情况时,我会执行以下操作:

  1. 使用标识符检索外围设备
  2. cancelPeripheralConnection(以防万一)
  3. 再次连接外设
  4. activePeripheral = 外设
  5. [activePeripheraldiscoverServices:services]; (我首先检查外围设备是否有服务,但没有)

当我尝试发现服务时,我的应用程序崩溃了!!

代码:

    NSArray *retrievePeripherals = [[PRCentralDiscoverManger sharedInstance] retrivePeripheral:@[peripheral.identifier]];
        NSLog(@"retrievePeripherals: %@",retrievePeripherals);

        for (CBPeripheral *p in retrievePeripherals)
        {
            if (peripheral == p )
            {
                for (PRPeripheralManger *pm in _serviceArray)
                {
                    if (p == pm.peripheral)
                    {
                        NSLog(@"PRPeripheralManger in _serviceArray");
                        //[[PRCentralDiscoverManger sharedInstance] disconnectPeripheral:p];
                        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                            //Here your non-main thread.
                            [NSThread sleepForTimeInterval:2.0f];
                            dispatch_async(dispatch_get_main_queue(), ^{
                                //Here you returns to main thread.
                                NSLog(@"Here you returns to main thread");

                                [[PRCentralDiscoverManger sharedInstance] connectPeripheral:peripheral];
                                service = [[PRPeripheralManger alloc]initWithPeripheral:peripheral controller:self];
                                [service startDiscoveringServicesWithUUIDString:uuid];
                            });
                        });

                        break;
                    }
                }
            }


        }
- (id) initWithPeripheral:(CBPeripheral *)peripheral controller:(id<PRPeripheralMangerProtocol>)controller
{


    self = [super init];
    if (self)
    {
        activePeripheral = peripheral;
        [activePeripheral setDelegate:self];
        _peripheralDelegate = controller;


    }
    return self;
}     
- (void) startDiscoveringServicesWithUUIDString:(NSString *)uuidString
{
    NSLog(@"startDiscoveringServicesWithUUIDString peripheral:%@",activePeripheral); //EXSIST
    NSLog(@"startDiscoveringServicesWithUUIDString services :%@",activePeripheral.services); // ALWAYS NULL

    if (activePeripheral.services)
    {
        NSLog(@"activePeripheral.services: %@",activePeripheral.services);

        [self peripheral:activePeripheral didDiscoverServices:nil];
    }
    else
    {
        if (uuidString)
        {
            NSLog(@"discoverServices: %@",uuidString);
            NSArray *services = @[[CBUUID UUIDWithString:uuidString]];
            [activePeripheral discoverServices:services];//CRASHES HERE
        }
        else
        {
            NSLog(@"discoverServices:nil");

            [activePeripheral discoverServices:nil];
        }
    }
}  

崩溃日志:

* thread #1: tid = 0x58f4, 0x30b00f46 libobjc.A.dylib`objc_msgSend + 6, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xc000000c)
frame #0: 0x30b00f46 libobjc.A.dylib`objc_msgSend + 6
frame #1: 0x230142ac CoreBluetooth`-[CBPeripheral handleServicesDiscovered:] + 620
frame #2: 0x230122b6 CoreBluetooth`-[CBPeripheral handleMsg:args:] + 282
frame #3: 0x2300e674 CoreBluetooth`-[CBCentralManager xpcConnection:didReceiveMsg:args:] + 256
frame #4: 0x23019356 CoreBluetooth`__34-[CBXpcConnection handleMsg:args:]_block_invoke + 54
frame #5: 0x002fb9da libdispatch.dylib`_dispatch_call_block_and_release + 10
frame #6: 0x002fb9c6 libdispatch.dylib`_dispatch_client_callout + 22
frame #7: 0x00303e28 libdispatch.dylib`_dispatch_queue_drain + 1092
frame #8: 0x002fe2c8 libdispatch.dylib`_dispatch_queue_invoke + 88
frame #9: 0x002ff21e libdispatch.dylib`_dispatch_main_queue_callback_4CF + 346
frame #10: 0x233003b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
frame #11: 0x232feab0 CoreFoundation`__CFRunLoopRun + 1512
frame #12: 0x2324c3c0 CoreFoundation`CFRunLoopRunSpecific + 476
frame #13: 0x2324c1d2 CoreFoundation`CFRunLoopRunInMode + 106
frame #14: 0x2a64a0a8 GraphicsServices`GSEventRunModal + 136
frame #15: 0x2685a7b0 UIKit`UIApplicationMain + 1440

如果这不是提出问题的方式,我对 stackoverflow 真的很陌生。

您应该将您的 iphone 与外围设备配对。 然后设置应用程序将在断开连接时重新连接到外围设备。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM