Пример #1
0
        private async void buttonConnect_Click(object sender, EventArgs e)
        {
            if (listBoxDeviceSearch.SelectedIndex < 0 || string.IsNullOrEmpty(SelectDeviceString))
            {
                MessageBox.Show("未选择设备!");
                return;
            }


            var dev = BleGattDevice.String2Device(SelectDeviceString);

            if (dev != null)
            {
                if (await CsrBleControl.Only().BleHostConnect(dev))
                {
                    OnDeviceConnectEvent(dev);
                    OnUpdateDeviceConnectResult(dev);
                }
            }
            else
            {
                OnDeviceConnectEvent(null);
                OnUpdateDeviceConnectResult(null);
            }

            listBoxDeviceSearch.SelectedItem = null;
            SelectDeviceString = string.Empty;
            SelectAddr         = string.Empty;
        }
        private async void buttonConnect_Click(object sender, EventArgs e)
        {
            if (listBoxDeviceSearch.SelectedIndex < 0 || string.IsNullOrEmpty(_selectSearchDeviceString))
            {
                MessageBox.Show("未选中设备!");
                return;
            }

            var dev = BleGattDevice.String2Device(_selectSearchDeviceString);

            if (dev != null)
            {
                if (await CsrBleControl.Only().BleHostConnect(dev))
                {
                    OnDeviceConnectResults(CsrBleControl.Only().GattConnectDevices);
                    OnDeviceSearchResults(CsrBleControl.Only().GattSearchDevices);

                    _selectConnectDeviceString        = string.Empty;
                    listBoxDeviceConnect.SelectedItem = null;
                }
                else
                {
                    MessageBox.Show("设备连接异常!");
                }
            }
            else
            {
                MessageBox.Show("设备连接异常!");
                OnDeviceConnectResults(CsrBleControl.Only().GattConnectDevices);
            }


            _selectSearchDeviceString = string.Empty;
            _selectSearchAddress      = string.Empty;
        }