Problem with rfcommDataListener.
site_archiver@lists.apple.com Delivered-To: bluetooth-dev@lists.apple.com Hi, Please review the code and provide your inputs. I want to do something like this, pseudo code: #include <CoreFoundation/CoreFoundation.h> // #include <IOBluetoothRFCOMMWorkaround.m> #include <IOBluetooth/objc/IOBluetoothDevice.h> #include <IOBluetooth/objc/IOBluetoothRFCOMMChannel.h> // Bluetooth #include <IOBluetooth/IOBluetoothUserLib.h> #include <IOBluetoothUI/IOBluetoothUIUserLib.h> #include <CoreServices/CoreServices.h> #import <Cocoa/Cocoa.h> IOBluetoothRFCOMMChannelRef mRFCOMMChannelRef; char userName[100]; bool StartBlueTooth() { id pool = [[NSAutoreleasePool alloc] init]; bool returnValue = false; IOBluetoothSDPServiceRecordRef serviceRecord; //CFArrayRef serviceArray; //IOBluetoothSDPUUIDRef chatUUID; strcpy(userName, "TestBluetooth RFCOMM"); IOBluetoothServiceBrowserControllerRef serviceBrowser; IOReturn result; if ( (result == kIOReturnSuccess) && (serviceRecord != NULL) ) { UInt8 rfcommChannelID; NSLog(@"Service record info1: %@", serviceRecord); bool sendData(void* buffer, UInt32 length) { if ( mRFCOMMChannelRef != nil ) { printf("Inside sendData\n"); UInt32 numBytesRemaining; IOReturn result; BluetoothRFCOMMMTU rfcommChannelMTU; result = kIOReturnSuccess; numBytesRemaining = length; return false; } kill1: TerminateBluetooth(); return 0; } http://www.patni.com World-Wide Partnerships. World-Class Solutions. _____________________________________________________________________ _______________________________________________ Do not post admin requests to the list. They will be ignored. Bluetooth-dev mailing list (Bluetooth-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/bluetooth-dev/site_archiver%40lists.a... This email sent to site_archiver@lists.apple.com We're building a small application to read & write with Barcode Scanner which are Bluetooth enabled, and I'm writing a Carbon application in C++ using CW 9.3 to get and display data from the device. I've successfully established a connection but not sure that i am sending & receiving data back from device . We use LinkSys bluetooth adapter to communication with barcode device. One More query i want to ask that, in USB connection we get device file object (something like "cu.usb...") under ../dev sys directory when we connect the device with Macintosh 10.3.5, through with we do low level file i/o and successfully send & receive data from device. So I was wandering that Cann't we get device file object in case of bluetooth connectivity, if yes then in which directory it makes the device file object. Please suggest. // ************************************************************************ ************************************/ // Serial headers #include <stdio.h> #include <string.h> #include <termios.h> // Hold the original termios attributes so we can reset them void rfcommEventListener (IOBluetoothRFCOMMChannelRef rfcommChannel, void *refCon, IOBluetoothRFCOMMChannelEvent *event) { switch (event->eventType) { case kIOBluetoothRFCOMMNewDataEvent: // In thise case: // event->u.newData.dataPtr is a pointer to the block of data received. // event->u.newData.dataSize is the size of the block of data. printf("Listener1 = %s\n", event->u.newData.dataPtr); printf(" Listener called"); break; case kIOBluetoothRFCOMMFlowControlChangedEvent: // In thise case: // event->u.flowStatus is the status of flow control (see IOBluetoothRFCOMMFlowControlStatus for current restrictions) break; case kIOBluetoothRFCOMMChannelTerminatedEvent: // In this case: // event->u.terminatedChannel is the channel that was terminated. It can be converted in an IOBluetoothRFCOMMChannel // object with [IOBluetoothRFCOMMChannel withRFCOMMChannelRef:]. (see below). break; } } static void rfcommDataListener(IOBluetoothRFCOMMChannelRef rfcommChannel,void* data, UInt16 length, void* refCon) { printf(" DataListener get called"); void *pBuffer = malloc(length); memcpy(pBuffer, data, length); printf("Data received from device is %s", pBuffer); } void TerminateBluetooth() { if ( mRFCOMMChannelRef != NULL ) { // This will close the RFCOMM channel and start an inactivity timer that will close the baseband connection if no other // channels (L2CAP or RFCOMM) are open after a set period of time. IOBluetoothRFCOMMChannelCloseChannel( mRFCOMMChannelRef ); // This close connection call signals to the system that we are done with the baseband connection. If no other // channels are open, it will immediately close the baseband connection. IOBluetoothDeviceCloseConnection( IOBluetoothRFCOMMChannelGetDevice( mRFCOMMChannelRef ) ); // Since we are done with the RFCOMM channel ref, we need to release it. IOBluetoothObjectRelease( mRFCOMMChannelRef ); mRFCOMMChannelRef = NULL; } } // First, we create a service browser controller which will allow us to put up a panel to allow the user // to select a device and service. //serviceBrowser = IOBluetoothServiceBrowserControllerCreate( kIOBluetoothServiceBrowserControllerOptionsNone ); // This call will return kIOReturnSuccess if the user has successfully selected a device and service that matches the // specified serviceArray. result = IOBluetoothServiceBrowserControllerDiscover(serviceBrowser, &serviceRecord); // To connect we need a device to connect and an RFCOMM channel ID to open on the device: result = IOBluetoothSDPServiceRecordGetRFCOMMChannelID(serviceRecord, &rfcommChannelID); if ( result == kIOReturnSuccess ) { IOBluetoothDeviceRef device; CFStringRef l_serviceName; char l_buffer[200]; // The service record contains all the useful information about the service the user selected // we log its name and RFCOMM Channel ID // IOBluetoothSDPServiceRecordGetServiceName( serviceRecord ), rfcommChannelID ); // Get the device from the service record device = IOBluetoothSDPServiceRecordGetDevice( serviceRecord ); // We must first open the baseband connection to the device before we can open the RFCOMM channel. The RFCOMM Channel // open API should probably do this automatically, but for now we have to do it manually. if ( device != NULL ) { NSLog(@"Service record info2: %@", serviceRecord); result = IOBluetoothDeviceOpenConnection(device, NULL, NULL); if (result == kIOReturnSuccess) { // Open channel and registers our callback for RFCOMM Events: result = IOBluetoothRFCOMMChannelRegisterIncomingDataListener(mRFCOMMChannelRef, rfcommDataListener, userName); result = IOBluetoothDeviceOpenRFCOMMChannel(device, rfcommChannelID, &mRFCOMMChannelRef) ; if ( result == kIOReturnSuccess ) returnValue = true; } else NSLog(@"Unable to open device 0x%x, err=0x%x, device info: %@", device, result, device); } } } [pool release]; return returnValue; } // Get the RFCOMM Channel's MTU. Each write can only contain up to the MTU size // number of bytes. rfcommChannelMTU = IOBluetoothRFCOMMChannelGetMTU( mRFCOMMChannelRef ); while ( ( result == kIOReturnSuccess ) && ( numBytesRemaining > 0 ) ) { // finds how many bytes I can send: UInt32 numBytesToSend = ( ( numBytesRemaining > rfcommChannelMTU ) ? rfcommChannelMTU : numBytesRemaining ); // This function 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 function could be used which would queue // up the buffer and return immediately. result = IOBluetoothRFCOMMChannelWriteSync( mRFCOMMChannelRef, buffer, numBytesToSend ); printf("After Write sendData\n"); // 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; } } int main(int argc, char *argv[]) { #pragma unused(argc, argv) printf("Starting the application\n"); bool ret = StartBlueTooth(); if(!ret ) { DLOG("Failed to start"); goto kill1; } //THIS IS THE TEST COMMAND WE WANT TO SEND TO BARCODE SCANNER WHICH IS BLUETOOTH ENABLED); char l_pTrigger[200]; int l_iLength; sprintf(l_pTrigger, "\x16T\x0D"); l_iLength = 3; //CFRunLoopRun(); // How should i put app in loop to read complete data if i send two commands. sendData((void *) l_pTrigger, l_iLength); 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 netadmin@patni.com and delete this mail. _____________________________________________________________________
participants (1)
-
Nilesh Khemkar