Detecting Wii Remote via Mac OSX Terminal
Sunday, January 2nd, 2011The type of Wii Hacks that interest me involve using the Wii Remote as an input device. The Wii Remote offers a lot of possibilities because it has several buttons, an accelerometer and an IR camera.
My first step into this world has involved basic detection of the device. Without using any fancy graphics or animations, and using code that I have found on different websites, I have been able to put together an application that uses the Mac Terminal to detect the Wii Remote. I am not yet able to connect to the Wii Remote or fetch any output from the Wii Remote.
Detect The Device
The first programming step is to detect the device. I had never done any Bluetooth programming and knew very little about the technology. Outside of Apple’s official documentation, it was a struggle to find much help in this area. Eventually I was able to piece something together.
#include <Foundation/Foundation.h>
#include <Cocoa/Cocoa.h>
#include <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
#include <IOBluetoothUI/IOBluetoothUI.h>int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@”start bluetooth search”);// Setup the inquiry to search for available devices
IOBluetoothDeviceInquiry *d = [[IOBluetoothDeviceInquiry new] init];
[d setInquiryLength: 5];
[d setUpdateNewDeviceNames: TRUE];
[d start];[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 7]];
[d stop];// Create an array of foundDevices
NSArray *deviceList = [d foundDevices];
NSLog(@”found %d devices”, [deviceList count]);// Loop through all the foundDevices
for(int i=0;i < [deviceList count]; i++) {NSScanner *theScanner = [NSScanner scannerWithString:[NSString stringWithFormat:@"%@", [deviceList objectAtIndex:i]]];
NSString *tagDeviceName = @”mName – “;
NSString *tagEndLine = @”\n”;NSString *currentDeviceName;
// extract the mName from the current array value
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString:tagDeviceName intoString:NULL];
[theScanner scanString:tagDeviceName intoString:NULL];
[theScanner scanUpToString:tagEndLine intoString:¤tDeviceName];
} // end [theScanner isAtEnd]NSLog(@”device name: %@”, currentDeviceName);
} // end for loop
[pool release];
return 0;}
That is all I have at the moment, and it has been a bit of a struggle to get this far. I have found very little sample code, so I am piecing this together bit by bit.
This should work with any Bluetooth device and will work with multiple devices at the same time. If you are using a Wii Remote, you need to press the 1 and 2 buttons at the same time, during the detection.