📄 btpairingdlg.vb
字号:
' Update progress bar
m_pProgressDlg.SetProgress(iState)
End If
If m_pProgressDlg.IsCancelled Then
' If user has clicked Cancel, return ECONA_CANCELLED
BTPairingNotifyCallback = ECONA_CANCELLED
End If
Application.DoEvents()
End Function
'===================================================================
' FRM_BTPairing_Load
'
' Initialization of FRM_BTPairing form
'
'===================================================================
Private Sub FRM_BTPairing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
BTRefreshPhoneList(True)
End Sub
'===================================================================
' ClearPhoneList
'
' Removes all items from the phone list, and frees memory
' allocated for each list item data
'===================================================================
Private Sub ClearPhoneList()
LVW_PhoneList.Items.Clear()
ReDim m_pDevices(0)
End Sub
'===================================================================
' 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 Sub BTRefreshPhoneList(ByVal bSearchDevices As Boolean)
' 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.
Const iMaxSearchTime As Integer = 240
Dim iRet As Integer = ECONA_UNKNOWN_ERROR
Dim iDeviceCount As Integer = 0
Dim i As Integer = 0
Dim pInfo As IntPtr
Dim Info() As CONAPI_CONNECTION_INFO
If m_hDMHandle Then
If (bSearchDevices) Then
m_pfnSearchNotify = AddressOf BTPairingNotifyCallback
iRet = CONASearchDevices(m_hDMHandle, API_MEDIA_BLUETOOTH Or CONAPI_GET_ALL_PHONES, iMaxSearchTime, m_pfnSearchNotify, iDeviceCount, pInfo)
If Not m_pProgressDlg Is Nothing Then
' progress is at the end. Hide progress bar
m_pProgressDlg.Hide()
m_pProgressDlg = Nothing
End If
ClearPhoneList()
ReDim m_pDevices(iDeviceCount)
ReDim Info(iDeviceCount)
For i = 0 To iDeviceCount - 1
' Calculate beginning of CONAPI_CONNECTION_INFO structure of item 'i'
Dim iPtr As Int64 = pInfo.ToInt64 + i * Marshal.SizeOf(GetType(CONAPI_CONNECTION_INFO))
' Convert integer to pointer
Dim ptr As IntPtr = IntPtr.op_Explicit(iPtr)
' Copy data from buffer
Info(i) = Marshal.PtrToStructure(ptr, GetType(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)
Next
If iRet = ECONA_CANCELLED Then
LVW_PhoneList.Items.Add("Search cancelled")
ElseIf iRet = ECONA_FAILED_TIMEOUT Then
LVW_PhoneList.Items.Add("Timeout reached")
ElseIf iRet <> CONA_OK Then
ShowErrorMessage("CONASearchDevices failed.", iRet)
LVW_PhoneList.Items.Add("Search failed")
End If
If iDeviceCount > 0 Then
iRet = CONAFreeConnectionInfoStructures(iDeviceCount, pInfo)
If iRet <> CONA_OK Then
ShowErrorMessage("CONAFreeConnectionInfoStructures failed.", iRet)
End If
End If
End If
' update device status to the list
iDeviceCount = LVW_PhoneList.Items.Count()
For i = 0 To iDeviceCount - 1
If LVW_PhoneList.Items(i).SubItems.Count <= 1 Then
LVW_PhoneList.Items(i).SubItems.Add(TrustedInfo2String(m_pDevices(i).iStatus))
Else
LVW_PhoneList.Items(i).SubItems(1).Text = TrustedInfo2String(m_pDevices(i).iStatus)
End If
Next
End If
End Sub
'===================================================================
' 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 Function ChangeTrustedState(ByVal iTrustedState As Integer, ByVal strPassword As String) As Integer
Dim iRet As Integer = ECONA_UNKNOWN_ERROR
If m_hDMHandle Then
' get selected device from the list
Dim iSelIndex As Integer = -1
If LVW_PhoneList.SelectedIndices().Count() > 0 Then
iSelIndex = LVW_PhoneList.SelectedItems(0).Index()
End If
If iSelIndex >= 0 Then
iRet = CONAChangeDeviceTrustedState(m_hDMHandle, iTrustedState, m_pDevices(iSelIndex).strAddress, strPassword, vbNullString)
If iRet = CONA_OK Then
' change the device status to the list according to return value
' re-searching all devices would take too long time
If iTrustedState And CONAPI_PAIR_DEVICE Then
m_pDevices(iSelIndex).iStatus = ((m_pDevices(iSelIndex).iStatus And CONAPI_DEVICE_PCSUITE_TRUSTED) Or CONAPI_DEVICE_PAIRED)
ElseIf iTrustedState And CONAPI_UNPAIR_DEVICE Then
m_pDevices(iSelIndex).iStatus = ((m_pDevices(iSelIndex).iStatus And CONAPI_DEVICE_PCSUITE_TRUSTED) Or CONAPI_DEVICE_UNPAIRED)
End If
If iTrustedState And CONAPI_SET_PCSUITE_TRUSTED Then
m_pDevices(iSelIndex).iStatus = (m_pDevices(iSelIndex).iStatus Or CONAPI_DEVICE_PCSUITE_TRUSTED)
ElseIf iTrustedState And CONAPI_SET_PCSUITE_UNTRUSTED Then
m_pDevices(iSelIndex).iStatus = (m_pDevices(iSelIndex).iStatus And Not CONAPI_DEVICE_PCSUITE_TRUSTED)
End If
BTRefreshPhoneList(False)
Else
ShowErrorMessage("CONAChangeDeviceTrustedState failed.", iRet)
End If
Else
MsgBox("Please select a phone from the list.")
End If
End If
ChangeTrustedState = iRet
End Function
Private Sub FRM_BTPairing_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
If m_hDMHandle <> 0 Then
' Close device management handle
Dim iRet As Integer = CONACloseDM(m_hDMHandle)
If iRet <> CONA_OK Then ShowErrorMessage("CONACloseDM", iRet)
End If
End Sub
'===================================================================
' BTN_Search_Click
'
' User has clicked Refresh List button
'===================================================================
Private Sub BTN_Search_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Search.Click
BTRefreshPhoneList(True)
End Sub
'===================================================================
' BTN_Pair_Click
'
' User has clicked Pair button
'===================================================================
Private Sub BTN_Pair_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Pair.Click
Dim PasswordDlg As New FRM_Password
If PasswordDlg.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
ChangeTrustedState(CONAPI_PAIR_DEVICE, PasswordDlg.TXB_Password.Text)
End If
End Sub
'===================================================================
' BTN_Unpair_Click
'
' User has clicked Unpair button
'===================================================================
Private Sub BTN_Unpair_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Unpair.Click
ChangeTrustedState(CONAPI_UNPAIR_DEVICE, vbNullString)
End Sub
'===================================================================
' BTN_SetTrusted_Click
'
' User has clicked Set Trusted button
'===================================================================
Private Sub BTN_SetTrusted_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_SetTrusted.Click
ChangeTrustedState(CONAPI_SET_PCSUITE_TRUSTED, vbNullString)
End Sub
'===================================================================
' BTN_SetUntrusted_Click
'
' User has clicked Set Untrusted button
'===================================================================
Private Sub BTN_SetUnTrusted_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_SetUnTrusted.Click
ChangeTrustedState(CONAPI_SET_PCSUITE_UNTRUSTED, vbNullString)
End Sub
'===================================================================
' BTN_Close_Click
'
' User has clicked Close button
'===================================================================
Private Sub BTN_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Close.Click
Me.Close()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -