added delegate methods for device or status cell selection, added option to show connecting...

This commit is contained in:
matthias.ringwald 2010-02-22 22:25:54 +00:00
parent 0e92e71ed3
commit e1b2631cab
5 changed files with 69 additions and 44 deletions

View File

@ -32,6 +32,7 @@
#import <BTstack/BTstackManager.h>
@class BTstackManager;
@protocol BTDiscoveryDelegate;
typedef enum {
kInquiryInactive,
@ -42,6 +43,7 @@ typedef enum {
@interface BTDiscoveryViewController : UITableViewController<BTstackManagerListener>
{
BTstackManager *bt;
NSObject<BTDiscoveryDelegate> * _delegate;
UIActivityIndicatorView *deviceActivity;
UIActivityIndicatorView *bluetoothActivity;
UIFont * deviceNameFont;
@ -49,6 +51,15 @@ typedef enum {
InquiryState inquiryState;
int remoteNameIndex;
BOOL showIcons;
int connectingIndex;
}
-(void) markConnecting:(int)index; // use -1 for no connection active
@property (nonatomic, assign) NSObject<BTDiscoveryDelegate> * delegate;
@property (nonatomic, assign) BOOL showIcons;
@end
@protocol BTDiscoveryDelegate
@optional
-(BOOL)willSelectDeviceAtIndex:(int)deviceIndex; // returns NO to ignore selection
-(void)statusCellSelected;
@end

View File

@ -46,12 +46,14 @@
@implementation BTDiscoveryViewController
@synthesize showIcons;
@synthesize delegate = _delegate;
- (id) init {
self = [super initWithStyle:UITableViewStyleGrouped];
macAddressFont = [UIFont fontWithName:@"Courier New" size:[UIFont labelFontSize]];
deviceNameFont = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
inquiryState = kInquiryInactive;
connectingIndex = -1;
deviceActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[deviceActivity startAnimating];
@ -59,7 +61,7 @@
[bluetoothActivity startAnimating];
bt = [BTstackManager sharedInstance];
_delegate = nil;
return self;
}
@ -127,6 +129,7 @@
// e.g. self.myOutlet = nil;
}
// BTstackManagerListenerDelegate
-(void) activated{
[self reload];
}
@ -146,6 +149,10 @@
[self reload];
}
-(void) markConnecting:(int)index; {
connectingIndex = index;
[self reload];
}
#pragma mark Table view methods
@ -191,35 +198,37 @@
theLabel = @"Bluetooth not accessible!";
cell.accessoryView = nil;
} else {
#if 0
if (connectedDevice) {
theLabel = @"Disconnect";
cell.accessoryView = nil;
} else if (remoteDevice) {
}
#endif
if (connectingIndex >= 0) {
theLabel = @"Connecting...";
cell.accessoryView = bluetoothActivity;
} else {
#endif
switch (inquiryState){
case kInquiryInactive:
if ([bt numberOfDevicesFound] > 0){
theLabel = @"Find more devices...";
} else {
theLabel = @"Find devices...";
}
cell.accessoryView = nil;
break;
case kInquiryActive:
theLabel = @"Searching...";
cell.accessoryView = bluetoothActivity;
break;
case kInquiryRemoteName:
theLabel = @"Query device names...";
cell.accessoryView = bluetoothActivity;
break;
switch (inquiryState){
case kInquiryInactive:
if ([bt numberOfDevicesFound] > 0){
theLabel = @"Find more devices...";
} else {
theLabel = @"Find devices...";
}
cell.accessoryView = nil;
break;
case kInquiryActive:
theLabel = @"Searching...";
cell.accessoryView = bluetoothActivity;
break;
case kInquiryRemoteName:
theLabel = @"Query device names...";
cell.accessoryView = bluetoothActivity;
break;
}
}
// }
}
} else {
@ -280,15 +289,10 @@
}
// set accessory view
switch ([dev connectionState]) {
case kBluetoothConnectionNotConnected:
case kBluetoothConnectionConnected:
cell.accessoryView = nil;
break;
case kBluetoothConnectionConnecting:
case kBluetoothConnectionRemoteName:
cell.accessoryView = deviceActivity;
break;
if (idx == connectingIndex){
cell.accessoryView = deviceActivity;
} else {
cell.accessoryView = nil;
}
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0
@ -303,18 +307,19 @@
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// if (allowSelection) {
// return indexPath;
//}
if (!_delegate) return nil;
int index = [indexPath indexAtPosition:1];
if (index >= [bt numberOfDevicesFound]){
if ([_delegate respondsToSelector:@selector(statusCellSelected)]){
[_delegate statusCellSelected];
return nil;
}
}
if ([_delegate respondsToSelector:@selector(willSelectDeviceAtIndex:)] && [_delegate willSelectDeviceAtIndex:index]){
return indexPath;
}
return nil;
}

View File

@ -6,10 +6,11 @@
#import <Foundation/Foundation.h>
#import <BTstack/BTstackManager.h>
#import <BTstack/BTDiscoveryViewController.h>
@class BTstackManager;
@class BTDiscoveryViewController;
@interface TestBTstackManager : NSObject<BTstackManagerDelegate,BTstackManagerListener>{
@interface TestBTstackManager : NSObject<BTstackManagerDelegate,BTstackManagerListener,BTDiscoveryDelegate>{
BTstackManager *bt;
BTDiscoveryViewController* discoveryView;
}

View File

@ -6,7 +6,7 @@
#import "TestBTstackManager.h"
#import <BTstack/BTDevice.h>
#import <BTstack/BTDiscoveryViewController.h>
@implementation TestBTstackManager
@ -27,10 +27,19 @@
-(void) deviceInfo:(BTDevice*)device {
NSLog(@"Device Info: addr %@ name %@ COD 0x%06x", [device addressString], [device name], [device classOfDevice] );
}
- (BOOL)willSelectDeviceAtIndex:(int)deviceIndex {
BTDevice *device = [bt deviceAtIndex:deviceIndex];
NSLog(@"Device selected: addr %@ name %@ COD 0x%06x", [device addressString], [device name], [device classOfDevice] );
return NO;
}
-(void)statusCellSelected{
NSLog(@"statusCellSelected!");
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// create discovery controller
discoveryView = [[BTDiscoveryViewController alloc] init];
[discoveryView setDelegate:self];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:discoveryView];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:nav.view];

View File

@ -5,7 +5,6 @@
NEXT:
- Provide BTstackManager Objective-C class
- support visualization of connections in BTDiscoveryViewController
- implement l2cap code
- implement rfcomm code