首页
社区
课程
招聘
未解决 [求助]Windows 10/11,建立蓝牙socket提示,0x80072740,怀疑被系统设置占用了?
发表于: 2024-2-21 09:04 1299

未解决 [求助]Windows 10/11,建立蓝牙socket提示,0x80072740,怀疑被系统设置占用了?

2024-2-21 09:04
1299

想通过rfcomm获取蓝牙模块的电量,建立socket时提示0x80072740,但是如果我在设置中断开连接的设备,再跑代码就没这个问题,于是我想通过代码断开设备,但是貌似没有相关的api。

请问下能否通过hook或者一些旁门左道获取这个电量,还是说有更合适的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
int main()
{
    init_apartment();
 
    auto bluetoothdevice = BluetoothDevice::GetDeviceSelector();
    auto devInfoCollAsync = DeviceInformation::FindAllAsync(bluetoothdevice);
 
    Windows::Foundation::TimeSpan waitTime(1000);
 
    //devInfoCollAsync.wait_for(waitTime);
 
    DeviceInformationCollection devInfoColl = devInfoCollAsync.get();
 
    for (int i = 0; i < devInfoColl.Size(); i++)
    {
        //Console.WriteLine(String.Format("{0}. {1}\t{2}", i, devices[i].Name, devices[i].Id));
        auto devinfoItem = devInfoColl.GetAt(i);
 
        string devieInfo = to_string(devinfoItem.Name()) + "--->" + to_string(devinfoItem.Id());
        cout << devieInfo << "\n";
    }
 
    int choeseIndex = -1;
    cout << "input number: ";
    cin >> choeseIndex;
 
    auto blDeviceAsync = BluetoothDevice::FromIdAsync(devInfoColl.GetAt(choeseIndex).Id());
    BluetoothDevice blDevice = blDeviceAsync.get();
 
    if (blDevice.ConnectionStatus() == BluetoothConnectionStatus::Connected)
    {
        blDevice.Close();
        //如果已连接,则断开从连
    }
 
 
 
    cout << "\n Discovering Rfcomm services..";
 
    auto rfcommResultAsync = blDevice.GetRfcommServicesAsync();
 
    RfcommDeviceServicesResult rfcommResult = rfcommResultAsync.get();
 
    //print service
 
    for (int i = 0; i < rfcommResult.Services().Size(); i++)
    {
        auto serviceItem = rfcommResult.Services().GetAt(i);
 
        string serviceInfo = to_string(to_hstring(serviceItem.ServiceId().Uuid()));
 
        cout << serviceInfo << "\n";
    }
 
    cout << "input number: ";
    cin >> choeseIndex;
    //try
     
 
        StreamSocket socket;
 
        try
        {
            auto service = rfcommResult.Services().GetAt(choeseIndex);
            cout << "\nrfcommDev  ConnectionHostName: " << to_string(service.ConnectionHostName().ToString());
            cout << "\nrfcommDev  ConnectionServiceName: " << to_string(service.ConnectionServiceName());
             
            auto rfcsp = RfcommServiceProvider::CreateAsync(service.ServiceId()).get();
            //rfcsp.
             
            //0x80072740
            //通常每个套接字地址(协议/网络地址/端口)只允许使用一次
            socket.ConnectAsync(rfcommResult.Services().GetAt(choeseIndex).ConnectionHostName(),
            //to_hstring("Bluetooth#Bluetoothc1:3c:59:76:b7:c6-60:f4:3a:8f:90:33#RFCOMM:00000000:{0000111e-0000-1000-8000-00805f9b34fb}"),
                rfcommResult.Services().GetAt(choeseIndex).ConnectionServiceName(),
                SocketProtectionLevel::BluetoothEncryptionAllowNullAuthentication).get();
        }
        catch (winrt::hresult_error const& ex)
        {
            winrt::hresult hr = ex.code(); // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND).
            winrt::hstring message = ex.message(); // The system cannot find the file specified.
            cout << "hresult: " << hr.value << "\n";
            cout << "message: " << to_string(message) << "\n";
        }
     
    Sleep(700);
 
    DataReader reader(socket.InputStream());
    DataWriter writer(socket.OutputStream());
 
    gwriter = &writer;
 
    //设置为非阻塞
    reader.InputStreamOptions(InputStreamOptions::Partial);
 
    write(&socket, "\r\nOK\r\n", &writer);
 
    while (true)
    {
        ReceiveStringLoop(&reader, &socket);
        Sleep(200);
    }
}

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//