[英]iOS Bluetooth retrievePeripherals causes EXC_BAD_ACCESS crash
I need to periodically reconnect to a peripheral, but trying to re-connect crashes. 我需要定期重新连接到外围设备,但尝试重新连接崩溃。
First, my central scans for peripherals' UUID, then it connects, then it stores the peripheral.UUID into a CFUUIDRef variable, and then disconnects. 首先,我的中央设备扫描外围设备的UUID,然后进行连接,然后将外围设备.UUID存储到CFUUIDRef变量中,然后断开连接。 Next, to re-connect it retrieves the UUID from variable, then either crashes with "EXC_BAD_ACCESS" when it does retreivePeripherals, or a little later when it does the subsequent didRetrievePeripherals.
接下来,要重新连接,它会从变量中检索UUID,然后在执行retreivePeripherals时崩溃,并以“ EXC_BAD_ACCESS”崩溃,或者在随后进行后续的didRetrievePeripherals时崩溃。
Maybe I'm not storing the UUID correctly, but I don't see the bug. 也许我没有正确存储UUID,但没有看到该错误。 Thanks.
谢谢。
Here's the code .......... 这是代码........
-(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];
}
Let me preface this by saying I'm writing this from my cell phone in a car, so excuse me for the poor formatting. 首先,我要说的是我是用手机在汽车上写的,因此,请原谅我格式化不当。 Ill just point out the main problem I see.
我只想指出我所遇到的主要问题。 You're not adopting the right delegate method:
您没有采用正确的委托方法:
It is: 它是:
- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *) peripherals
Not this: 不是这个:
- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(CBPeripheral *)peripheral
Also I don't see why you're doing that cfbridgingrelease
. 我也看不到你为什么要
cfbridgingrelease
。 Just pass it in as @[(id)yourCfuuidref]
. 只需将其作为
@[(id)yourCfuuidref]
。 You didn't create that cfuuid. 您没有创建该cfuuid。
The solution was to store the ASCII UUID, and use use retrieveConnectedPeripherals as well as retreivePeripherals. 解决方案是存储ASCII UUID,并使用retrieveConnectedPeripherals和retreivePeripherals。 Storing the pointer probably failed because the data was not retained.
存储指针可能失败,因为未保留数据。 I also implemented tdevoy's solution.
我还实现了tdevoy的解决方案。 didRetrievePeripherals gets called but its peripherals list is empty, so I added to it retrieveConnectedPeripherals and didRetrieveConnectedPeripherals, which gets called, and lists the peripheral, which I successfully connect to.
didRetrievePeripherals被调用,但是其外围设备列表为空,因此我在其中添加了retrieveConnectedPeripherals和didRetrieveConnectedPeripherals,它们被调用并列出了我成功连接到的外围设备。 (Apple Prog. guide for these is vague and lacks example code.)
(有关这些内容的Apple Prog。指南含糊不清,缺少示例代码。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.