WIFI DIrect API pair always failed
Hi Everyone,
My app need use WIFI Direct feature, so I refer to a Microsoft sample Code (WiFiDirectLegacyAPDemo).
For some reason, I can't use legacy mode of WIFI Direct, so I need call customer pair when got a connection request, but it always get a DevicePairingResultStatus_Failed.
This is code: https://1drv.ms/u/s!AvrPdsDWcn8akMEN-iBLhtILwLF-RA?e=g4mRHJ
Use this test app, you start a WIFI direct access point in one notebook, and connect it in another notebook:
Best Regards
ZC
Windows API - Win32
Windows 11
-
Jeanine Zhang-MSFT 9,786 Reputation points • Microsoft Vendor
2024-08-12T06:15:41.5566667+00:00 Could you please tell us how do you call customer pair? Whether you could try to use the official sample to reproduce the issue?
The code link you provided couldn't be opened due to our policy. Could you please provide a minimum code via GitHub to help us reproduce the issue?
-
zheng cheng 1 Reputation point
2024-08-12T08:00:06.67+00:00 Hello Jeanine,
Below is my code how to call customer pair. The official sample code has no customer pair feature. I will put to GitHub latter.
void WlanHostedNetworkHelper::StartListener() { HRESULT hr = S_OK; // Create WiFiDirectConnectionListener hr = Windows::Foundation::ActivateInstance(HStringReference(RuntimeClass_Windows_Devices_WiFiDirect_WiFiDirectConnectionListener).Get(), &_connectionListener); if (FAILED(hr)) { throw WlanHostedNetworkException("ActivateInstance for WiFiDirectConnectionListener failed", hr); } hr = _connectionListener->add_ConnectionRequested( Callback<ConnectionRequestedHandler>([this](IWiFiDirectConnectionListener* sender, IWiFiDirectConnectionRequestedEventArgs* args) -> HRESULT { HRESULT hr = S_OK; ComPtr<IWiFiDirectConnectionRequest> request; if (_listener != nullptr) { _listener->LogMessage(L"Connection Requested..."); } bool acceptConnection = true; if (!_autoAccept && _prompt != nullptr) { acceptConnection = _prompt->AcceptIncommingConnection(); } try { hr = args->GetConnectionRequest(request.GetAddressOf()); if (FAILED(hr)) { throw WlanHostedNetworkException("Get connection request for ConnectionRequestedEventArgs failed", hr); } if (acceptConnection) { HString deviceId; ComPtr<IDeviceInformation> deviceInformation; hr = request->get_DeviceInformation(deviceInformation.GetAddressOf()); if (FAILED(hr)) { throw WlanHostedNetworkException("Get device information for ConnectionRequest failed", hr); } hr = deviceInformation->get_Id(deviceId.GetAddressOf()); if (FAILED(hr)) { throw WlanHostedNetworkException("Get ID for DeviceInformation failed", hr); } #if 0 this->ConnectDeviceInternal(deviceId.Get()); #else ComPtr<IDeviceInformation2> info2; HRESULT hr = deviceInformation->QueryInterface(IID_PPV_ARGS(&info2)); if (FAILED(hr)) { throw WlanHostedNetworkException("Get DeviceInformation2 failed", hr); } this->PairDeviceInternal(deviceId.GetRawBuffer(NULL), info2.Get()); #endif } else { if (_listener != nullptr) { _listener->LogMessage(L"Declined"); } } } catch (WlanHostedNetworkException& e) { if (_listener != nullptr) { std::wostringstream ss; ss << e.what() << ": " << e.GetErrorCode(); _listener->OnAsyncException(ss.str()); } return e.GetErrorCode(); } return hr; }).Get(), &_connectionRequestedToken); if (FAILED(hr)) { throw WlanHostedNetworkException("Add ConnectionRequested handler for WiFiDirectConnectionListener failed", hr); } if (_listener != nullptr) { _listener->LogMessage(L"Connection Listener is ready"); } } void WlanHostedNetworkHelper::PairDeviceInternal(const wchar_t* szDeviceId, ABI::Windows::Devices::Enumeration::IDeviceInformation2* pDevInfo2) { ComPtr<IDeviceInformationPairing> devInfoPair; HRESULT hr = pDevInfo2->get_Pairing(&devInfoPair); if (SUCCEEDED(hr)) { boolean bCanPair = false; boolean isPaired = false; hr = devInfoPair->get_IsPaired(&isPaired); hr = devInfoPair->get_CanPair(&bCanPair); if (!isPaired) { ComPtr<IDeviceInformationPairing2> devInfoPair2; hr = devInfoPair.As(&devInfoPair2); if (SUCCEEDED(hr)) { ComPtr<IDeviceInformationCustomPairing> spCustomPairing; hr = devInfoPair2->get_Custom(spCustomPairing.GetAddressOf()); if (FAILED(hr)) { throw WlanHostedNetworkException("Get IDeviceInformationCustomPairing failed", hr); } ComPtr<IWiFiDirectConnectionParameters> param; hr = Windows::Foundation::ActivateInstance(HStringReference(RuntimeClass_Windows_Devices_WiFiDirect_WiFiDirectConnectionParameters).Get(), ¶m); if (FAILED(hr)) { throw WlanHostedNetworkException("ActivateInstance IWiFiDirectConnectionParameters failed", hr); } hr = param->put_GroupOwnerIntent(15); DevicePairingKinds devicePairingKinds = DevicePairingKinds::DevicePairingKinds_ConfirmOnly | DevicePairingKinds::DevicePairingKinds_DisplayPin/* | DevicePairingKinds::DevicePairingKinds_ProvidePin*/; ComPtr<IWiFiDirectConnectionParameters2> spConParam2; hr = param.As(&spConParam2); if (FAILED(hr)) { throw WlanHostedNetworkException("Get IDevicePairingSettings failed", hr); } WiFiDirectPairingProcedure proc = WiFiDirectPairingProcedure::WiFiDirectPairingProcedure_GroupOwnerNegotiation; spConParam2->put_PreferredPairingProcedure(proc); ComPtr<IDevicePairingSettings> spSetting; hr = param.As(&spSetting); if (FAILED(hr)) { throw WlanHostedNetworkException("Get IDevicePairingSettings failed", hr); } spCustomPairing->add_PairingRequested(Callback<CustomPairHandler>([this](IDeviceInformationCustomPairing* pCustomPairing, IDevicePairingRequestedEventArgs* pArgs) -> HRESULT { OutputDebugString(L"pair requested.\n"); HString pin; ABI::Windows::Devices::Enumeration::DevicePairingKinds kinds; pArgs->get_PairingKind(&kinds); if (kinds == ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_DisplayPin) { pArgs->get_Pin(pin.GetAddressOf()); } if (_PairRequest) { std::wstring strPin = pin.GetRawBuffer(NULL); bool bAllowed = _PairRequest->PairRequest(kinds, strPin); if (bAllowed) { if (kinds & ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_ConfirmOnly | kinds & ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_DisplayPin) { pArgs->Accept(); } else if (kinds & ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_ProvidePin) { pin.Set(strPin.c_str()); pArgs->AcceptWithPin(pin.Get()); } } } return S_OK; }).Get(), &_DevicePairToken); ComPtr<IAsyncOperation<ABI::Windows::Devices::Enumeration::DevicePairingResult*>> asyncAction; hr = spCustomPairing->PairWithProtectionLevelAndSettingsAsync(devicePairingKinds, DevicePairingProtectionLevel::DevicePairingProtectionLevel_Default, spSetting.Get(), &asyncAction); if (SUCCEEDED(hr)) { IDeviceInformation2* pDevInfo = pDevInfo2; asyncAction->put_Completed(Callback<PairAsyncHandler>([this, pDevInfo](IAsyncOperation<DevicePairingResult*>* pHandler, AsyncStatus status) -> HRESULT { if (status == AsyncStatus::Completed) { HString id; ABI::Windows::Devices::Enumeration::DevicePairingResultStatus pairStatus; ComPtr<IDevicePairingResult> spResult; ComPtr<IDeviceInformation> devInfoInfo; HRESULT hr = pHandler->GetResults(spResult.GetAddressOf()); if (SUCCEEDED(hr)) { hr = spResult->get_Status(&pairStatus); } pDevInfo->QueryInterface(IID_PPV_ARGS(devInfoInfo.GetAddressOf())); if (devInfoInfo) { devInfoInfo->get_Id(id.GetAddressOf()); } if (pairStatus == ABI::Windows::Devices::Enumeration::DevicePairingResultStatus::DevicePairingResultStatus_Paired) { if (_listener != nullptr) _listener->OnDevicePaired(id.GetRawBuffer(NULL)); } else { if (_listener != nullptr) _listener->OnDevicePairedError(id.GetRawBuffer(NULL), pairStatus); } } else { if (_listener != nullptr) { switch (status) { case AsyncStatus::Started: _listener->LogMessage(L"Device pair, status=Started"); break; case AsyncStatus::Canceled: _listener->LogMessage(L"Device pair, status=Canceled"); break; case AsyncStatus::Error: _listener->LogMessage(L"Device pair, status=Error"); break; } } } return S_OK; }).Get()); } } } } }
-
zheng cheng 1 Reputation point
2024-08-12T08:17:31.8633333+00:00 -
Jeanine Zhang-MSFT 9,786 Reputation points • Microsoft Vendor
2024-08-13T03:21:33.25+00:00 As this issue is complex, to resolve this issue as soon as possible, you can open an incident via
Contact us
tab at link below: https://developer.microsoft.com/en-us/windows/support/Please choose the
Technical Support - Coding/Debugging
for Windows SDK for this issue. In-addition, if the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged.
Sign in to comment