iOS Bluetooth retrievePeripherals导致EXC_BAD_ACCESS崩溃

时间:2013-09-06 21:43:10

标签: ios bluetooth exc-bad-access core-bluetooth

我需要定期重新连接到外围设备,但尝试重新连接崩溃。

首先,我的中心扫描外围设备' UUID,然后它连接,然后它将peripheral.UUID存储到CFUUIDRef变量,然后断开连接。接下来,重新连接它从变量中检索UUID,然后与" EXC_BAD_ACCESS"崩溃。什么时候它会检测外围设备,或者稍后它会执行后续的didRetrievePeripherals。

也许我没有正确存储UUID,但我没有看到错误。感谢。

这里是代码..........

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    ///////////////////////   C E N T R A L   C O N N E C T E D   P E R I P H E R A L   //////////////////////

    [self.centralManager   stopScan];
    // Make sure we get the discovery callbacks
    peripheral.delegate = pwr_tx_management;

    switch( PWR_TX_manager.present_operation )
    {
        case POLLING_FOR_GENERIC_DVINEWAVE_DEVICE:
            if( dbg_ev )        printf("1");        
            // Add new record for connection device to local system status database:
            update_device_record( &packed_ad_record    );       //      ==>  device_record_index
            // Store peripheral UUID for later, perioed re-connection for polling:
            area_device_status[ device_area ][ device_record_index ].peripheral_UUID = peripheral.UUID;
                                 // uses declaration:  CFUUIDRef            peripheral_UUID;

    // Disconnect:        !!!
        [self.centralManager   cancelPeripheralConnection:   peripheral];
        break;


        case OPERATION_SPECIFIC_POLLING:
                                        NSLog( @"(7) didConnectPeripheral   " );
            if( dbg_ev )        printf("2");        
            // Proceed with reading device's status and updating local database:
                [peripheral   discoverServices:nil];    // request service, characteristics, then request device send its packed status record

            break;
        default:
            break;
    }    
}





// Connects to device specificed by index next_dbase_record .
void connect_specific_device( void )
{
                                                        NSLog( @"(5) connect_specific_device    - next_dbase_record = %d", next_dbase_record );
    CFUUIDRef uuid = area_device_status[ device_area ][ next_dbase_record ].peripheral_UUID;

    if( uuid )
    {
        // Request call to didRetrievePeripherals() with CBPeripheral:
        [pwr_tx_management.centralManager retrievePeripherals:[NSArray arrayWithObject: (id)CFBridgingRelease(uuid)]];     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< crashes here with:  Thread 1: EXC_BAD_ACCESS
    }
    else
    {
        NSLog( @"   - missing area_device_status[][].peripheral_UUID ");
    }

}



// Iniates connection to peripheral device specified before by retrievePeripherals.
- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(CBPeripheral *)peripheral
{
    NSLog( @"(6) didRetrievePeripherals");
    [central   connectPeripheral:peripheral   options:nil];
}

2 个答案:

答案 0 :(得分:1)

让我先说一下,我是用手机把它写在汽车里,所以请原谅我的格式不佳。我只是指出我看到的主要问题。你没有采用正确的委托方法:

是:

- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *) peripherals

不是这个:

- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(CBPeripheral *)peripheral

此外,我不明白你为什么这样做cfbridgingrelease。只需将其传递为@[(id)yourCfuuidref]即可。你没有创造那种cfuuid。

答案 1 :(得分:1)

解决方案是存储ASCII UUID,并使用retrieveConnectedPeripherals和retreivePeripherals。存储指针可能失败,因为未保留数据。我还实现了tdevoy的解决方案。 didRetrievePeripherals被调用,但它的外围设备列表是空的,所以我添加了retrieveConnectedPeripherals和didRetrieveConnectedPeripherals,它被调用,并列出我成功连接到的外围设备。 (针对这些的Apple Prog。指南含糊不清,缺少示例代码。)