I've spent two days googling and reading the Bluetooth programming guide while trying to piece together a small Mac app that will retrieve images from a drop folder and send any new files to a predetermined device over Bluetooth. There doesn't seem to be many good examples available.
I'm at the point where I'm able to spawn the Bluetooth Service Browser and select the device and its OBEX service, establishing a service and creating a connection, but then nothing more happens. Could anyone please point me in the direction of/show me a simple example that would work?
AppDelegate source code enclosed. Thanks for reading!
#import "AppDelegate.h" @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { IOBluetoothServiceBrowserController *browser = [IOBluetoothServiceBrowserController serviceBrowserController:0]; [browser runModal]; //IOBluetoothSDPServiceRecord IOBluetoothSDPServiceRecord *result = [[browser getResults] objectAtIndex:0]; [self describe:result]; if ([[result.device.name substringToIndex:8] isEqualToString:@"Polaroid"]) { printer = result.device; serviceRecord = result; [self testPrint]; } else { NSLog(@"%@ is not a valid device", result.device.name); } } - (void) testPrint { currentFilePath = @"/Users/oyvind/Desktop/_DSC8797.jpg"; [self sendFile:currentFilePath]; } - (void) sendFile:(NSString *)filePath { IOBluetoothOBEXSession *obexSession = [[IOBluetoothOBEXSession alloc] initWithSDPServiceRecord:serviceRecord]; if( obexSession != nil ) { NSLog(@"OBEX Session Established"); OBEXFileTransferServices *fst = [OBEXFileTransferServices withOBEXSession:obexSession]; OBEXDelegate *obxd = [[OBEXDelegate alloc] init]; [obxd setFile:filePath]; [fst setDelegate:obxd]; OBEXError cnctResult = [fst connectToObjectPushService]; if( cnctResult != kIOReturnSuccess ) { NSLog(@"Error creating connection"); return; } else { NSLog(@"OBEX Session Created. Sending file: %@", filePath); [fst sendFile:filePath]; [printer openConnection]; } } else { NSLog(@"Error creating OBEX session"); NSLog(@"Error sending file"); } } @end
OK; here's what ultimately became the core parts of the functionality. The application I made was a sort of print server for Polaroid instant printers that would only accept images over Object Push.
First, ensure watched folder exists.
Then scan for available printers:
That last function call is to the BluetoothPushDevice's built-in method to test the connection. Here is the delegate handler for the response:
Upon server start, this method will run in response to a timer tick or manual scan:
When new files are found, ww first need to find a device that is not busy recieving a file of printing it:
Then a file is sent with this method:
Upon transfer complete, this delegate method is called:
I hope this helps someone!