mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-09 15:39:08 +00:00
allow for BTInquiryDelegate that gets notified on device selection
This commit is contained in:
parent
287f9f01fb
commit
09f92ef2fe
@ -8,6 +8,9 @@
|
||||
|
||||
#include <btstack/hci_cmds.h> // for HCI_STATE
|
||||
|
||||
@class BTDevice;
|
||||
@protocol BTInquiryDelegate;
|
||||
|
||||
typedef enum {
|
||||
kInquiryInactive,
|
||||
kInquiryActive,
|
||||
@ -22,10 +25,16 @@ typedef enum {
|
||||
UIActivityIndicatorView *bluetoothActivity;
|
||||
UIFont * deviceNameFont;
|
||||
UIFont * macAddressFont;
|
||||
id<BTInquiryDelegate> delegate;
|
||||
}
|
||||
- (void) setBluetoothState:(HCI_STATE)state;
|
||||
- (void) setInquiryState:(InquiryState)state;
|
||||
- (InquiryState) inquiryState;
|
||||
- (HCI_STATE) bluetoothState;
|
||||
@property (nonatomic, retain) NSMutableArray *devices;
|
||||
@property (nonatomic, retain) id<BTInquiryDelegate> delegate;
|
||||
@end
|
||||
|
||||
@protocol BTInquiryDelegate
|
||||
-(void) deviceChoosen:(BTInquiryViewController *) inqView device:(BTDevice*) device;
|
||||
@end
|
||||
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#import "BTInquiryViewController.h"
|
||||
|
||||
@interface BTstackCocoaAppDelegate : NSObject <UIApplicationDelegate> {
|
||||
@interface BTstackCocoaAppDelegate : NSObject <UIApplicationDelegate,BTInquiryDelegate> {
|
||||
UIWindow *window;
|
||||
BTInquiryViewController *inqView;
|
||||
bool inqActive;
|
||||
|
@ -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];
|
||||
|
Loading…
Reference in New Issue
Block a user