Geeks With Blogs
Kate Moss' Open Space

Just like the WiFi network, every device has a unique identification, BD address, and a more readable device name for human, BT local name.

As the BT name is for identification/management purpose on BT network, so it is reasonable to give a more unique name rather than default one.

Since the BT stack comes with CE6 uses Computer Name as the BT name, the most straight forward way is to change the device name. Here is some approach to do that.

  1. Change the "HKEY_LOCAL_MACHINE\Ident\Name" in either platform.reg or project.reg and rebuild the OS image.
  2. In run-time, you could modify the registry or use sethostname to change the device name and reboot the device to take effect.
  3. If reboot the device is not an option to you or the registry is not persistent on the device, restart the BT stack is an alternative approach and no need to reboot the device; shown as the following sample code 
    char pszName = "TheDevName";
    if(sethostname(pszName, strlen(pszName)+1)==0)
    {
            HANDLE hDev = CreateFile(L"BTD0:", GENERIC_READ | GENERIC_WRITE,
                                     FILE_SHARE_READ | FILE_SHARE_WRITE,
                                     NULL, OPEN_EXISTING, 0, NULL);
            if(hDev != INVALID_HANDLE_VALUE)
            {
                    int iErr;
                    iErr = DeviceIoControl(hDev, IOCTL_SERVICE_REFRESH,  NULL, 0,
                                          NULL, 0, NULL, NULL);
                    CloseHandle(hDev); 
            }
    }
  4. But what if you want to keep the original device name but still like to use different BT name? That will be more difficult but we will show you the solution.

Let's dig into the BT stack first to see how the BT name associated with device name.

The BT stack can be found in CE6 shared source in private\winceos\comm\bluetooth\ and by looking into the StackUp function in private\winceos\comm\bluetooth\sys\btd.cxx, one can find the following code

    if (gpState->fIsRunning && gpState->fConnected && GetComputerName (szComputerName, &dwSize) &&
        (WideCharToMultiByte (CP_UTF8, 0, szComputerName, -1, (char *)aComputerName, sizeof(aComputerName), NULL, NULL) ||
        WideCharToMultiByte (CP_ACP, 0, szComputerName, -1, (char *)aComputerName, sizeof(aComputerName), NULL, NULL))) {
        HCI_ChangeLocalName_In pNameCallback = gpState->hci_if.hci_ChangeLocalName_In;
        // Prepare the call...
        __try {
            iRes = pNameCallback (hHCI, pCall, aComputerName);
        } __except (1) {
        }
	// Error handling...
    }

The BT Stack uses hci_ChangeLocalName_In (an internal BT HCI level API and don't worry we will trace it later.) to change the name, and it is obvious that the name is obtained from GetComputerName, so now we know how the BT name is derived from device name.

So what are we going to do? Hack the BT stack? That might work but whenever Microsoft release an update for the BT stack, you will need to diff the update and merge our hack again and again. It is feasible but is not recommended, especially in the perspective of code maintenance.

As modifying the stack is not an option, it looks like a dead end. But wait, if BT Stack needs to call hci_ChangeLocalName_In to Change the BT name, what is the default BT name? Well, that depends on your BT module. Some of the BT modules have a boot ROM which contains many configuration data, such as BD address and yes the default value of the BT local name. So if you are luckily having a boot ROM on your BT module and you can reprogram it, that will be a solution.

But what if there is no boot ROM? In this case, your transportation driver probably loads the configuration data at HCI driver initialization. By altering this configuration data before it loads to BT module may do the work as well. Note that no matter reprogram the boot ROM or altering the conflagration data, you will need to consult with the BT chip data-sheet or the manufacture.

No matter which solution are you going to take, it only change the default BT name but soon the name will be overwritten by BT stack in the StackUp function. Since the BT stack use hci_ChangeLocalName_In to overwrite the name, so let's trace into it to see what we can do.

The implementation of hci_ChangeLocalName_In is in private\winceos\comm\bluetooth\hci\base\hci.cxx, the bt_change_local_name_in function.

static int bt_change_local_name_in
(
HANDLE hDeviceContext,
void *pCallContext,
unsigned char utf_name[248]
) {
...
    if ((gpHCI->eStage == Connected) && (gpHCI->transport.uiFlags & HCI_FLAGS_NOLOCALNAME)) {
        IFDBG(DebugOut (DEBUG_ERROR, L"bt_change_local_name_in returns ERROR_NOT_SUPPORTED\n"));
        gpHCI->Unlock ();
        return ERROR_NOT_SUPPORTED;
    }
...
}

Well, the HCI_FLAGS_NOLOCALNAME looks like what we need, suppress the BT stack over write the BT name we just programmed. But how to set this flag? The HCI_PARAMETERS in MSDN tells us it is passed by the HCI transportation driver via HCI_ReadHciParameters function and by digging into one of the HCI transportation driver, says the UART (PUBLIC\COMMON\OAK\DRIVERS\BLUETOOTH\TRANSPORTS\UART\hciuart.cxx), the flags setting is from [HKLM\Software\Microsoft\Bluetooth\hci\flags] registry setting.

Here is the summarize for the method 4:

  1. Set the default BT local name either by reprogramming the boot ROM or by configuration data
  2. Have the HKLM\Software\Microsoft\Bluetooth\hci\flags with 0x02 flags (HCI_FLAGS_NOLOCALNAME)

Finally, if both approach is not for you, you could still use HCI command to change the BT local name. The default HCI transportation driver may not be easy to insert HCI command in initialization, so you would like to create a Bluetooth HCI Stack Extension Layer.

To do so you could use public\common\oak\drivers\bluetooth\SAMPLE\bthlink\bthlink.cxx as a template
and use the hci_ChangeLocalName_In to change the BT device name.
You can refer to btd.cxx for the usage of hci_ChangeLocalName_In, and the
prototype is in public\common\oak\inc\bt_ddi.h

For more information about BthLink
http://msdn.microsoft.com/en-us/library/bb665971.aspx,
http://msdn.microsoft.com/en-us/library/aa916292.aspx

 

Posted on Thursday, October 1, 2009 6:05 PM | Back to top


Comments on this post: Change the Bluetooth Name in CE6

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Kate Moss' Open Space | Powered by: GeeksWithBlogs.net