📄 btpairingdlg.cpp
字号:
void CBTPairingDlg::RefreshPhoneList(bool bSearchDevices)
{
const int iMaxSearchTime = 240;
// This is the maximum time to be spent to the device search, in seconds.
// The required time may vary a lot, make sure it is big enough. If this
// timeout value is too small, CONASearchDevices returns ECONA_FAILED_TIMEOUT,
// and PC Suite Connectivity API doesn't find all available devices.
DWORD dwRet = ECONA_UNKNOWN_ERROR;
DWORD dwDeviceCount = 0;
DWORD dw = 0;
CONAPI_CONNECTION_INFO *pInfo = NULL;
CBTDevice *pDevice = NULL;
if(m_hDM)
{
if(bSearchDevices)
{
// reset search cancel flag and show progress dialog.
g_bSearchCancelled = FALSE;
g_ProgressDlg.Create(IDD_SEARCH_PROGRESS_DIALOG, this);
g_ProgressDlg.ShowWindow(SW_SHOW);
ProcessMessages();
DWORD dwSearchOptions = API_MEDIA_BLUETOOTH;
if(m_CheckUseCache.GetCheck() == BST_CHECKED)
{
dwSearchOptions |= CONAPI_ALLOW_TO_USE_CACHE;
}
switch(m_ComboSearchOptions.GetCurSel())
{
case 1:
dwSearchOptions |= CONAPI_GET_PAIRED_PHONES;
break;
case 2:
dwSearchOptions |= CONAPI_GET_TRUSTED_PHONES;
break;
case 0:
default:
dwSearchOptions |= CONAPI_GET_ALL_PHONES;
break;
}
dwRet = CONASearchDevices(m_hDM,
dwSearchOptions,
iMaxSearchTime,
(PFN_CONA_SEARCH_CALLBACK)BTPairingNotifyCallback,
&dwDeviceCount,
&pInfo );
ClearPhoneList();
g_ProgressDlg.ShowWindow( SW_HIDE);
g_ProgressDlg.DestroyWindow();
if( dwRet != CONA_OK)
{
ErrorMessageDlg( L"CONASearchDevices failed.", dwRet);
}
if( pInfo)
{
for( dw = 0; dw < dwDeviceCount; dw++)
{
pDevice = new CBTDevice( pInfo[dw].pstrDeviceName,
pInfo[dw].pstrAddress,
pInfo[dw].dwDeviceID,
pInfo[dw].dwState );
m_PhoneList.InsertItem( dw, pInfo[dw].pstrDeviceName);
m_PhoneList.SetItemData( dw, (DWORD_PTR)pDevice);
}
dwRet = CONAFreeConnectionInfoStructures( dwDeviceCount, &pInfo);
if( dwRet != CONA_OK)
{
ErrorMessageDlg( L"CONAFreeConnectionInfoStructures failed.", dwRet);
}
}
if( dwRet == ECONA_CANCELLED)
{
m_PhoneList.InsertItem( dw++, L"Search cancelled");
}
else if( dwRet == ECONA_FAILED_TIMEOUT)
{
m_PhoneList.InsertItem( dw++, L"Timeout reached");
}
else if( dwRet != CONA_OK)
{
// other error happened
m_PhoneList.InsertItem( dw++, L"Search failed");
}
}
// update device status to the list
dwDeviceCount = m_PhoneList.GetItemCount();
for( dw=0; dw<dwDeviceCount; dw++)
{
pDevice = (CBTDevice*)m_PhoneList.GetItemData( dw);
if( pDevice)
{
m_PhoneList.SetItemText( dw, 1, TrustedInfo2String( pDevice->GetStatus()));
}
}
}
}
//===================================================================
// OnDestroy
//
//===================================================================
void CBTPairingDlg::OnDestroy()
{
ClearPhoneList();
CDialog::OnDestroy();
}
//===================================================================
// ChangeTrustedState
//
// Changes the device trusted state. dwTrustedState may be
// CONAPI_PAIR_DEVICE, CONAPI_UNPAIR_DEVICE, CONAPI_SET_PCSUITE_TRUSTED
// or CONAPI_SET_PCSUITE_UNTRUSTED. lpszPassword is needed only
// with CONAPI_PAIR_DEVICE, with other operations it must be NULL.
//===================================================================
DWORD CBTPairingDlg::ChangeTrustedState(DWORD dwTrustedState, LPCWSTR lpszPassword)
{
DWORD dwRet = ECONA_UNKNOWN_ERROR;
CBTDevice *pDevice = NULL;
CWaitCursor wait;
if( m_hDM)
{
// get selected device from the list
pDevice = GetSelectedDevice();
if( pDevice)
{
dwRet = CONAChangeDeviceTrustedState( m_hDM,
dwTrustedState,
pDevice->GetAddress(),
lpszPassword,
NULL);
if( dwRet == CONA_OK)
{
// change the device status to the list according to return value
// re-searching all devices would take too long time
if( dwTrustedState & CONAPI_PAIR_DEVICE)
{
pDevice->SetStatus( (pDevice->GetStatus() & CONAPI_DEVICE_PCSUITE_TRUSTED) | CONAPI_DEVICE_PAIRED);
}
else if( dwTrustedState & CONAPI_UNPAIR_DEVICE)
{
pDevice->SetStatus( (pDevice->GetStatus() & CONAPI_DEVICE_PCSUITE_TRUSTED) | CONAPI_DEVICE_UNPAIRED);
}
if( dwTrustedState & CONAPI_SET_PCSUITE_TRUSTED)
{
pDevice->SetStatus( pDevice->GetStatus() | CONAPI_DEVICE_PCSUITE_TRUSTED);
}
else if( dwTrustedState & CONAPI_SET_PCSUITE_UNTRUSTED)
{
pDevice->SetStatus( pDevice->GetStatus() & ~CONAPI_DEVICE_PCSUITE_TRUSTED);
}
RefreshPhoneList(false);
}
else
{
ErrorMessageDlg( L"CONAChangeDeviceTrustedState failed.", dwRet);
}
}
else
{
AfxMessageBox( L"Please select the phone from the list.");
}
}
return dwRet;
}
//===================================================================
// OnButtonSearch
//
// Search button message handler
//===================================================================
void CBTPairingDlg::OnButtonSearch()
{
RefreshPhoneList(true);
}
//===================================================================
// OnButtonPair
//
// Pairing button message handler
//===================================================================
void CBTPairingDlg::OnButtonPair()
{
CBTPasswordDlg dlg;
dlg.SetPassword(L"123");
if(dlg.DoModal() == IDOK)
{
ChangeTrustedState(CONAPI_PAIR_DEVICE, dlg.GetPassword());
}
}
//===================================================================
// OnButtonUnpair
//
// Unpair button message handler
//===================================================================
void CBTPairingDlg::OnButtonUnpair()
{
ChangeTrustedState( CONAPI_UNPAIR_DEVICE, NULL);
}
//===================================================================
// OnButtonSetTrusted
//
// "Set device trusted" button message handler
//===================================================================
void CBTPairingDlg::OnButtonSetTrusted()
{
ChangeTrustedState( CONAPI_SET_PCSUITE_TRUSTED, NULL);
}
//===================================================================
// OnButtonSetUntrusted
//
// "Set device untrusted" button message handler
//===================================================================
void CBTPairingDlg::OnButtonSetUntrusted()
{
ChangeTrustedState( CONAPI_SET_PCSUITE_UNTRUSTED, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -