问题遇到的现象和发生背景
最近在组长给了两个游戏手柄,一个是有线USB接口的阿修罗手柄,一个是无线的xbox one游戏手柄,xbox 的手柄将USB接口的接收器插
在电脑上手柄通过蓝牙匹配接收器,就连接上了电脑,识别出来依然是USB设备。我用了libusb库和hidpai这两个库,都能正常识别并打开有线
的游戏手柄,正常接收数据。但却不能正常打开或接收xbox的无线手柄。用libusb库是直接识别不到手柄,只能识别到接收器,用hidapi库能识
别到手柄,且能打开,但是不能接收到手柄的数据。但是下了给bus hound工具,这个工具却可以正常抓取到所有数据。
问题相关代码,请勿粘贴截图
下面是我用的libusb的代码
int ret = libusb_open(m_pDev,&m_devHandle);
if(ret != 0)
{
qDebug()<<"open usb dev failed";
qDebug()<<libusb_error_name(ret)<<" error"<<ret;
m_devHandle = nullptr;
return;
}
else
{
qDebug()<<"open usb success";
}
ret = libusb_kernel_driver_active(m_devHandle,0);
if(ret == 1)
{
libusb_detach_kernel_driver(m_devHandle,0);
}
ret = libusb_claim_interface(m_devHandle,0);
if(ret != 0)
{
libusb_close(m_devHandle);
m_devHandle = nullptr;
return;
}
int ret = libusb_bulk_transfer(m_devHandle,0x81,(unsigned char*)&cmd,sizeof(cmd),&nActualByte,1024);
if(ret != 0)
{
qDebug()<<"receive failed";
}
else
{
QByteArray ba((char*)&cmd,nActualByte);
txtFile.write(ba.toHex());
txtFile.write("\r\n");
txtFile.flush();
}
下面是用的hidapi的代码
hid_device* handle = nullptr;
handle = hid_open(0x45e,0x02ea,nullptr);
if(handle == nullptr)
{
qDebug()<<"open xbox failed";
return;
}
wchar_t wstr[256] ={0};
int ret = hid_get_manufacturer_string(handle,wstr,256);
if(ret < 0)
qDebug()<<"unable to read manufacturer string";
printf("manufacturer string: %ls\n",wstr);
wstr[0] = 0x0000;
ret = hid_get_product_string(handle,wstr,256);
if(ret < 0)
qDebug()<<"unable to read product string";
printf("product string %ls\n",wstr);
wstr[0] = 0x0000;
ret = hid_get_serial_number_string(handle,wstr,256);
if(ret <0)
qDebug()<<"unable to read serial number string\n";
printf("serial number string: %d %ls\n",wstr[0],wstr);
hid_set_nonblocking(handle,0);
unsigned char buff[1024];
memset(buff,0,1024);
// buff[0] = 0x01;
// buff[1] = 0x81;
// int res = hid_write(handle,buff,17);
// if(res < 0)
// {
// qDebug()<<"unable to write";
// return;
// }
while(true)
{
int res = hid_read(handle,buff,1024);
if(res < 0)
{
qDebug()<<"read failed";
break;
}
else if(res == 0)
{
continue;
}
QByteArray ba((char*)buff,res);
qDebug()<<ba.toHex();
}
运行结果及报错内容
这两个代码用有线手柄时都能正常接收。但是无线手柄不行
我的解答思路和尝试过的方法
求解决方案