Thread: Bluetooth SDK

  1. #1
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542

    Bluetooth SDK

    I started some wireless development.. Bluetooth and IrDA.. I kinda figured out some IrDA (but still searching for some good stuff).. but couldnt manage to do anything with Bluetooth.. I got the bluetooth device and the drivers disk and everything.. but I haven't got any SDK.. so I was wondering if there's any free or trial version of a good bluetooth sdk available for download.. I don't care if I have to pay for it.. I want a trial.. and if it suits me then I'll think about purchasing it. Thanks.. (oh and if anyone knows some more about IrDA development, please post as well )
    what does signature stand for?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As mentioned in the other thread, on modern Windows, you can use winsock for either IrDA or Bluetooth. What platform are you using? What is the specific troulble you are having with bluetooth?

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    The problem is sending some dumb stupid file from the computer on a mobile phone.. I'm using windows xp pro sp1 (already got bluetooth drivers)
    what does signature stand for?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This page suggests you should be able to transfer a file without writing software:
    Bluetooth File Transfer with Windows XP
    Were you able to connect using this method?

  5. #5
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Yes.. i were able to connect and send files using that method.. but you see, if I use that method in my software (i mean calling the transfer program with some parameters) I won't be able to recieve back some data.. like about if the file was actually sent, if the device is low on memory, if something else went wrong.. same was with irftp.exe
    what does signature stand for?

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The trouble is that while Windows provides an API for bluetooth communication (the bluetooth equivalent of TCP/IP) it doesn't appear to offer APIs for the higher level protocols (the bluetooth equivalents of HTTP, FTP, etc) needed to transfer files, etc. However, your phone most likely uses the OBEX protocol (a protocol is called a service in bluetooth parlance) to transfer files and there is an open source implementation called Open Obex that may be able to be ported to Windows.

    You may be better off parsing the result of the transfer program than trying to code your own.

    Anyway, the first step is to enumerate devices. This code, from the platform SDK bluetooth sample, finds all the devices in range. You will need MSVC and a recent platform SDK to compile it. Compile it (it's C++) and if everything is working it should list your device.
    Code:
    #include <stdio.h>
    #include <winsock2.h>
    #include <ws2bth.h>
    #pragma comment(lib, "ws2_32.lib")
    
    
    #define CXN_MAX_INQUIRY_RETRY             3 
    #define CXN_DELAY_NEXT_INQUIRY            15 
    
    //
    // This function demonstrates device inquiry, with optional LUP flags.
    //
    ULONG ListBluetoothDevices(IN const char * pszRemoteName, OUT BTH_ADDR * pRemoteBtAddr)
    {
        INT          iResult = 0, iRetryCount = 0;
        BOOL         bContinueLookup = FALSE, bRemoteDeviceFound = FALSE;
        ULONG        ulFlags = 0, ulPQSSize = sizeof(WSAQUERYSET);
        HANDLE       hLookup = 0;
        PWSAQUERYSET pWSAQuerySet = NULL;
    
        if ( ( NULL == pszRemoteName ) || ( NULL == pRemoteBtAddr ) )
        {
            goto CleanupAndExit;
        }
    
        if ( NULL == ( pWSAQuerySet = (PWSAQUERYSET) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulPQSSize) ) )
        {
            printf("!ERROR! | Unable to allocate memory for WSAQUERYSET\n");
            goto CleanupAndExit;
        }
    
        //
        // Search for the device with the correct name
        //
        for (iRetryCount = 0; !bRemoteDeviceFound && (iRetryCount < CXN_MAX_INQUIRY_RETRY); iRetryCount++)
        {
            //
            // WSALookupService is used for both service search and device inquiry
            // LUP_CONTAINERS is the flag which signals that we're doing a device inquiry.  
            //    
            ulFlags = LUP_CONTAINERS;
    
            //
            // Friendly device name (if available) will be returned in lpszServiceInstanceName
            //
            ulFlags |= LUP_RETURN_NAME;
    
            //
            // BTH_ADDR will be returned in lpcsaBuffer member of WSAQUERYSET
            //
            ulFlags |= LUP_RETURN_ADDR;
    
            if ( 0 == iRetryCount )
            {
                printf("*INFO* | Inquiring device from cache...\n");
            }
            else
            {
                //
                // Flush the device cache for all inquiries, except for the first inquiry
                //
                // By setting LUP_FLUSHCACHE flag, we're asking the lookup service to do 
                // a fresh lookup instead of pulling the information from device cache.
                //
                ulFlags |= LUP_FLUSHCACHE;
    
                //
                // Pause for some time before all the inquiries after the first inquiry
                //
                //
                // BUGBUG why sleep. Try to get rid of this
                //
                // Remote Name requests will arrive after device inquiry has
                // completed.  Without a window to receive IN_RANGE notifications,
                // we don't have a direct mechanism to determine when remote
                // name requests have completed.
                //
                printf("*INFO* | Unable to find device.  Waiting for %d seconds before re-inquiry...\n", CXN_DELAY_NEXT_INQUIRY);
                Sleep(CXN_DELAY_NEXT_INQUIRY * 1000);
    
                printf("*INFO* | Inquiring device ...\n");
            }
    
            //
            // Start the lookup service
            //
            iResult = 0;
            hLookup = 0;
            bContinueLookup = FALSE;
            ZeroMemory(pWSAQuerySet, ulPQSSize);
            pWSAQuerySet->dwNameSpace = NS_BTH;
            pWSAQuerySet->dwSize = sizeof(WSAQUERYSET);
            iResult = WSALookupServiceBegin(pWSAQuerySet, ulFlags, &hLookup);
    
            if ( (NO_ERROR == iResult) && (NULL != hLookup) )
            {
                bContinueLookup = TRUE;
            }
            else if ( 0 < iRetryCount )
            {
                printf("=CRITICAL= | WSALookupServiceBegin() failed with error code %d, WSALastError = %d\n", iResult, WSAGetLastError());
                goto CleanupAndExit;
            }
    
            while ( bContinueLookup )
            {
                //
                // Get information about next bluetooth device
                // 
                // Note you may pass the same WSAQUERYSET from LookupBegin
                // as long as you don't need to modify any of the pointer
                // members of the structure, etc.
                //
                // ZeroMemory(pWSAQuerySet, ulPQSSize);
                // pWSAQuerySet->dwNameSpace = NS_BTH;
                // pWSAQuerySet->dwSize = sizeof(WSAQUERYSET);
                if ( NO_ERROR == WSALookupServiceNext(hLookup, ulFlags, &ulPQSSize, pWSAQuerySet) )
                {
                    printf("Found Device: %s\n", pWSAQuerySet->lpszServiceInstanceName);
    
                    //
                    // Since we're a non-unicode application, the remote
                    // name in lpszServiceInstanceName will have been converted
                    // from CP_UTF8 to CP_ACP, this may cause the name match
                    // to fail unexpectedly.  If the app is to handle this,
                    // the app needs to be unicode.
                    //
                    if ( ( pWSAQuerySet->lpszServiceInstanceName != NULL ) && ( 0 == _stricmp(pWSAQuerySet->lpszServiceInstanceName, pszRemoteName) ) )
                    {
                        //
                        // Found a remote bluetooth device with matching name.
                        // Get the address of the device and exit the lookup.
                        //
                        CopyMemory(pRemoteBtAddr, 
                                   &((PSOCKADDR_BTH) pWSAQuerySet->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr, 
                                   sizeof(*pRemoteBtAddr));
                        bRemoteDeviceFound = TRUE;
                        bContinueLookup = FALSE;
                    }
                }
                else
                {
                    if ( WSA_E_NO_MORE == ( iResult = WSAGetLastError() ) ) //No more data
                    {
                        //
                        // No more devices found.  Exit the lookup.
                        //
                        bContinueLookup = FALSE;
                    }
                    else if ( WSAEFAULT == iResult )
                    {
                        //
                        // The buffer for QUERYSET was insufficient.  
                        // In such case 3rd parameter "ulPQSSize" of function "WSALookupServiceNext()" receives 
                        // the required size.  So we can use this parameter to reallocate memory for QUERYSET.
                        //
                        HeapFree(GetProcessHeap(), 0, pWSAQuerySet);
                        pWSAQuerySet = NULL;
                        if ( NULL == ( pWSAQuerySet = (PWSAQUERYSET) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulPQSSize) ) )
                        {
                            printf("!ERROR! | Unable to allocate memory for WSAQERYSET\n");
                            bContinueLookup = FALSE;
                        }
                    }
                    else
                    {
                        printf("=CRITICAL= | WSALookupServiceNext() failed with error code %d\n", iResult);
                        bContinueLookup = FALSE;
                    }
                }
            }
    
            //
            // End the lookup service
            //
            WSALookupServiceEnd(hLookup);
        }
    
    CleanupAndExit:
        if ( NULL != pWSAQuerySet )
        {
            HeapFree(GetProcessHeap(), 0, pWSAQuerySet);
            pWSAQuerySet = NULL;
        }
    
        if ( bRemoteDeviceFound )
        {
            return(0);
        }
        else
        {
            return(1);
        }
    }
    
    
    
    
    int main(void)
    {
    	WSADATA    WSAData = {0};
    	WSAStartup(MAKEWORD(2, 2), &WSAData);
    
    
    	BTH_ADDR btAddr;
    	ListBluetoothDevices("Test", &btAddr);
    
    	getchar();
    	return 0;
    }

  7. #7
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Nice.. I didn't try that yet but I will.. and where did you get that code? Did you write it yourself?
    what does signature stand for?

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> and where did you get that code? Did you write it yourself? <<

    It is slightly edited code from a platform SDK sample. If you have the SDK installed, you should find the bluetooth sample somewhere like C:\Program Files\Microsoft SDK\Samples\netds\Bluetooth.

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    I don't have the SDK installed, where do I find that?
    what does signature stand for?

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Platform SDK. The download is a 150MB+.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bluetooth Questions
    By Tonto in forum Tech Board
    Replies: 2
    Last Post: 05-30-2009, 02:41 AM
  2. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 07:16 PM
  3. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Directx SDK Documentation
    By Zoalord in forum Game Programming
    Replies: 4
    Last Post: 05-08-2003, 06:07 AM