我正在尝试编写一个程序,使与Windows配对的Wiimotes的管理更加简单和自动化。该程序使用WiimoteLib (使用hidsdi.h和setupapi.h)连接到Wiimote设备,使用32feet (使用{{3 }})自动配对/取消配对设备。配对/取消配对的代码基于Windows Bluetooth API。目前,该过程有些颠簸且缓慢,但是可以正常工作。 (但仅适用于一个Wiimote)
问题是我配对/取消配对蓝牙设备的模块没有有关如何识别HID设备(由Wiimote类使用)是否为同一设备的信息。如果蓝牙设备已被强制关闭或未配对,我希望能够向Wiimote类发出警报,以便它可以正常断开连接。反之亦然,我希望Wiimote在断开HID设备的连接时提醒配对器/取消配对器,以便可以有选择地取消配对Bluetooth设备(假设您计划关闭Wiimote)。
如果我只想访问一个Wiimote,那么这不是什么大问题,但是我希望能够访问多个Wiimotes,并能够通过使用其HID信息和蓝牙信息来区分它们。我已经在使用自己的P / Invoke来弥补32feet所欠缺的区域,因此再使用它就不是问题。
这是我的配对器的主要代码。 (尽管我不确定是否真的必要):
(注意:IsDiscoverable()
,ToPin()
和ToMacAddress()
都是扩展方法。)
// Once this is true, the Wiimote will
// attempt to connect to an HID device.
public bool IsAnyDeviceAvailable { get; private set; }
private void PairTask(CancellationToken token) {
// Setup automatic authentication
BluetoothWin32Authentication auth = new BluetoothWin32Authentication(OnHandleRequests);
while (!token.IsCancellationRequested)
PairLoop(token);
}
private void PairLoop(CancellationToken token) {
// Get a copy of known addresses since
// these are added to in another task.
BluetoothAddress[] addresses;
lock (knownAddresses)
addresses = KnownAddresses.ToArray();
bool available = false;
foreach (BluetoothAddress address in addresses) {
if (token.IsCancellationRequested)
return;
BluetoothDeviceInfo device = new BluetoothDeviceInfo(address);
if (device.Connected) {
if (!available && !IsAnyDeviceAvailable) {
lock (knownAddresses)
IsAnyDeviceAvailable = true;
}
available = true;
continue;
}
if (device.Remembered) {
RemoveDevice(device, token);
}
else if (device.IsDiscoverable() && !device.Authenticated) {
if (PairDevice(device, token, available))
available = true;
}
token.WaitHandle.WaitOne(500);
}
if (!available && IsAnyDeviceAvailable) {
Trace.WriteLine("No more devices connected");
lock (knownAddresses)
IsAnyDeviceAvailable = false;
}
}
private void RemoveDevice(BluetoothDeviceInfo device, CancellationToken token) {
token.WaitHandle.WaitOne(1000);
if (BluetoothSecurity.RemoveDevice(device.DeviceAddress)) {
Trace.WriteLine($"Wiimote removed: {device.DeviceAddress.ToMacAddress()}");
token.WaitHandle.WaitOne(2000);
}
}
private bool PairDevice(BluetoothDeviceInfo device, CancellationToken token,
bool available)
{
string pin = device.DeviceAddress.ToPin();
try {
if (BluetoothSecurity.PairRequest(device.DeviceAddress, pin)) {
Trace.WriteLine($"Wiimote authenticated: {device.DeviceAddress.ToMacAddress()}");
token.WaitHandle.WaitOne(1000);
// Calling this before and after seems to help unsure
// the device works when paired programmatically.
Guid[] services = device.InstalledServices;
device.SetServiceState(Uuids.HumanInterfaceDeviceServiceClass_UUID, true, true);
services = device.InstalledServices;
Trace.WriteLine($"Wiimote paired: {device.DeviceAddress.ToMacAddress()}");
token.WaitHandle.WaitOne(8000);
if (!available && !IsAnyDeviceAvailable) {
Trace.WriteLine("First device has been connected");
lock (knownAddresses)
IsAnyDeviceAvailable = true;
}
return true;
}
else {
Trace.WriteLine($"Wiimote authentication failed: {device.DeviceAddress.ToMacAddress()}");
}
}
catch {
Trace.WriteLine($"Wiimote pairing failed: {device.DeviceAddress.ToMacAddress()}");
}
return false;
}
private void OnHandleRequests(object sender, BluetoothWin32AuthenticationEventArgs e) {
e.Confirm = true;
}
答案 0 :(得分:2)
您不需要与Wiimote配对。与Wiimote配对仅做一件事:Wiimote记住配对设备的MAC,然后可以将其运行并连接到它(太Wii或其他设备)。但是,它在Windows上无法使用,因此不需要配对。如果您需要配对,请使用旧版PIN配对。 PIN是按反向字节顺序的Wiimote MAC。
使用BluetoothSetServiceState将您的Wiimote作为HID设备添加到系统中。
以下代码显示了如何通过其MAC查找Wiimote HID(该代码摘自我们的Wireless Communication Library,其中包括对Wiimote的支持)。
m_fInstalled = true;
Sleep(1000);
CwclStringList* Wiis = new CwclStringList();
m_sDevicePath = WCL_EMPTY_STR;
DWORD dwTik = GetTickCount();
while (m_sDevicePath == WCL_EMPTY_STR && GetTickCount() - dwTik < 20000)
if (EnumHID(*Wiis) == WCL_E_SUCCESS)
{
CwclString sAddress = GetBluetoothParams()->GetAddress();
CwclString sAdr = sAddress.Mid(1, 2) + sAddress.Mid(4, 2) + sAddress.Mid(7, 2) + sAddress.Mid(10, 2) + sAddress.Mid(13, 2) + sAddress.Mid(16, 2);
HKEY hKey;
CwclString aRegKey(WCL_WII_REG_KEY);
DWORD dwRes = RegOpenKey(HKEY_LOCAL_MACHINE, aRegKey, &hKey);
if (dwRes != ERROR_SUCCESS)
aRegKey = CwclString(WCL_WII_REG_KEY_NEW);
dwRes = RegOpenKey(HKEY_LOCAL_MACHINE, aRegKey, &hKey);
if (dwRes == ERROR_SUCCESS)
{
DWORD dwNdx = 0;
WCHAR szSubKeyName[512];
DWORD dwSubKeySize = sizeof(szSubKeyName);
ZeroMemory(szSubKeyName, dwSubKeySize);
while (RegEnumKeyEx(hKey, dwNdx, szSubKeyName, &dwSubKeySize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
CwclString sSubKeyStr = CwclString(szSubKeyName);
if (sSubKeyStr.Find(sAdr) >= 0)
{
CwclString sSubKeyPath = aRegKey + L"\\" + sSubKeyStr;
HKEY hSubKey;
dwRes = RegOpenKey(HKEY_LOCAL_MACHINE, sSubKeyPath, &hSubKey);
if (dwRes == ERROR_SUCCESS)
{
WCHAR szValue[512];
DWORD dwValueSize = sizeof(szValue);
ZeroMemory(szValue, dwValueSize);
dwRes = RegQueryValueEx(hSubKey, L"ParentIdPrefix", NULL, NULL, (LPBYTE)szValue, &dwValueSize);
if (dwRes == ERROR_SUCCESS)
{
CwclString sValueStr = CwclString(szValue);
for (INT_PTR i = 0; i < Wiis->GetCount(); i++)
if (Wiis->GetItems(i).Find(sValueStr) >= 0)
{
m_sDevicePath = Wiis->GetItems(i);
break;
}
}
RegCloseKey(hSubKey);
}
}
if (m_sDevicePath != WCL_EMPTY_STR)
break;
dwSubKeySize = sizeof(szSubKeyName);
ZeroMemory(szSubKeyName, dwSubKeySize);
dwNdx++;
}
RegCloseKey(hKey);
}
}
一旦获得DevicePath,就可以使用CreateFile打开HID设备的句柄。
答案 1 :(得分:1)
我忘记添加EnumHid函数。在这里
header {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
align-self: center;
}
要检测Wiimote断开连接,请在从Wiimote读取HID数据包时在ReadFile上使用错误。该方法适用于任何蓝牙驱动程序(所有关于MS的驱动程序)。但是,对于MS,您还可以使用HCI_DISCONNECT标志来处理WM_DEVICE_CHANGE。
在那里使用的成分:
int CwclWiimote::EnumHID(CwclStringList& rWiis)
{
if (!wclIsTransportAvailable(trBluetooth))
return WCL_E_TRANSPORT_NOT_AVAILABLE;
GUID Guid;
HidD_GetHidGuid(&Guid);
HDEVINFO hDevInfo = SetupDiGetClassDevs(&Guid, NULL, 0, DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
return WCL_E_INTERNAL;
SP_DEVICE_INTERFACE_DATA diData;
ZeroMemory(&diData, sizeof(SP_DEVICE_INTERFACE_DATA));
diData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
rWiis.Clear();
DWORD dwIndex = 0;
PSP_DEVICE_INTERFACE_DETAIL_DATA_W diDetail;
while (SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &Guid, dwIndex, &diData))
{
DWORD dwSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &diData, NULL, 0, &dwSize, NULL);
diDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)LocalAlloc(LPTR, dwSize);
if (diDetail)
{
ZeroMemory(diDetail, dwSize);
diDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &diData, diDetail, dwSize, &dwSize, NULL))
{
HANDLE hHandle = CreateFile(diDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hHandle != INVALID_HANDLE_VALUE)
{
HIDD_ATTRIBUTES Attrib;
ZeroMemory(&Attrib, sizeof(HIDD_ATTRIBUTES));
Attrib.Size = sizeof(HIDD_ATTRIBUTES);
if (HidD_GetAttributes(hHandle, &Attrib))
if (Attrib.VendorID == WCL_WII_VID && (Attrib.ProductID == WCL_WII_PID || Attrib.ProductID == WCL_WII_PID_NEW))
{
CwclString Str = diDetail->DevicePath;
rWiis.Add(Str);
}
CloseHandle(hHandle);
}
}
LocalFree((HLOCAL)diDetail);
}
dwIndex++;
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return WCL_E_SUCCESS;
}