📄 btpairingdlg.cs
字号:
}
//===================================================================
// FRM_BTPairing_Load
//
// Initialization of FRM_BTPairing form
//
//===================================================================
private void FRM_BTPairing_Load(object sender, System.EventArgs e)
{
BTRefreshPhoneList(true);
}
//===================================================================
// ClearPhoneList
//
// Removes all items from the phone list, and frees memory
// allocated for each list item data
//===================================================================
private void ClearPhoneList()
{
LVW_PhoneList.Items.Clear();
m_pDevices = null;
}
//===================================================================
// RefreshPhoneList
//
// Refreshes the phone list to the list control. If bSearchDevices
// is TRUE, searches all BT devices using connectivity API, else
// only copies the device information from memory to the list control.
//===================================================================
private void BTRefreshPhoneList(bool bSearchDevices)
{
// 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 Connectivity API doesn't find all available devices.
const int iMaxSearchTime = 240;
int iRet = PCCSErrors.ECONA_UNKNOWN_ERROR;
int iDeviceCount = 0;
int i = 0;
System.IntPtr pInfo= System.IntPtr.Zero;
CONADefinitions.CONAPI_CONNECTION_INFO[] Info = null;
if (m_hDMHandle != 0)
{
if ((bSearchDevices))
{
m_pfnSearchNotify = BTPairingNotifyCallback;
iRet = CONADeviceManagement.CONASearchDevices(m_hDMHandle, CONADefinitions.API_MEDIA_BLUETOOTH | CONADefinitions.CONAPI_GET_ALL_PHONES, iMaxSearchTime, m_pfnSearchNotify,ref iDeviceCount,ref pInfo);
if ((m_pProgressDlg != null))
{
// progress is at the end. Hide progress bar
m_pProgressDlg.Hide();
m_pProgressDlg = null;
}
ClearPhoneList();
m_pDevices = null;
m_pDevices = new BTDevice[iDeviceCount];
Info = null;
Info = new CONADefinitions.CONAPI_CONNECTION_INFO[iDeviceCount];
for (i = 0; i < iDeviceCount; i++)
{
// Calculate beginning of CONAPI_CONNECTION_INFO structure of item 'i'
System.Int64 iPtr = pInfo.ToInt64() + i * Marshal.SizeOf(typeof(CONADefinitions.CONAPI_CONNECTION_INFO));
// Convert integer to pointer
System.IntPtr ptr = new System.IntPtr(iPtr);
// Copy data from buffer
Info[i] = (CONADefinitions.CONAPI_CONNECTION_INFO)Marshal.PtrToStructure(ptr, typeof(CONADefinitions.CONAPI_CONNECTION_INFO));
m_pDevices[i].strName = Info[i].pstrDeviceName;
m_pDevices[i].strAddress = Info[i].pstrAddress;
m_pDevices[i].iDeviceID = Info[i].iDeviceID;
m_pDevices[i].iStatus = Info[i].iState;
LVW_PhoneList.Items.Add(Info[i].pstrDeviceName);
}
if (iRet == PCCSErrors.ECONA_CANCELLED)
{
LVW_PhoneList.Items.Add("Search cancelled");
}
else if (iRet == PCCSErrors.ECONA_FAILED_TIMEOUT)
{
LVW_PhoneList.Items.Add("Timeout reached");
}
else if (iRet != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("CONASearchDevices failed.", iRet);
LVW_PhoneList.Items.Add("Search failed");
}
if (iDeviceCount > 0)
{
iRet = CONADeviceManagement.CONAFreeConnectionInfoStructures(iDeviceCount,ref pInfo);
if (iRet != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("CONAFreeConnectionInfoStructures failed.", iRet);
}
}
}
// update device status to the list
iDeviceCount = LVW_PhoneList.Items.Count;
for (i = 0; i < iDeviceCount; i++)
{
if (LVW_PhoneList.Items[i].SubItems.Count <= 1)
{
LVW_PhoneList.Items[i].SubItems.Add(PCCAPIUtils.TrustedInfo2String(m_pDevices[i].iStatus));
}
else
{
LVW_PhoneList.Items[i].SubItems[1].Text = PCCAPIUtils.TrustedInfo2String(m_pDevices[i].iStatus);
}
}
}
}
//===================================================================
// ChangeTrustedState
//
// Changes the device trusted state. iTrustedState may be
// CONAPI_PAIR_DEVICE, CONAPI_UNPAIR_DEVICE, CONAPI_SET_PCSUITE_TRUSTED
// or CONAPI_SET_PCSUITE_UNTRUSTED. strPassword is needed only
// with CONAPI_PAIR_DEVICE, with other operations it should be NULL.
//===================================================================
private int ChangeTrustedState(int iTrustedState, string strPassword)
{
int iRet = PCCSErrors.ECONA_UNKNOWN_ERROR;
if (m_hDMHandle != 0)
{
// get selected device from the list
int iSelIndex = -1;
if (LVW_PhoneList.SelectedIndices.Count > 0)
{
iSelIndex = LVW_PhoneList.SelectedItems[0].Index;
}
if (iSelIndex >= 0)
{
iRet = CONADeviceManagement.CONAChangeDeviceTrustedState(m_hDMHandle, iTrustedState, m_pDevices[iSelIndex].strAddress, strPassword, null);
if (iRet == PCCSErrors.CONA_OK)
{
// change the device status to the list according to return value
// re-searching all devices would take too long time
if ((iTrustedState & CONADefinitions.CONAPI_PAIR_DEVICE) != 0)
{
m_pDevices[iSelIndex].iStatus = ((m_pDevices[iSelIndex].iStatus & CONADefinitions.CONAPI_DEVICE_PCSUITE_TRUSTED) | CONADefinitions.CONAPI_DEVICE_PAIRED);
}
else if ((iTrustedState & CONADefinitions.CONAPI_UNPAIR_DEVICE) != 0)
{
m_pDevices[iSelIndex].iStatus = ((m_pDevices[iSelIndex].iStatus & CONADefinitions.CONAPI_DEVICE_PCSUITE_TRUSTED) | CONADefinitions.CONAPI_DEVICE_UNPAIRED);
}
if ((iTrustedState & CONADefinitions.CONAPI_SET_PCSUITE_TRUSTED) != 0)
{
m_pDevices[iSelIndex].iStatus = (m_pDevices[iSelIndex].iStatus | CONADefinitions.CONAPI_DEVICE_PCSUITE_TRUSTED);
}
else if ((iTrustedState & CONADefinitions.CONAPI_SET_PCSUITE_UNTRUSTED) != 0)
{
m_pDevices[iSelIndex].iStatus = (int)((uint)m_pDevices[iSelIndex].iStatus & (0xFFFFFFFF ^ (uint)CONADefinitions.CONAPI_DEVICE_PCSUITE_TRUSTED));
}
BTRefreshPhoneList(false);
}
else
{
PCCAPIUtils.ShowErrorMessage("CONAChangeDeviceTrustedState failed.", iRet);
}
}
else
{
MessageBox.Show("Please select a phone from the list.");
}
}
return iRet;
}
private void FRM_BTPairing_Closed(object sender, System.EventArgs e)
{
if (m_hDMHandle != 0)
{
// Close device management handle
int iRet = CONADeviceManagement.CONACloseDM(m_hDMHandle);
if (iRet != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONACloseDM", iRet);
}
}
//===================================================================
// BTN_Search_Click
//
// User has clicked Refresh List button
//===================================================================
private void BTN_Search_Click(object sender, System.EventArgs e)
{
BTRefreshPhoneList(true);
}
//===================================================================
// BTN_Pair_Click
//
// User has clicked Pair button
//===================================================================
private void BTN_Pair_Click(object sender, System.EventArgs e)
{
FRM_Password PasswordDlg = new FRM_Password();
if (PasswordDlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
ChangeTrustedState(CONADefinitions.CONAPI_PAIR_DEVICE, PasswordDlg.TXB_Password.Text);
}
}
//===================================================================
// BTN_Unpair_Click
//
// User has clicked Unpair button
//===================================================================
private void BTN_Unpair_Click(object sender, System.EventArgs e)
{
ChangeTrustedState(CONADefinitions.CONAPI_UNPAIR_DEVICE, null);
}
//===================================================================
// BTN_SetTrusted_Click
//
// User has clicked Set Trusted button
//===================================================================
private void BTN_SetTrusted_Click(object sender, System.EventArgs e)
{
ChangeTrustedState(CONADefinitions.CONAPI_SET_PCSUITE_TRUSTED, null);
}
//===================================================================
// BTN_SetUntrusted_Click
//
// User has clicked Set Untrusted button
//===================================================================
private void BTN_SetUnTrusted_Click(object sender, System.EventArgs e)
{
ChangeTrustedState(CONADefinitions.CONAPI_SET_PCSUITE_UNTRUSTED, null);
}
//===================================================================
// BTN_Close_Click
//
// User has clicked Close button
//===================================================================
private void BTN_Close_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -