diff --git a/CocoaTouch/include/BTstack/BTDevice.h b/CocoaTouch/include/BTstack/BTDevice.h index 98cdb6b2d..f17673c6d 100644 --- a/CocoaTouch/include/BTstack/BTDevice.h +++ b/CocoaTouch/include/BTstack/BTDevice.h @@ -59,12 +59,16 @@ typedef enum { } BluetoothConnectionState; @interface BTDevice : NSObject { - bd_addr_t address; + + bd_addr_t address; // potential key + NSString * name; + uint32_t classOfDevice; + uint8_t pageScanRepetitionMode; uint16_t clockOffset; - uint32_t classOfDevice; - BluetoothConnectionState connectionState; + + BluetoothConnectionState connectionState; } - (void) setAddress:(bd_addr_t *)addr; diff --git a/CocoaTouch/include/BTstack/BTInquiryViewController.h b/CocoaTouch/include/BTstack/BTInquiryViewController.h index 907fbe15f..5489ba185 100644 --- a/CocoaTouch/include/BTstack/BTInquiryViewController.h +++ b/CocoaTouch/include/BTstack/BTInquiryViewController.h @@ -39,6 +39,11 @@ #include // for HCI_STATE +#define PREFS_SHARED_SUITE "ch.ringwald.BTstack" +#define PREFS_DEVICES "Devices" +#define PREFS_NAME "Name" +#define PREFS_LINK_KEY "LinkKey" + @class BTDevice; @protocol BTInquiryDelegate; @@ -61,6 +66,9 @@ typedef enum { bool allowSelection; bool showIcons; + // used to store link keys and remote names (soon :) + // NSMutableDictionary *deviceInfo; + // hacks bool stopRemoteNameGathering; bool restartInquiry; @@ -68,6 +76,9 @@ typedef enum { BTDevice *remoteDevice; // device for which connection is pending BTDevice *connectedDevice; // device to which we're connected bool notifyDelegateOnInquiryStopped; + + // 2.0 - 3.0 compatibilty + bool onSDK20; } - (void) startInquiry; @@ -80,6 +91,7 @@ typedef enum { @property (nonatomic, assign) bool showIcons; @property (nonatomic, retain) NSMutableArray *devices; @property (nonatomic, retain) id delegate; +// @property (nonatomic, retain) NSMutableDictionary *deviceInfo; @end @protocol BTInquiryDelegate diff --git a/CocoaTouch/src/BTInquiryViewController.m b/CocoaTouch/src/BTInquiryViewController.m index bf6b61767..20ad65cab 100644 --- a/CocoaTouch/src/BTInquiryViewController.m +++ b/CocoaTouch/src/BTInquiryViewController.m @@ -39,9 +39,18 @@ #import "BTDevice.h" #include - +#include #define INQUIRY_INTERVAL 3 +#ifndef __IPHONE_3_0 +#define __IPHONE_3_0 30000 +// SDK 30 defines missing in SDK 20 +@interface UITableViewCell (NewIn30) +- (id)initWithStyle:(int)style reuseIdentifier:(NSString *)reuseIdentifier; +@end + +#endif + static BTInquiryViewController *inqView; static btstack_packet_handler_t clientHandler; static uint8_t remoteNameIndex; @@ -62,6 +71,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe @implementation BTInquiryViewController @synthesize devices; +// @synthesize deviceInfo; @synthesize delegate; @synthesize allowSelection; @synthesize showIcons; @@ -86,6 +96,24 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe devices = [[NSMutableArray alloc] init]; inqView = self; + + // create suite preferences + //CFStringRef testValue = CFSTR("TEST!!!"); + //CFPreferencesSetAppValue (CFSTR(PREFS_DEVICES), testValue, CFSTR(PREFS_DEVICES)); + //CFPreferencesAppSynchronize (CFSTR(PREFS_SHARED_SUITE)); + + + // preferences + /* NSMutableDictionary *emptyDeviceInfo = [NSMutableDictionary dictionary]; + NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary]; + [defaultValues setObject:emptyDeviceInfo forKey:@PREFS_DEVICES]; + [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues]; + [[NSUserDefaults standardUserDefaults] addSuiteNamed:@PREFS_SHARED_SUITE]; + [self setDeviceInfo:[[NSUserDefaults standardUserDefaults] objectForKey:@PREFS_DEVICES] ]; + */ + + // check for the one missing method + onSDK20 = [UITableViewCell instancesRespondToSelector:@selector(initWithFrame:reuseIdentifier:)]; return self; } @@ -217,6 +245,12 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe [self getNextRemoteName]; break; + case HCI_EVENT_LINK_KEY_REQUEST: + // link key request + bt_flip_addr(event_addr, &packet[2]); + bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr); + break; + default: break; @@ -423,69 +457,83 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { - cell = [[[UITableViewCell alloc] initWithStyle:/* UITableViewCellStyleDefault = */0 reuseIdentifier:CellIdentifier] autorelease]; - // cell.selectionStyle = UITableViewCellSelectionStyleNone; + if(onSDK20){ + // @TODO 2.0 fix this: + // cell = [[UITableViewCell alloc] initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier; + } else { + cell = [[[UITableViewCell alloc] initWithStyle:/* UITableViewCellStyleDefault = */0 reuseIdentifier:CellIdentifier] autorelease]; + } } // Set up the cell... - NSString *label = nil; + NSString *theLabel = nil; + UIImage *theImage = nil; + UIFont *theFont = nil; + int idx = [indexPath indexAtPosition:1]; if (bluetoothState != HCI_STATE_WORKING || idx >= [devices count]) { if (bluetoothState == HCI_STATE_INITIALIZING){ - label = @"Activating BTstack..."; + theLabel = @"Activating BTstack..."; cell.accessoryView = bluetoothActivity; } else if (bluetoothState == HCI_STATE_OFF){ - label = @"Bluetooth not accessible!"; + theLabel = @"Bluetooth not accessible!"; cell.accessoryView = nil; } else { if (connectedDevice) { - label = @"Disconnect"; + theLabel = @"Disconnect"; cell.accessoryView = nil; } else if (remoteDevice) { - label = @"Connecting..."; + theLabel = @"Connecting..."; cell.accessoryView = bluetoothActivity; } else { switch (inquiryState){ case kInquiryInactive: if ([devices count] > 0){ - label = @"Find more devices..."; + theLabel = @"Find more devices..."; } else { - label = @"Find devices..."; + theLabel = @"Find devices..."; } cell.accessoryView = nil; break; case kInquiryActive: - label = @"Searching..."; + theLabel = @"Searching..."; cell.accessoryView = bluetoothActivity; break; case kInquiryRemoteName: - label = @"Query device names..."; + theLabel = @"Query device names..."; cell.accessoryView = bluetoothActivity; break; } } } } else { + BTDevice *dev = [devices objectAtIndex:idx]; - label = [dev nameOrAddress]; + + // pick font + theLabel = [dev nameOrAddress]; if ([dev name]){ - cell.font = deviceNameFont; + theFont = deviceNameFont; } else { - cell.font = macAddressFont; + theFont = macAddressFont; } + // pick an icon for the devices if (showIcons) { int major = ([dev classOfDevice] & 0x1f00) >> 8; if (major == 0x01) { - cell.image = [UIImage imageNamed:@"computer.png"]; + theImage = [UIImage imageNamed:@"computer.png"]; } else if (major == 0x02) { - cell.image = [UIImage imageNamed:@"smartphone.png"]; + theImage = [UIImage imageNamed:@"smartphone.png"]; } else if ( major == 0x05 && ([dev classOfDevice] & 0xff) == 0x40){ - cell.image = [UIImage imageNamed:@"keyboard.png"]; + theImage = [UIImage imageNamed:@"keyboard.png"]; } else { - cell.image = [UIImage imageNamed:@"bluetooth.png"]; + theImage = [UIImage imageNamed:@"bluetooth.png"]; } + } + + // set accessory view switch ([dev connectionState]) { case kBluetoothConnectionNotConnected: case kBluetoothConnectionConnected: @@ -497,7 +545,15 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe break; } } - cell.text = label; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0 + if (theLabel) cell.textLabel.text = theLabel; + if (theFont) cell.textLabel.font = theFont; + if (theImage) cell.imageView.image = theImage; +#else + if (theLabel) cell.text = theLabel; + if (theFont) cell.font = theFont; + if (theImage) cell.image = theImage; +#endif return cell; }