Establishing a Connection to a Bluetooth Network

Windows CE 5.0 Not SupportedWindows Embedded NavReady 2009 Supported

10/16/2008

Applies to Windows Embedded NavReady

This topic describes how to establish a connection to a Bluetooth network by using Connection Manager.

To complete this task, you must have completed the previous steps in How to Make a Data Connection by Using Connection Manager.

Establishing a connection to a Bluetooth network

  1. In the source code for your application, use a constant to represent the destination network that you want to connect to. For example, for "the Internet", you can use IID_DestNetInternet, which is defined in ConnMgr.h.

  2. Create a bool value called IsConnected that is set when the application is successfully connected, and is cleared when the application is disconnected.

  3. Create a CONNMGR_CONNECTIONINFO structure. Make sure that you set the guidDestNet member to the GUID value for your connection. Also pass in a handle to the application main window that receives status notifications in the hWnd member.

  4. Create a connection request either asynchronously or synchronously.

  5. When the connection succeeds, set IsConnected to true.

    All application code that requires connectivity must execute on the condition that IsConnected is true.

Example

The following code sample asynchronously requests a connection from Connection Manager. When the application is connected, it executes code that connects to the Internet.

Note

To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.

#include <windows.h>
#include "dubinet.h"

#define INITGUID

#include <connmgr.h>

#define INTERNET_BUFFER_SIZE  10*1024
BOOL IsConnected;

void DoInternetReadFile(HINTERNET hFile)
{
    DWORD dwRead = 0;
    char  pBuf[INTERNET_BUFFER_SIZE + 1];
    TCHAR szBuf[INTERNET_BUFFER_SIZE + 1];

    while(InternetReadFile(hFile, pBuf, sizeof(pBuf), &dwRead))
    {
        if(!dwRead)
        {
            break;
        }

        pBuf[dwRead]                = 0;
        pBuf[INTERNET_BUFFER_SIZE]  = 0;
        szBuf[INTERNET_BUFFER_SIZE] = 0;

        MultiByteToWideChar(
            CP_ACP, 
            0,
            pBuf, 
            -1, 
            szBuf, 
            INTERNET_BUFFER_SIZE 
            );
        RETAILMSG(1, ( TEXT("InternetReadFile() read %d bytes => %s \r\n"), dwRead, szBuf));
    }
}

void DoInternetTransaction()
{
    HINTERNET hInet = InternetOpen(
        L"bttest", 
        INTERNET_OPEN_TYPE_DIRECT, 
        0,
        0, 
        0 
        );
    
    HINTERNET hConnect = InternetConnect( 
        hInet, 
        L"m.live.com", 
        INTERNET_DEFAULT_HTTP_PORT, 
        NULL, 
        NULL, 
        INTERNET_SERVICE_HTTP, 
        0, 
        0 
        );

    if(!hConnect)
    {
        return;
    }
    
    HINTERNET hRequest = HttpOpenRequest( 
        hConnect, 
        L"GET", 
        L"", 
        NULL, 
        NULL, 
        NULL, 
        INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 
        0 
        );
        
    if(!hRequest)
    {
        return;
    }
    
    BOOL bRequestOkay = HttpSendRequest( 
        hRequest, 
        NULL, 
        0, 
        NULL, 
        0 
        );
    if(bRequestOkay)
    {
        DoInternetReadFile( hRequest );
    }   

    InternetCloseHandle (hRequest);
    InternetCloseHandle (hConnect);
    InternetCloseHandle (hInet);
}

BOOL EstablishConnection()
{
    CONNMGR_CONNECTIONINFO  ci = {0};
    HANDLE ConnMgrConnection;
    
    ci.cbSize     = sizeof(ci);
    ci.dwParams   = CONNMGR_PARAM_GUIDDESTNET;
    ci.dwFlags    = CONNMGR_FLAG_SUSPEND_AWARE;
    ci.dwPriority = CONNMGR_PRIORITY_USERBACKGROUND;
    ci.bExclusive = FALSE;

    ci.guidDestNet = IID_DestNetInternet;
    
    DWORD ConnectionStatus;
    HRESULT hr = ConnMgrEstablishConnectionSync(
        &ci,
        &ConnMgrConnection,
        500000,            
        &ConnectionStatus
        );

    if(FAILED(hr) || !ConnMgrConnection)
    {
        return FALSE;
    }        
    IsConnected = true;
    return TRUE;
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
    EstablishConnection();
     if (IsConnected)
     {
           DoInternetTransaction();
     }
}

This code sample connects to the Internet by using the Bluetooth Dial-up Networking (DUN) profile.

See Also

Other Resources

Connection Manager Application Development for Portable Navigation Devices