C++/WIN32 (XP) 蓝牙设备名称更改

发布于 2024-11-24 04:49:19 字数 40 浏览 0 评论 0原文

有谁知道如何以编程方式更改蓝牙设备名称? 或者设备名称存储在哪里?

Does anyone know how to change the Bluetooth device name programatically?
Or where is the device name stored?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-12-01 04:49:19

已解决

这是封装到类方法中的现成解决方案。

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;

}

感谢: 丛集

Resolved

Here is the ready-to-go solution encapsulated into a class method.

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;

}

Thanks to: clumpter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文