示例#1
0
        private void buttonPairRequest_Click(object sender, EventArgs e)
        {
            labelResult.ForeColor = Color.Orange;
            labelResult.Text      = "Try to pairing ...";
            UpdateBTDeviceDetails("Pairing for Bluetooth devices...");
            // 建立委派物件(C# 3.0 only)
            ActionSecurityWithPin actionPair = (BluetoothAddress device, string passcode) => BluetoothSecurity.PairRequest(device, passcode);

            DoSecurityActionWithPinInput("Pair Request", actionPair);
        }
示例#2
0
        private void DoSecurityActionWithPinInput(string name, ActionSecurityWithPin action)
        {
            try
            {
                SelectBluetoothDeviceDialog mDeviceDialog = new SelectBluetoothDeviceDialog(); // 選擇藍牙裝置清單
                mDeviceDialog.ShowRemembered    = true;                                        // 顯示已經記住的藍牙設備
                mDeviceDialog.ShowAuthenticated = true;                                        // 顯示認證過的藍牙設備
                mDeviceDialog.ShowUnknown       = true;                                        // 顯示位置藍牙設備
                if (mDeviceDialog.ShowDialog() == DialogResult.OK)
                {
                    selectedDevice = (BluetoothDeviceInfo)mDeviceDialog.SelectedDevice;
                    UpdateBTDeviceDetails("\r\nDevice Name : " + mDeviceDialog.SelectedDevice.DeviceName.ToString());
                    UpdateBTDeviceDetails("Address : " + mDeviceDialog.SelectedDevice.DeviceAddress.ToString());// 獲取選擇的遠程藍牙地址

                    bool result = action(selectedDevice.DeviceAddress, "");
                    UpdateBTDeviceDetails("Bluetooth Pair Request, Result : " + result);

                    if (result)
                    {
                        UpdateFindDeviceResult("Device connected.");
                        buttonPairRequest.Enabled = false;
                        buttonAudioTest.Enabled   = true;
                    }
                    else
                    {
                        SetFailEvent();
                    }
                }
            }
            catch (SocketException sex)
            {
                UpdateBTDeviceDetails("Connect failed : " + sex.Message + ", Code :  " + sex.SocketErrorCode.ToString("D"));
                SetFailEvent();
            }
            catch (Exception ex)
            {
                UpdateBTDeviceDetails(ex.Message);
                SetFailEvent();
            }
        }