Skip to content

Instantly share code, notes, and snippets.

@pqvst
Created May 18, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pqvst/78897d6f304aa9411bcb to your computer and use it in GitHub Desktop.
Save pqvst/78897d6f304aa9411bcb to your computer and use it in GitHub Desktop.
Bluetooth (OSX)
-(BOOL) start
{
NSString* dictionaryPath = mPath;
NSMutableDictionary* sdpEntries = [NSMutableDictionary dictionaryWithContentsOfFile:dictionaryPath];
if (sdpEntries != nil)
{
[sdpEntries setObject:@"My Service" forKey:@"0100 - ServiceName*"];
IOBluetoothSDPServiceRecord* serviceRecord = [IOBluetoothSDPServiceRecord publishedServiceRecordWithDictionary:sdpEntries];
[serviceRecord getRFCOMMChannelID:&mServerChannelID];
[serviceRecord getServiceRecordHandle:&mServerHandle];
mIncomingChannelNotification = [IOBluetoothRFCOMMChannel
registerForChannelOpenNotifications:self
selector:@selector(newRFCOMMChannelOpened:channel:)
withChannelID:mServerChannelID
direction:kIOBluetoothUserNotificationChannelDirectionIncoming];
return TRUE;
}
return FALSE;
}
-(void) stop
{
if (mServerHandle != 0)
{
IOBluetoothRemoveServiceWithRecordHandle(mServerHandle);
}
if (mIncomingChannelNotification != nil)
{
[mIncomingChannelNotification unregister];
mIncomingChannelNotification = nil;
}
mServerChannelID = 0;
}
-(void) newRFCOMMChannelOpened:(IOBluetoothUserNotification*)inNotification channel:(IOBluetoothRFCOMMChannel*)newChannel
{
if (newChannel != nil && [newChannel isIncoming] && [newChannel getChannelID] == mServerChannelID)
{
mChannel = newChannel;
[mChannel retain];
if ( [mChannel setDelegate:self] == kIOReturnSuccess )
{
if (mListener != nil)
{
mListener->onConnected();
}
}
else
{
[mChannel release];
mChannel = nil;
}
}
}
- (void)rfcommChannelClosed:(IOBluetoothRFCOMMChannel*)rfcommChannel;
{
mListener->onDisconnected();
}
- (void)rfcommChannelData:(IOBluetoothRFCOMMChannel*)rfcommChannel data:(void *)dataPointer length:(size_t)dataLength;
{
mListener->onData(dataPointer, dataLength);
}
-(void) disconnect
{
if (mChannel != nil)
{
// And closes the channel:
[mChannel closeChannel];
// We do not need the channel anymore:
[mChannel release];
mChannel = nil;
}
}
-(void) send:(char*)dataPointer length:(size_t)dataLength
{
if (mChannel != nil)
{
UInt32 numBytesRemaining;
IOReturn result;
BluetoothRFCOMMMTU rfcommChannelMTU;
numBytesRemaining = dataLength;
result = kIOReturnSuccess;
// Get the RFCOMM Channel's MTU. Each write can only contain up to the MTU size
// number of bytes.
rfcommChannelMTU = [mChannel getMTU];
// Loop through the data until we have no more to send.
while ( ( result == kIOReturnSuccess ) && ( numBytesRemaining > 0 ) )
{
// finds how many bytes I can send:
UInt32 numBytesToSend = ( ( numBytesRemaining > rfcommChannelMTU ) ? rfcommChannelMTU : numBytesRemaining );
// This method won't return until the buffer has been passed to the Bluetooth hardware to be sent to the remote device.
// Alternatively, the asynchronous version of this method could be used which would queue up the buffer and return immediately.
result = [mChannel writeSync:dataPointer length:numBytesToSend];
// Updates the position in the buffer:
numBytesRemaining -= numBytesToSend;
dataPointer += numBytesToSend;
}
// We are successful only if all the data was sent:
if ( ( numBytesRemaining == 0 ) && ( result == kIOReturnSuccess ) )
{
//return TRUE;
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment