Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@crazycoder1999
Created July 18, 2012 23:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save crazycoder1999/3139668 to your computer and use it in GitHub Desktop.
Save crazycoder1999/3139668 to your computer and use it in GitHub Desktop.
BT Communication On OSX
//sample of a bluetooth RfComm COmmunication between a GPS and OSX.
//more information on: http://pestohacks.blogspot.it/2012/07/make-osx-talks-with-bluetooth-gps.html
//let's go on.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"ok, go on"); //
btDevice = nil;
IOBluetoothDeviceInquiry *ibdi = [IOBluetoothDeviceInquiry inquiryWithDelegate:self]; //inquiry, have delegates methoeds
[ibdi setUpdateNewDeviceNames:YES]; //Yes, I want also names for the bt devices found.
[ibdi start]; //start inquiry
}
//not important
- (IBAction)sliderChanged:(id)sender {
NSLog(@"am I changed? %d",[_mySlider intValue]);
}
//not important.
- (IBAction)hitMeClicked:(id)sender {
NSLog(@"someone hitted me... OUCH!");
[_myDescription setTitleWithMnemonic:@"fuck you"];
}
//---------------------------------------------------------------------------------------------------------------------------
/*! @method deviceInquiryStarted
@discussion This message will be delivered when the inquiry actually starts. Since the inquiry could be throttled, this
message may not be received immediately after called -start.
@param sender Inquiry object that sent this delegate message.
*/
- (void) deviceInquiryStarted:(IOBluetoothDeviceInquiry*)sender{
NSLog(@"Let's start inquiry devices around me");
}
//---------------------------------------------------------------------------------------------------------------------------
/*! @method deviceInquiryDeviceFound
@discussion A new device has been found. You do not need to retain the device - it will be held in the internal
storage of the inquiry, and can be accessed later using -foundDevices.
@param sender Inquiry object that sent this delegate message.
@param device IOBluetoothDevice that was found.
*/
- (void) deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)sender device:(IOBluetoothDevice*)device{
NSLog(@"found this device %@ ", [device getAddressString]);
}
//---------------------------------------------------------------------------------------------------------------------------
/*! @method deviceInquiryUpdatingDeviceNamesStarted
@discussion The inquiry has begun updating device names that were found during the search.
@param sender Inquiry object that sent this delegate message.
@param devicesRemaining Number of devices remaining to update.
*/
- (void) deviceInquiryUpdatingDeviceNamesStarted:(IOBluetoothDeviceInquiry*)sender devicesRemaining:(uint32_t)devicesRemaining{
NSLog(@"updating names for: %d",devicesRemaining);
}
//---------------------------------------------------------------------------------------------------------------------------
/*! @method deviceInquiryDeviceNameUpdated
@discussion A device name has been retrieved. Also indicates how many devices are left to be updated.
@param sender Inquiry object that sent this delegate message.
@param device IOBluetoothDevice that was updated.
@param devicesRemaining Number of devices remaining to update.
*/
- (void) deviceInquiryDeviceNameUpdated:(IOBluetoothDeviceInquiry*)sender device:(IOBluetoothDevice*)device devicesRemaining:(uint32_t)devicesRemaining{
NSString *btDevName = [device getName]; //this is a deprecated methoed.
if ([btDevName isEqualToString:@"HBTGPS"]) {
NSLog(@"That's my boy! %@",btDevName);
btDevice = device;
}
}
//---------------------------------------------------------------------------------------------------------------------------
/*! @method deviceInquiryComplete
@discussion When the inquiry is completely stopped, this delegate method will be invoked. It will supply an error
code value, kIOReturnSuccess if the inquiry stopped without problem, otherwise a non-kIOReturnSuccess
error code will be supplied.
@param sender Inquiry object that sent this delegate message.
@param error Error code. kIOReturnSuccess if the inquiry completed without incident.
@param aborted TRUE if user called -stop on the inquiry.
*/
- (void) deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender error:(IOReturn)error aborted:(BOOL)abort{
if(btDevice != nil) { //so I have something to query.
NSLog(@"I gottcha!");
IOReturn ret = [btDevice performSDPQuery:self]; //some delegate methoed called after SDP is finished.
if (ret != kIOReturnSuccess) {
NSLog(@"smthg went wrong");
return;
}
BluetoothRFCOMMChannelID rfCommChan;
if([self findRfCommChannel:&rfCommChan] != kIOReturnSuccess) //check If the device have RFCOMM channel.
return;
NSLog(@"Found rfcomm channel on device.. %d",rfCommChan);
[self openConnection:&rfCommChan];
} else {
NSLog(@"Not Found!");
}
}
//called during SDP step.
- (void)sdpQueryComplete:(IOBluetoothDevice *)device status:(IOReturn)status {
if (status != kIOReturnSuccess) {
NSLog(@"SDP query got status %d", status);
return;
}
NSLog(@"sdpCompleted");
}
//get the RfComm channel.
-(IOReturn) findRfCommChannel:(BluetoothRFCOMMChannelID *) rfChan{
if(btDevice == nil)
return kIOReturnNotFound;
IOReturn ret;
NSArray* services = [btDevice getServices];
BluetoothRFCOMMChannelID newChan;
for (IOBluetoothSDPServiceRecord* service in services) {
NSLog(@"Service: %@", [service getServiceName]);
ret = [service getRFCOMMChannelID:&newChan];
if (ret == kIOReturnSuccess) {
*rfChan = newChan;
NSLog(@"ChannelID FOUND %d %d", newChan, *rfChan);
return kIOReturnSuccess;
}
}
return kIOReturnNotFound;
}
//Open the connection.
-(BOOL) openConnection:(BluetoothRFCOMMChannelID *) chanId{
IOBluetoothRFCOMMChannel *channel;
if ([btDevice openRFCOMMChannelAsync:&channel withChannelID:*chanId delegate:self] != kIOReturnSuccess) { // after connection it is established.. the delegates methoed are triggered.
NSLog(@"Couldn't open channel");
return NO;
}
[channel closeChannel];
return YES;
}
//delegate RFComm channel
- (void)rfcommChannelOpenComplete:(IOBluetoothRFCOMMChannel*)rfcommChannel
status:(IOReturn)error {
if (error != kIOReturnSuccess) {
NSLog(@"Failed to open channel, error %d", error);
return;
}
NSLog(@"channel complete");
}
//connection closed.
- (void)rfcommChannelClosed:(IOBluetoothRFCOMMChannel*)rfcommChannel {
NSLog(@"Channel closed");
}
//reading data from rfcomm
- (void)rfcommChannelData:(IOBluetoothRFCOMMChannel*)rfcommChannel data:(void *)dataPointer length:(size_t)dataLength {
NSString *newStr = [[NSString alloc] initWithBytes:dataPointer length:dataLength encoding:NSUTF8StringEncoding];
NSLog(@"received: %@",newStr);
}
//writing data from rfcomm
- (void)rfcommChannelWriteComplete:(IOBluetoothRFCOMMChannel*)rfcommChannel refcon:(void*)refcon status:(IOReturn)error { /* not used yet */}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment