简体   繁体   English

如何连接到 Windows 上的蓝牙设备?

[英]How to connect to bluetooth device on Windows?

I would like to allow the user to connect to paired audio devices directly from the app instead of navigating to the bluetooth settings manually.我想允许用户直接从应用程序连接到配对的音频设备,而不是手动导航到蓝牙设置。

I am successfully listing all bluetooth devices using WinRT Apis:我使用 WinRT API 成功列出了所有蓝牙设备:

var result = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector());
// allow user to select a device
DeviceInfo d = await SelectDevice(result);
BluetoothDevice bluetoothDevice = await BluetoothDevice.FromIdAsync(d.Id);

As the WinRT-Apis do not expose any "Connect" interface (Remember, I want to connect the device as Windows would not communicate with it myself), I'm exploring using P/Invoke, so I use the following after reading this answer on superuser.com which suggests using BluetoothSetServiceState :由于 WinRT-Apis 不公开任何“连接”接口(请记住,我想连接设备,因为 Windows 不会自己与之通信),我正在探索使用 P/Invoke,所以我在阅读此答案后使用以下内容建议使用BluetoothSetServiceState superuser.com上:


// Definitions
private const string bluetoothDll = "bthprops.cpl";

[DllImport(bluetoothDll, ExactSpelling = true, SetLastError = true)]
private static extern uint BluetoothSetServiceState(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, ref Guid pGuidService, uint dwServiceFlags);

[DllImport(bluetoothDll, SetLastError = true)]
private static extern IntPtr BluetoothFindFirstRadio(ref Bluetooth_Find_Radio_Params pbtfrp, out IntPtr phRadio);

[DllImport(bluetoothDll, SetLastError = true)]
private static extern bool BluetoothFindRadioClose(IntPtr findHandle);

[DllImport(bluetoothDll, SetLastError = true)]
private static extern uint BluetoothGetDeviceInfo(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi);

private const uint BLUETOOTH_SERVICE_DISABLE = 0;
private const uint BLUETOOTH_SERVICE_ENABLE = 0x00000001;

// Code (using the bluetoothDevice obtained from the WinRT Api)
using(var pointer = GetRadioPointer())
{
    BLUETOOTH_DEVICE_INFO deviceInfo = new BLUETOOTH_DEVICE_INFO
    {
        Address = bluetoothDevice.BluetoothAddress,
        dwSize = (uint)Marshal.SizeOf<BLUETOOTH_DEVICE_INFO>()
    };

    uint result = BluetoothGetDeviceInfo(pointer.Handle, ref deviceInfo);

    Guid serviceRef = InTheHand.Net.Bluetooth.BluetoothService.Handsfree;
    result = BluetoothSetServiceState(pointer.Handle, ref deviceInfo, ref serviceRef, 1);
}

// I get the radio like this:

private RadioHandle GetRadioPointer()
{
    Bluetooth_Find_Radio_Params pbtfrp = new Bluetooth_Find_Radio_Params();
    pbtfrp.Initialize();

    IntPtr findHandle = IntPtr.Zero;
    try
    {
        findHandle = BluetoothFindFirstRadio(ref pbtfrp, out IntPtr phRadio);
        return new RadioHandle(phRadio);
    }
    finally
    {
        if (findHandle != IntPtr.Zero)
        {
            BluetoothFindRadioClose(findHandle);
        }
    }
}

However, I cannot get it to work.但是,我无法让它工作。 BluetoothSetServiceState always returns 87, which is ERROR_INVALID_PARAMETER and nothing happens. BluetoothSetServiceState始终返回 87,即ERROR_INVALID_PARAMETER并且没有任何反应。 Any idea on how to solve this?关于如何解决这个问题的任何想法? Using the command line tools referenced in the superuser-post, it works...使用超级用户帖子中引用的命令行工具,它可以工作...

Thanks for your help.谢谢你的帮助。

Why would you ever expect that the following line would work:为什么您会期望以下行会起作用:

private const string bluetoothDll = "bthprops.cpl";

when MSDN's page states:MSDN 的页面指出:

DLL: Bthprops.dll

? ?

I found it out by chance myself and it works now.我自己偶然发现了它,现在可以使用了。 Depending whether the service is already in the state (even if the device is disconnected), you will need to turn it off before.根据服务是否已经处于状态(即使设备断开连接),您需要先关闭它。 So turning it off and on again works:因此,将其关闭并再次打开有效:

BluetoothSetServiceState(pointer.Handle, ref deviceInfo, ref serviceRef, 0);
BluetoothSetServiceState(pointer.Handle, ref deviceInfo, ref serviceRef, 1);

So as it turns out, you can connect a device if you enumerate the services, turn all off and on again.因此,事实证明,如果您枚举服务,关闭所有服务并再次打开,您就可以连接设备。 Disconnecting works by turning all off.断开连接的工作原理是全部关闭。 When the last one is off, Windows disconnects the device.当最后一个关闭时,Windows 会断开设备的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows Store应用程序上连接到第三方蓝牙设备? - How To Connect To A 3rd Party Bluetooth Device On A Windows Store App? 连接到蓝牙设备/如何设置rfcomm功能 - Connect to Bluetooth Device / how to set the rfcomm capability 如何在Windows 10 UWP中丢失连接后以编程方式连接到配对的蓝牙设备 - How to programmatically connect to paired Bluetooth device once connection is lost in Windows 10 UWP 在Windows 8 / 8.1应用商店中搜索并连接到蓝牙设备? - Search and Connect to Bluetooth device in Windows 8/8.1 Store apps? 使用 windows 10 绕过 c# 中的连接蓝牙设备权限 - Bypassing connect bluetooth device permission in c# with windows 10 如何从Windows CE读取蓝牙数据到蓝牙设备 - How to read bluetooth data from Windows CE to bluetooth Device 如何使用 Android Java 有效地连接到蓝牙设备套接字? - How to effectively connect to Bluetooth Device socket with Android Java? 如何使用Windows Phone 8从蓝牙设备连续读取数据 - How to Read Data continuously from bluetooth device using windows phone 8 如何使用C#.NET在Windows中“取消配对”,“删除”蓝牙设备 - How to “unpair”, “remove” Bluetooth device in Windows with C# .NET Windows Phone应用程序蓝牙连接 - Windows phone application bluetooth connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM