-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWiiRemoteDiscovery.m
More file actions
148 lines (114 loc) · 3.41 KB
/
WiiRemoteDiscovery.m
File metadata and controls
148 lines (114 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// WiiRemoteDiscovery.m
// DarwiinRemote
//
// Created by Ian Rickard on 12/9/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import <IOBluetooth/objc/IOBluetoothDevice.h>
#import "WiiRemoteDiscovery.h"
@implementation WiiRemoteDiscovery
- (id) init{
inquiry = nil;
_delegate = nil;
return self;
}
+ (WiiRemoteDiscovery*) discoveryWithDelegate:(id)delegate {
WiiRemoteDiscovery *out = [[WiiRemoteDiscovery alloc] init];
[out setDelegate: delegate];
return out;
}
- (void)setDelegate:(id)delegate{
_delegate = delegate;
}
- (IOReturn)start {
if (nil != inquiry) {
NSLog(@"Warning: Attempted to start already-started WiiRemoteDiscovery");
return kIOReturnSuccess;
}
if (nil == _delegate) {
NSLog(@"Warning: starting WiiRemoteDiscovery without delegate set");
}
inquiry = [IOBluetoothDeviceInquiry inquiryWithDelegate: self];
if (nil == inquiry) {
NSLog(@"Error: Failed to alloc IOBluetoothDeviceInquiry");
return kIOReturnNotAttached;
}
// calling initWithDelegate after this isn't correct. see:
// http://lists.apple.com/archives/Bluetooth-dev/2005/Aug/msg00006.html
// retain ourselves while there's an outstanding inquiry, don't want to go disappearing
// before we get called as a delegate
[self retain];
IOReturn ret = [inquiry start];
if (ret == kIOReturnSuccess){
// copied from old code, is this necesary? Shouldn't it already be retained once when it was initted?
[inquiry retain];
} else {
NSLog(@"Error: Inquiry did not start, error %d", ret);
[inquiry setDelegate:nil];
[inquiry release];
inquiry = nil;
}
return ret;
}
- (IOReturn)stop {
if (nil == inquiry) {
NSLog(@"Warning: Attempted to stop already-stopped WiiRemoteDiscovery");
return kIOReturnSuccess;
}
IOReturn ret = [inquiry stop];
if (ret != kIOReturnSuccess && ret != kIOReturnNotPermitted) {
// kIOReturnNotPermitted is if it's already stopped
NSLog(@"Error: Inquiry did not stop, error %d", ret);
}
[inquiry setDelegate:nil];
// and release the hold on ourselves
[self release];
[inquiry release];
inquiry = nil;
return ret;
}
- (void)dealloc {
if (nil != inquiry)
[self stop];
[super dealloc];
}
/////// IOBluetoothDeviceInquiry delegates //////
- (void) deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
error:(IOReturn)error aborted:(BOOL)aborted {
if (aborted) return; // called by stop ;)
if (kIOReturnSuccess != error) {
[_delegate WiiRemoteDiscoveryError:error];
[self stop];
return;
}
//[inquiry clearFoundDevices];
IOReturn ret = [inquiry start];
if (ret != kIOReturnSuccess) {
NSLog(@"Error: Restarting Inquiry failed: %d", ret);
[_delegate WiiRemoteDiscoveryError: ret];
[inquiry stop];
}
}
- (void)checkDevice:(IOBluetoothDevice*)device {
if ([[device getName] isEqualToString:@"Nintendo RVL-CNT-01"]){
WiiRemote *wii = [[WiiRemote alloc] init];
IOReturn ret = [wii connectTo:device];
if (ret == kIOReturnSuccess) {
[_delegate WiiRemoteDiscovered: wii];
} else {
[wii release];
// initWithDevice generated error message
[_delegate WiiRemoteDiscoveryError: ret];
}
}
}
- (void)deviceInquiryDeviceNameUpdated:(IOBluetoothDeviceInquiry*)sender
device:(IOBluetoothDevice*)device devicesRemaining:(int)devicesRemaining {
[self checkDevice:device];
}
- (void) deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)sender
device:(IOBluetoothDevice*)device {
[self checkDevice:device];
}
@end