From 09f92ef2fe4e3a6f7dc32c2da05082b2aa49bbbe Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Sun, 18 Oct 2009 17:38:11 +0000 Subject: [PATCH] allow for BTInquiryDelegate that gets notified on device selection --- .../include/BTstack/BTInquiryViewController.h | 9 +++ CocoaTouch/src/BTInquiryViewController.m | 67 +++---------------- CocoaTouch/src/BTstackCocoaAppDelegate.h | 2 +- CocoaTouch/src/BTstackCocoaAppDelegate.m | 22 +++--- 4 files changed, 34 insertions(+), 66 deletions(-) diff --git a/CocoaTouch/include/BTstack/BTInquiryViewController.h b/CocoaTouch/include/BTstack/BTInquiryViewController.h index 25420a359..1ec368d3b 100644 --- a/CocoaTouch/include/BTstack/BTInquiryViewController.h +++ b/CocoaTouch/include/BTstack/BTInquiryViewController.h @@ -8,6 +8,9 @@ #include // for HCI_STATE +@class BTDevice; +@protocol BTInquiryDelegate; + typedef enum { kInquiryInactive, kInquiryActive, @@ -22,10 +25,16 @@ typedef enum { UIActivityIndicatorView *bluetoothActivity; UIFont * deviceNameFont; UIFont * macAddressFont; + id delegate; } - (void) setBluetoothState:(HCI_STATE)state; - (void) setInquiryState:(InquiryState)state; - (InquiryState) inquiryState; - (HCI_STATE) bluetoothState; @property (nonatomic, retain) NSMutableArray *devices; +@property (nonatomic, retain) id delegate; +@end + +@protocol BTInquiryDelegate +-(void) deviceChoosen:(BTInquiryViewController *) inqView device:(BTDevice*) device; @end diff --git a/CocoaTouch/src/BTInquiryViewController.m b/CocoaTouch/src/BTInquiryViewController.m index dce29ae32..7d55b0fce 100644 --- a/CocoaTouch/src/BTInquiryViewController.m +++ b/CocoaTouch/src/BTInquiryViewController.m @@ -10,8 +10,7 @@ @implementation BTInquiryViewController @synthesize devices; - -// #define MOCKUP +@synthesize delegate; int mock_state = 0; @@ -170,63 +169,17 @@ int mock_state = 0; // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil]; // [self.navigationController pushViewController:anotherViewController]; // [anotherViewController release]; - -#ifdef MOCKUP - switch (mock_state) { - - case 0: - bluetoothState = HCI_STATE_WORKING; - [tableView reloadData]; - mock_state++; - break; - - case 1: - inquiryState = kInquiryActive; - [tableView reloadData]; - mock_state++; - - case 2: - case 3: { - BTDevice * dev = [[BTDevice alloc] init]; - bd_addr_t addr = { mock_state, mock_state, mock_state, mock_state, mock_state, mock_state}; - [dev setAddress:&addr]; - [devices addObject:dev]; - [tableView reloadData]; - mock_state++; - break; - } - case 4: { - inquiryState = kInquiryRemoteName; - BTDevice * dev; - dev = [devices objectAtIndex:0]; - // [dev setConnectionState:kBluetoothConnectionRemoteName]; - dev = [devices objectAtIndex:1]; - // [dev setConnectionState:kBluetoothConnectionRemoteName]; - [tableView reloadData]; - mock_state++; - break; - } - case 5: { - BTDevice * dev; - dev = [devices objectAtIndex:0]; - [dev setName:@"Fake Device 1"]; - [dev setConnectionState:kBluetoothConnectionNotConnected]; - [tableView reloadData]; - mock_state++; - break; - } - case 6: { - inquiryState = kInquiryInactive; - BTDevice * dev; - dev = [devices objectAtIndex:1]; - [dev setConnectionState:kBluetoothConnectionNotConnected]; - [dev setName:@"Fake Device 2"]; - [tableView reloadData]; - mock_state = 1; - break; + + // valid selection? + int idx = [indexPath indexAtPosition:1]; + if (bluetoothState == HCI_STATE_WORKING && inquiryState == kInquiryInactive && idx < [devices count]){ + if (delegate && [delegate respondsToSelector:@selector(deviceChoosen:device:)]){ + NSLog(@"delegate would respond"); + [delegate deviceChoosen:self device:[devices objectAtIndex:idx]]; } + } else { + [tableView deselectRowAtIndexPath:indexPath animated:TRUE]; } -#endif } diff --git a/CocoaTouch/src/BTstackCocoaAppDelegate.h b/CocoaTouch/src/BTstackCocoaAppDelegate.h index 0dea35d66..773f95d7f 100644 --- a/CocoaTouch/src/BTstackCocoaAppDelegate.h +++ b/CocoaTouch/src/BTstackCocoaAppDelegate.h @@ -8,7 +8,7 @@ #import "BTInquiryViewController.h" -@interface BTstackCocoaAppDelegate : NSObject { +@interface BTstackCocoaAppDelegate : NSObject { UIWindow *window; BTInquiryViewController *inqView; bool inqActive; diff --git a/CocoaTouch/src/BTstackCocoaAppDelegate.m b/CocoaTouch/src/BTstackCocoaAppDelegate.m index 1ffd5d7b2..6880f17bb 100644 --- a/CocoaTouch/src/BTstackCocoaAppDelegate.m +++ b/CocoaTouch/src/BTstackCocoaAppDelegate.m @@ -130,16 +130,16 @@ void showAlert(NSString *title, NSString *message){ break; case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: + bt_flip_addr(event_addr, &data[3]); + BTDevice *dev = [self getDeviceForAddress:&event_addr]; + if (!dev) break; + [dev setConnectionState:kBluetoothConnectionNotConnected]; if (data[2] == 0) { - bt_flip_addr(event_addr, &data[3]); - BTDevice *dev = [self getDeviceForAddress:&event_addr]; - if (!dev) break; - [dev setConnectionState:kBluetoothConnectionNotConnected]; [dev setName:[NSString stringWithUTF8String:(const char *) &data[9]]]; - [[inqView tableView] reloadData]; - remoteNameIndex++; - [self getNextRemoteName]; } + [[inqView tableView] reloadData]; + remoteNameIndex++; + [self getNextRemoteName]; break; case L2CAP_EVENT_CHANNEL_OPENED: @@ -187,6 +187,7 @@ void showAlert(NSString *title, NSString *message){ inqView = [[BTInquiryViewController alloc] init]; devices = [[NSMutableArray alloc] init]; [inqView setDevices:devices]; + [inqView setDelegate:self]; [window addSubview:[inqView view]]; @@ -201,7 +202,7 @@ void showAlert(NSString *title, NSString *message){ if (res){ [inqView setBluetoothState:HCI_STATE_OFF]; showAlert(@"Bluetooth not accessible!", - @"The connection to BTstack failed!\n" + @"Connection to BTstack failed!\n" "Please make sure that BTstack is installed correctly."); } else { bt_register_packet_handler(packet_handler); @@ -209,6 +210,11 @@ void showAlert(NSString *title, NSString *message){ } } +/** BTInquiryDelegate */ +-(void) deviceChoosen:(BTInquiryViewController *) inqView device:(BTDevice*) device{ + NSLog(@"deviceChoosen %@", [device toString]); +} + - (void)dealloc { [window release]; [super dealloc];