Bluetooth Communication Issue with Barcode Scanner
Bluetooth Communication Issue with Barcode Scanner
- Subject: Bluetooth Communication Issue with Barcode Scanner
- From: Nilesh Khemkar <email@hidden>
- Date: Thu, 27 Apr 2006 19:40:48 +0530
Hi,
I am attempting to communicate with bluetooth enabled scanner device,
for which i did try the following code to start communication with
device using bluetooth adapter. I did pairing successfully and receive
acknowledgement when i run the below program from device but could not
able to open the device file to write or read. Also could not able to
locate the device object/device file under ../dev directory as usually
we get after connecting to any USB device.
I tried to debug and found IOBluetoothDeviceOpenConnection API gets
failed. What could be the reason ? I receive error no. which is not
informative as such.
OR What should be the right approach to start communication with the
scanner device ? Just have a look at the snippet of code below. Any
pointers or clues ?
//==============================================================//
// Serial headers
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <CoreFoundation/CoreFoundation.h>
// Bluetooth
#include <IOBluetooth/IOBluetoothUserLib.h>
#include <IOBluetoothUI/IOBluetoothUIUserLib.h>
#include <CoreServices/CoreServices.h>
IOBluetoothRFCOMMChannelRef mRFCOMMChannelRef;
// Hold the original termios attributes so we can reset them
void rfcommEventListener (IOBluetoothRFCOMMChannelRef rfcommChannel,
void *refCon, IOBluetoothRFCOMMChannelEvent *event)
{}
void TerminateBluetooth()
{
if ( mRFCOMMChannelRef != NULL )
{
IOBluetoothRFCOMMChannelCloseChannel( mRFCOMMChannelRef
);
IOBluetoothDeviceCloseConnection(
IOBluetoothRFCOMMChannelGetDevice(
mRFCOMMChannelRef ) );
IOBluetoothObjectRelease( mRFCOMMChannelRef );
mRFCOMMChannelRef = NULL;
}
}
bool StartBlueTooth()
{
bool returnValue = FALSE;
IOBluetoothSDPServiceRecordRef serviceRecord;
IOBluetoothServiceBrowserControllerRef serviceBrowser;
IOReturn result;
// to select a device and service.
serviceBrowser =
IOBluetoothServiceBrowserControllerCreate(kIOBluetoothServiceBrowserCont
rollerOptionsNone
);
result = IOBluetoothServiceBrowserControllerDiscover(serviceBrowser,
&serviceRecord);
if((result == kIOReturnSuccess) && (serviceRecord != NULL) )
{
UInt8 rfcommChannelID;
result = IOBluetoothSDPServiceRecordGetRFCOMMChannelID(serviceRecord,
&rfcommChannelID);
if ( result == kIOReturnSuccess )
{
IOBluetoothDeviceRef device;
//NSLog( @"Service selected '%@' - RFCOMM
Channel ID = %d\n",
(NSString*)IOBluetoothSDPServiceRecordGetServiceName( serviceRecord ),
rfcommChannelID );
// Get the device from the service record
device = IOBluetoothSDPServiceRecordGetDevice(
serviceRecord );
if ( ( device != NULL ) && (
IOBluetoothDeviceOpenConnection(device,
NULL, NULL) == kIOReturnSuccess ) )
{
// Open channel and registers our
callback for RFCOMM Events:
result = IOBluetoothDeviceOpenRFCOMMChannelAsync(device,
&mRFCOMMChannelRef, rfcommChannelID, rfcommEventListener, NULL);
printf("result = %d\n", result);
if ( result == kIOReturnSuccess )
returnValue = TRUE;
}
}
}
return returnValue;
}
bool sendData(void* buffer, UInt32 length)
{
if ( mRFCOMMChannelRef != nil )
{
UInt32 numBytesRemaining;
IOReturn result;
BluetoothRFCOMMMTU rfcommChannelMTU;
result = kIOReturnSuccess;
numBytesRemaining = length;
rfcommChannelMTU = IOBluetoothRFCOMMChannelGetMTU(
mRFCOMMChannelRef
);
while ( ( result == kIOReturnSuccess ) && (
numBytesRemaining > 0 ) )
{
UInt32 numBytesToSend = ( ( numBytesRemaining > rfcommChannelMTU ) ?
rfcommChannelMTU : numBytesRemaining );
result = IOBluetoothRFCOMMChannelWriteSync( mRFCOMMChannelRef, buffer,
numBytesToSend );
// Updates the position in the buffer:
numBytesRemaining -= numBytesToSend;
((char *) buffer) += numBytesToSend;
}
// We are successful only if all the data was sent:
if ( ( numBytesRemaining == 0 ) && ( result ==
kIOReturnSuccess ) )
{
return TRUE;
}
}
return FALSE;
}
int main(int argc, char *argv[])
{
#pragma unused(argc, argv)
printf("Starting the application\n");
bool ret = StartBlueTooth();
if (ret != TRUE)
{
DLOG("Failed to start");
goto kill1;
}
char l_pTrigger[200]; int l_iLength;
sprintf(l_pTrigger, "\x16T\x0D"); l_iLength = 3;
CFRunLoopRun();
sendData((void *) l_pTrigger, l_iLength);
kill1:
TerminateBluetooth();
return 0;
}
//==============================================================//
Thanks in advance.
http://www.patni.com
World-Wide Partnerships. World-Class Solutions.
_____________________________________________________________________
This e-mail message may contain proprietary, confidential or legally
privileged information for the sole use of the person or entity to
whom this message was originally addressed. Any review, e-transmission
dissemination or other use of or taking of any action in reliance upon
this information by persons or entities other than the intended
recipient is prohibited. If you have received this e-mail in error
kindly delete this e-mail from your records. If it appears that this
mail has been forwarded to you without proper authority, please notify
us immediately at email@hidden and delete this mail.
_____________________________________________________________________
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Bluetooth-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden