From d34a6e86ce1b144f93151860e44f041e2964e3ce Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Wed, 4 Apr 2012 16:19:26 +0000 Subject: [PATCH] prevent BTdaemon from stalling when Preferences.app with BTstack Prefs enters suspend mode --- PrefsBundle/BluetoothController.m | 20 +++---- PrefsBundle/BluetoothTableViewAdapter.m | 4 +- PrefsBundle/Makefile | 1 + PrefsBundle/PrefsViewController.m | 78 ++++++++++++++----------- 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/PrefsBundle/BluetoothController.m b/PrefsBundle/BluetoothController.m index 57e12260c..82181ab24 100644 --- a/PrefsBundle/BluetoothController.m +++ b/PrefsBundle/BluetoothController.m @@ -100,7 +100,7 @@ static BluetoothController* sharedInstance = nil; NSLog(@"Cannot connect to BTdaemon!"); return NO; } - NSLog(@"Connected to BTdaemon!"); + // NSLog(@"Connected to BTdaemon!"); isConnected = YES; } @@ -111,7 +111,7 @@ static BluetoothController* sharedInstance = nil; } -(void)connectionBroke { - NSLog(@"BTstack stopped"); + // NSLog(@"BTstack stopped"); [self resetState]; [self open]; [listener bluetoothStateChanged]; @@ -119,7 +119,7 @@ static BluetoothController* sharedInstance = nil; -(void)close{ if (isConnected) { - NSLog(@"Disconnected from BTdaemon!"); + // NSLog(@"Disconnected from BTdaemon!"); bt_close(); } [self resetState]; @@ -165,7 +165,7 @@ static BluetoothController* sharedInstance = nil; return; } - NSLog(@"bluetoothStateChanged %u", type); + // NSLog(@"bluetoothStateChanged %u", type); state = kIdle; @@ -180,11 +180,11 @@ static BluetoothController* sharedInstance = nil; switch(packet[0]){ case BTSTACK_EVENT_STATE: hci_state = packet[2]; - NSLog(@"new BTSTACK_EVENT_STATE %u", hci_state); + // NSLog(@"new BTSTACK_EVENT_STATE %u", hci_state); break; case BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED: system_bluetooth = packet[2]; - NSLog(@"new BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", system_bluetooth); + // NSLog(@"new BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", system_bluetooth); break; default: break; @@ -214,16 +214,16 @@ static BluetoothController* sharedInstance = nil; case kW4BTstackOffToEnableSystem: if (packet[0] == BTSTACK_EVENT_STATE) { if (hci_state == HCI_STATE_OFF) { - NSLog(@"Sending set system bluetooth enable A"); + // NSLog(@"Sending set system bluetooth enable A"); bt_send_cmd(&btstack_set_system_bluetooth_enabled, 1); } } if (packet[0] == BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED) { if (system_bluetooth == 0){ - NSLog(@"Sending set system bluetooth enable B"); + // NSLog(@"Sending set system bluetooth enable B"); bt_send_cmd(&btstack_set_system_bluetooth_enabled, 1); } else { - NSLog(@"Sending set system bluetooth enable DONE"); + // NSLog(@"Sending set system bluetooth enable DONE"); state = kIdle; } } @@ -238,7 +238,7 @@ static BluetoothController* sharedInstance = nil; } -(void)requestType:(BluetoothType_t)bluetoothType{ - NSLog(@"bluetoothChangeRequested: old %u, new %u", [self bluetoothType], bluetoothType); + // NSLog(@"bluetoothChangeRequested: old %u, new %u", [self bluetoothType], bluetoothType); // ignore taps during transition if (state != kIdle) { diff --git a/PrefsBundle/BluetoothTableViewAdapter.m b/PrefsBundle/BluetoothTableViewAdapter.m index d52efa553..0e7707683 100644 --- a/PrefsBundle/BluetoothTableViewAdapter.m +++ b/PrefsBundle/BluetoothTableViewAdapter.m @@ -61,7 +61,7 @@ } -(void) loggingSwitchToggled { - NSLog(@"Logging: %u", loggingSwitch.on); + // NSLog(@"Logging: %u", loggingSwitch.on); CFPropertyListRef on; if (loggingSwitch.on){ on = kCFBooleanTrue; @@ -98,7 +98,7 @@ BluetoothType_t bluetoothType = [[BluetoothController sharedInstance] targetType]; BOOL activity = [[BluetoothController sharedInstance] isActive]; - NSLog(@"tableView update: type %u, active %u", bluetoothType, activity); + // NSLog(@"tableView update: type %u, active %u", bluetoothType, activity); switch ([indexPath section]) { case 0: diff --git a/PrefsBundle/Makefile b/PrefsBundle/Makefile index 3bf50751c..4b5fdd852 100644 --- a/PrefsBundle/Makefile +++ b/PrefsBundle/Makefile @@ -1,3 +1,4 @@ +export TARGET=iphone:latest:4.0 include $(THEOS)/makefiles/common.mk BUNDLE_NAME = BTstack diff --git a/PrefsBundle/PrefsViewController.m b/PrefsBundle/PrefsViewController.m index b25938e27..7388066ab 100644 --- a/PrefsBundle/PrefsViewController.m +++ b/PrefsBundle/PrefsViewController.m @@ -52,15 +52,13 @@ BluetoothController *bluetoothController; UIView *_wrapperView; // for < 3.2 UITableView *tableView; - BOOL initialized; @end @implementation BluetoothPSViewController - (id)initForContentSize:(CGSize)size { - NSLog(@"initForContentSize"); - initialized = NO; + // NSLog(@"initForContentSize"); if ([PSViewController instancesRespondToSelector:@selector(initForContentSize:)]) { if ((self = [super initForContentSize:size])) { CGRect frame; @@ -77,8 +75,9 @@ - (void)dealloc { - NSLog(@"dealloc"); + // NSLog(@"dealloc"); + [[BluetoothController sharedInstance] close]; [[BluetoothController sharedInstance] setListener:nil]; [tableViewAdapter release]; @@ -88,8 +87,6 @@ [_wrapperView release]; - initialized = NO; - [super dealloc]; } @@ -98,18 +95,37 @@ return [super view] ? [super view] : _wrapperView; } --(void)myInit{ - - if (initialized) return; - - initialized = YES; - - NSLog(@"myInit"); +-(void)didEnterBackground:(id)object{ + // NSLog(@"didEnterBackground"); + // close connection to BTdaemon + [[BluetoothController sharedInstance] close]; +} +-(void)willEnterForeground:(id)object{ + // NSLog(@"willEnterForeground"); + // open connection to BTdaemon + [[BluetoothController sharedInstance] open]; +} + +-(void)viewWillAppear:(BOOL)animated { + // NSLog(@"viewWillAppear"); + // open connection to BTdaemon + [[BluetoothController sharedInstance] open]; +} + +-(void) viewDidDisappear:(BOOL)animated { + // NSLog(@"viewDidDisappear"); + // close connection to BTdaemon + [[BluetoothController sharedInstance] close]; +} + +-(void)viewDidLoad { + // setup view + // NSLog(@"initialize"); + ((UINavigationItem*)[super navigationItem]).title = @"BTstack"; UIView *view = [self view]; - tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped]; tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; @@ -120,27 +136,21 @@ [view addSubview:tableView]; + // setup Bluetooth Controller [[BluetoothController sharedInstance] setListener:tableViewAdapter]; - [[BluetoothController sharedInstance] open]; + + // register for backrounding events + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(didEnterBackground:) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(willEnterForeground:) + name:UIApplicationWillEnterForegroundNotification + object:nil]; } --(void)viewDidLoad -{ - NSLog(@"viewDidLoad"); - [super viewDidLoad]; - [self myInit]; -} - -- (void)viewWillBecomeVisible:(void *)source -{ - NSLog(@"viewWillBecomeVisible %@", source); - [self myInit]; - [super viewWillBecomeVisible:source]; -} - -- (void)viewWillAppear:(BOOL)animated -{ - NSLog(@"viewWillAppear"); - [self myInit]; -} @end