locked
Bluetooth 101 WSAStringToAddress RRS feed

  • Question

  • Hello again, once again I'm trying to do something totally new to me in windows, talk to a bluetooth device. I'm trying to start with the example in the Windows SDK
    "C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\NetDs\winsock\Bluetooth"
    When I send it the command to connect to my device (I already know the address so that's what I'm giving it, though I did try with the device name too) I get the following error
    -FATAL- | Unable to get address of the remote radio having formatted address-string 10:00:E8:AC:4D:FD
    From what I gather it isn't even trying to connect to it yet, just convert the string with the address to some internal data type using the WSAStringToAddress function. An investiation refealed it's reporting a WSAEINVAL error.

    From the example
                ulRetCode = WSAStringToAddress(g_szRemoteAddr,
                                               AF_BTH,
                                               NULL,
                                               (LPSOCKADDR)&ululRemoteBthAddr,
                                               &iAddrLen);
    
    I tried setting the sa_family member of ululRemoteBthAddr to AF_INET6 like a suggestion I found but without any improvement.

    Appreciate any ideas you have.
    Friday, May 8, 2009 7:18 PM

Answers

  • Hi Davep,

    The error message  comes from the following code:

    if ( 0 != ( ulRetCode = AddrStringToBtAddr(g_szRemoteAddr, (BTH_ADDR *) &ululRemoteBthAddr) ) )
               {
                   printf("-FATAL- | Unable to get address of the remote radio having formated address-string %s\n", g_szRemoteAddr);
                   goto CleanupAndExit;
               }
    And AddrStringToBtAddr definition shows as follows:

    ULONG AddrStringToBtAddr(IN const char * pszRemoteAddr, OUT BTH_ADDR * pRemoteBtAddr)
    {
       ULONG ulAddrData[6], ulRetCode = 0;
       BTH_ADDR BtAddrTemp = 0;
    
       if ( ( NULL == pszRemoteAddr ) || ( NULL == pRemoteBtAddr ) )
       {
           ulRetCode = 1;
           goto CleanupAndExit;
       }
    
       *pRemoteBtAddr = 0;
    
       //
       // Populate a 6 membered array of unsigned long integers 
       // by parsing the given address in string format
       //
       sscanf(pszRemoteAddr, 
              "%02x:%02x:%02x:%02x:%02x:%02x",
              &ulAddrData[0],&ulAddrData[1],&ulAddrData[2],&ulAddrData[3],&ulAddrData[4],&ulAddrData[5]);
    
       //
       // Construct a BTH_ADDR from the 6 integers stored in the array
       //
       for ( int i=0; i<6; i++ )
       {
           //
           // Extract data from the first 8 lower bits.
           //
           BtAddrTemp = (BTH_ADDR)( ulAddrData[i] & 0xFF );
    
           //
           // Push 8 bits to the left
           //
           *pRemoteBtAddr = ( (*pRemoteBtAddr) << 8 ) + BtAddrTemp;
       }
    
    CleanupAndExit:
       return ulRetCode;
    }
    I think the your issue is  one parameter in AddrStringToBtAddr(g_szRemoteAddr, (BTH_ADDR *) &ululRemoteBthAddr) ) ) is NULL. So I would like to suggest you check if one of these two parameters is NULL.

    If I misunderstood you, or you have any problems, please let me know.

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Saturday, May 16, 2009 2:05 PM
    Friday, May 15, 2009 3:19 AM