Wang Tim
Wang Tim

Reputation: 127

How to advertise bluetooth service on Windows?

I'm developing a Bluetooth(not BLE) application on Windows. I need the Windows PC to be the server, and advertise service through SDP protocol, but somehow I can hardly find a suitable API, except this one: BluetoothSetLocalServiceInfo. Documents are here: https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsetlocalserviceinfo. However, I currently still cannot successfully use it to publish a service that other devices could detect using the following code, although it returned ERROR_SUCCESS.

void BluetoothManager::RegisterServiceBtAPI(){
    BLUETOOTH_LOCAL_SERVICE_INFO_STRUCT serviceInfo;
    serviceInfo.Enabled = TRUE;
    BLUETOOTH_ADDRESS btAddress;
    QUtils::CharArray2BTH_ADDR(btServerAddress.toString().toStdString().c_str(), &btAddress.ullLong);
    serviceInfo.btAddr  = btAddress;
    QString("ServiceName").toWCharArray(serviceInfo.szName);
    QString("DeviceName").toWCharArray(serviceInfo.szDeviceString);

    QUtils::EnableSpecificPrivilege(SE_LOAD_DRIVER_NAME);

    //{00001101-0000-1000-8000-00805F9B34FB};
    const GUID serviceGUID = { 0x00001101, 0x0000, 0x0000, { 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb } };
    int result = BluetoothSetLocalServiceInfo(
        NULL, &serviceGUID, 0, &serviceInfo
    );

    if(result!=ERROR_SUCCESS){
        emit ShowDebug(QString("BluetoothSetLocalServiceInfo failed with error: %1").arg(result));
    }else{
        emit ShowDebug(QString("BluetoothSetLocalServiceInfo succeeded."));
    }
}

I'm wondering if it's correct to use this method to advertise bluetooth service on Windows PC and if so, whether or not I used it correctly. If not, what is the suitable API to publish a service on Windows?

Upvotes: 0

Views: 698

Answers (2)

JoseCutileiro
JoseCutileiro

Reputation: 1

Workaround for Bluetooth Advertisement Limitations on Windows Systems

Although the Windows API theoretically allows the use of Bluetooth functionality, many computers block the advertisement feature, including mine. To address this issue, I implemented the following solution:

I created a virtual machine (VM) using VirtualBox with minimal resource allocation, designating it as the advertising device. I obtained a USB Bluetooth adapter and configured it to be exclusively assigned to the VM (this can be done in vbox configurations). A shared folder was established between the Windows host and the VM, where the Windows system writes data. A script running on the VM continuously scans the shared file and transmits the data via Bluetooth Low Energy (BLE). This method successfully bypassed the advertisement restriction on my personal machine. I hope this approach proves useful, as I spent four days searching the internet without finding any alternative solutions that worked for my specific case.

Good luck :)

Upvotes: 0

Wang Tim
Wang Tim

Reputation: 127

I think the correct API should be WSASetService. Documents could be found here: https://learn.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-and-wsasetservice. Alternatively, IOCTL_BTH_SDP_SUBMIT_RECORD IOCTL could be used, although I don't known how yet. Documents are here: https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/bthioctl/ni-bthioctl-ioctl_bth_sdp_submit_record.

Upvotes: 0

Related Questions