检查是否显示通知警报 - 断开连接时蓝牙通知

时间:2013-11-25 22:24:05

标签: iphone objective-c uialertview core-bluetooth

我的iPhone应用使用以下代码连接到BLE外设:

[manager connectPeripheral:per.peripheral options:[NSDictionary dictionaryWithObject [NSNumber numberWithBool:YES] 
forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

我启用选项以在外围设备断开连接时通知用户,这只会在应用程序未被使用时发生(即iPhone被锁定或应用程序在后台)

当外围设备断开连接时,它会调用一个功能,在该功能中,我向用户显示警告,告诉用户它已断开连接:

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {    
    //Show alert to the user
    NSString *str = [[NSString alloc] initWithFormat:@"Peripheral disconnected. Try connecting again. Error: %@", error.localizedDescription];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Peripheral Disconnected"
                                                    message:str delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [self.navigationController popToRootViewControllerAnimated:NO];
}

我遇到的问题是,当iPhone被锁定且外围设备断开连接时,两个警报都会弹出,并使用户在两个警报上单击“确定”。如何检查是否显示“NotifyOnDisconnect”警报以取消显示其他警报?

1 个答案:

答案 0 :(得分:2)

您可以完全避免使用CBConnectPeripheralOptionNotifyOnDisconnectionKey并在-centralManager:didDisconnectPeripheral:实施中检查应用是否在后台,如果它位于后台,则会显示UILocalNotification 或< / em>如果应用位于前台,您现有的UIAlertView。这样,只会显示一个警报。