corebluetooth重新连接到Peripheral和discoverServices ios

时间:2015-04-28 12:29:36

标签: ios core-bluetooth

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

我的问题在我的BLE设备断开连接时开始。当发生这种情况时,我会做以下事情:

  1. retrievePeripheralsWithIdentifiers
  2. cancelPeripheralConnection(以防万一)
  3. 再次连接外围设备
  4. activePeripheral = peripheral
  5. [activePeripheral discoverServices:services]; (我首先检查外围设备是否有服务,但它没有服务)
  6. 当我尝试发现服务我的应用程序崩溃!!

    CODE:

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

    CRASH LOG:

    * 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来说真的很新。

1 个答案:

答案 0 :(得分:-1)

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