簡體   English   中英

C++/WIN32 (XP) 藍牙設備名稱更改

[英]C++/WIN32 (XP) Bluetooth device name change

有誰知道如何以編程方式更改藍牙設備名稱? 或者設備名稱存儲在哪里?

解決

這是封裝在 class 方法中的現成解決方案。

bool CBluetooth::ChangeLocalRadioName(LPTSTR newName)
{
    LPTSTR instanceID = GetGenericBluetoothAdapterInstanceID();

    if (instanceID == NULL)
    {       
        //_tprintf(_TEXT("Failed to get Generic Bluetooth Adapter InstanceID\n"));
        return false;
    }
    LPTSTR instanceIDModified = new TCHAR[_tcslen(instanceID)];
    _tcscpy(instanceIDModified, instanceID); 
    find_and_replace(instanceIDModified, _TEXT("\\"), _TEXT("#"));


    HANDLE hDevice;
    TCHAR fileName[1024] = { 0 };   
    _tcscpy(fileName, _TEXT("\\\\.\\"));    
    _tcscat(fileName, instanceIDModified);
    _tcscat(fileName, _TEXT("#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"));

    hDevice = CreateFile(fileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

    if (hDevice == INVALID_HANDLE_VALUE)
    {
        //_tprintf(_TEXT("Failed to open device. Error code: %d\n"), GetLastError());       
        return false;
    }

    //Change radio module local name in registry
    LPTSTR RMLocalName = newName;
    CHAR bufRMLocalName[256];

    HKEY hKey;
    TCHAR rmLocalNameKey[1024] = { 0 };
    LSTATUS ret;
    _tcscpy(rmLocalNameKey, _TEXT("SYSTEM\\ControlSet001\\Enum\\"));    
    _tcscat(rmLocalNameKey, instanceID);
    _tcscat(rmLocalNameKey, _TEXT("\\Device Parameters"));

    ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, rmLocalNameKey, 
        0L, KEY_SET_VALUE , &hKey);

    if(ret != ERROR_SUCCESS)
    {
        //_tprintf(TEXT("Failed to open registry key. Error code: %d\n"), ret);
        return false;
    }

    ret = RegSetValueEx(hKey, _TEXT("Local Name"), 0, REG_BINARY, 
        (LPBYTE)RMLocalName,strlen(RMLocalName));

    if (ret != ERROR_SUCCESS)
    {
        //_tprintf(TEXT("Failed to set registry key. Error code: %d\n"), ret);
        return false;
    }

    RegCloseKey(hKey);

    // This check decides what IO control code to use based on if we're in XP or Vista (and later). 
    OSVERSIONINFO osver;
    osver.dwOSVersionInfoSize = sizeof(osver);
    GetVersionEx(&osver);

    UINT ctlCode = (UINT)(6 > osver.dwMajorVersion ? 0x220fd4 : 0x411008);
    long reload = 4;  // tells the control function to reset or reload or similar...
    DWORD bytes = 0; // merely a placeholder

    // Send radio module driver command to update device information
    if (!DeviceIoControl(hDevice, ctlCode, &reload, 4, NULL, 0, &bytes, NULL))
    {
        //_tprintf(TEXT("Failed to update radio module local name. Error code: %d\n"), GetLastError());
        return false;
    }



    return true;

}

感謝:叢生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM