2013-09-24 118 views
1

我開發了一個藍牙應用程序,它在iOS 6上運行良好,但是當我在iOS 7上運行它時,應用程序在-didDiscoverPeripheral回調中崩潰。崩潰信息表明在CBPeripheral對象上調用了一個發行版。我已經使用ARC內存管理,這裏是我的聲明中,CBPeripheral對象和回調代碼初始化:iOS 7中的核心藍牙故障

@interface BrLEDiscovery() <CBCentralManagerDelegate, CBPeripheralDelegate> { 
    CBCentralManager *centralManager; 
    CBPeripheral *currentperipheral; 
    BOOL    pendingInit; 
} 


- (id) init 
{ 
    self = [super init]; 
    if (self) { 
     pendingInit = YES; 
     centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()]; 
     currentperipheral=[[CBPeripheral alloc]init]; 
     founddevice=FALSE; 
     foundPeripherals = [[NSMutableArray alloc] init]; 
     connectedServices = [[NSMutableArray alloc] init]; 

    } 
    return self; 
} 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
{ NSLog(@"Found Peripheral %@",[peripheral name]); 

    if ([[peripheral name] isEqualToString:peripheralname]) { 
     founddevice=TRUE; 
     currentperipheral=peripheral; 

     if (![foundPeripherals containsObject:peripheral]) { 
      [foundPeripherals addObject:peripheral]; 
      [discoveryDelegate discoveryDidRefresh]; 
      [discoveryDelegate DiscoveryDidFindDevice:peripheral]; 

     } 

     [RecursiveScanTimer invalidate]; 
    } 


} 
+0

什麼是錯誤?請張貼它。考慮到流程控制和語法,代碼本身看起來或多或少都不錯。 – allprog

回答

2

從你的描述,這是非常有可能要在這一行崩潰:

currentperipheral=peripheral; 

對我來說,在你的init中分配一個CBPeripheral對象有點麻煩,然後在未知的時間(尤其是弧)分配外設。如果要繼續參考發現的外設,只需在界面中創建一個CBPeripheral對象,保留髮現的外設並將其分配給接口外設。

剛剛嘗試這樣的事:

currentPeripheral = [discoveredPeripheral retain];//where discoveredPeripheral is the one given in the delegate callback 

我個人不使用圓弧任何我corebluetooth相關applications..You的需要非常小心與corebluetooth框架和外圍引用雖然...它變得雜亂。注意:如果你並不總是需要與外設直接接觸,那麼保留CFUUIDRef(或者identifier for iOS 7)的記錄通常很方便,只要你需要它就可以檢索外設。祝你好運!

+0

非常感謝,在界面中聲明它,而不是在init中分配它似乎工作。我仍然不明白爲什麼當我嘗試在回調中重新分配init變量時初始化的實例變量被釋放。事實上,這只是在iOS7上發生,這更加令我困惑。 ARC中缺少一些基本的東西嗎? – Yash

+1

漂亮! @Yash永遠不會分配一個'CBPeripheral'對象。在初始化中使用'nil'。在iOS7中發生崩潰的原因可能是CBPeripheral類的dealloc方法中的一些內部變化。沒有崩潰報告這很難說。 – allprog

+0

allprog。是的,我剛剛從負載blePeripheral聲明中刪除我的alloc init,並嘿presto,沒有更多的VC dealloc崩潰 –