示例#1
0
        public bool ConnectUEI(string address)
        {
            int    ndx     = addressList.IndexOf(address);
            string addrStr = "0x";

            for (int i = 0; i < 18; i += 3)
            {
                addrStr += address.Substring(i, 2);
            }
            long addrVal = Convert.ToInt64(addrStr, 16);

            Client.Address = addrVal;
            stage          = 1;
            if (Radio != null)
            {
                int Res = Client.Connect(Radio);
                FEvent.WaitOne(2000);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    return(false);
                }
                else
                {
                    Res  = Radio.GetRemoteRssi(addrVal, out sbyte rssiOut);
                    rssi = Res == wclErrors.WCL_E_SUCCESS ? rssiOut: 1;
                }
                stage = 2;
                return(true);
            }
            return(false);
        }
示例#2
0
        void FManager_OnDiscoveringCompleted(object Sender, wclBluetoothRadio Radio, int Error)
        {
            if (Error != wclErrors.WCL_E_SUCCESS)
            {
                Trace("Discovering completed with error: 0x" + Error.ToString("X8"));
                CloseBluetoothManager();
            }
            else
            {
                if (FDevices.Count == 0)
                {
                    Trace("No one BLE device was found");
                    CloseBluetoothManager();
                }
                else
                {
                    Trace("Discovering completed with success");
                    Trace("Use firs found device to connect: " + FDevices[0].ToString("X12"));

                    FClient.Address = FDevices[0];
                    Int32 Res = FClient.Connect(Radio);
                    if (Res != wclErrors.WCL_E_SUCCESS)
                    {
                        Trace("Connect attemp failed; 0x" + Res.ToString("X8"));
                        CloseBluetoothManager();
                    }
                }
            }
            FDevices = null;
        }
示例#3
0
文件: wclWeDoHub.cs 项目: leuher/WeDo
        /// <summary> Connects to a selected WeDo Hub. </summary>
        /// <param name="Radio"> The <see cref="wclBluetoothRadio" /> object that should be used
        ///   for executing Bluetooth LE connection. </param>
        /// <param name="Address"> The WeDo Hub MAC address. </param>
        /// <returns> If the method completed with success the returning value is
        ///   <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is
        ///   one of the Bluetooth Framework error code. </returns>
        /// <seealso cref="wclBluetoothRadio" />
        public Int32 Connect(wclBluetoothRadio Radio, Int64 Address)
        {
            if (Radio == null || Address == 0)
            {
                return(wclErrors.WCL_E_INVALID_ARGUMENT);
            }

            // This check prevent exception raising when you try to change
            // connection MAC address on already connected client.
            if (FClient.State != wclClientState.csDisconnected)
            {
                return(wclConnectionErrors.WCL_E_CONNECTION_ACTIVE);
            }

            FClient.Address = Address;
            return(FClient.Connect(Radio));
        }
示例#4
0
        public async Task <bool> ConnectAsync(object macAdd)
        {
            if (_readerState != READERSTATE.DISCONNECT)
            {
                return(false); // reader can not reconnect
            }
            //            wclBluetoothRadio btdevice = (wclBluetoothRadio)device;

            //Client = new wclGattClient();
            Int32 Res;

            Client.Address = (long)macAdd;
            Res            = Client.Connect(DeviceFinder.Radio);
            if (Res != wclErrors.WCL_E_SUCCESS)
            {
                CSLibrary.Debug.WriteLine("Error: 0x" + Res.ToString("X8"));
                return(false);
            }

            return(true);
        }
示例#5
0
        private void bleConnect_Click(object sender, EventArgs e)
        {
            if (lvDevices.SelectedItems.Count == 0)
            {
                return;
            }

            var item = lvDevices.SelectedItems[0];

            _client.Address = Convert.ToInt64(item.Text, 16);
            var radio      = _devices[item.Text];
            var resConnect = _client.Connect(radio);

            if (resConnect != wclErrors.WCL_E_SUCCESS)
            {
                MessageBox.Show("连接失败");
                return;
            }

            btSetTimeIcon.Enabled = true;
            btnGetRecord.Enabled  = true;
        }
示例#6
0
        private void FManager_AfterOpen(object sender, EventArgs e)
        {
            lbLog.Items.Add("Bluetooth Manager has been opened");

            if (FManager.Count == 0)
            {
                lbLog.Items.Add("No Bluetooth radio found");
                FManager.Close();
            }
            else
            {
                wclBluetoothRadio Radio = FManager[0];
                lbLog.Items.Add("Found " + Radio.ApiName + " Bluetooth radio");

                Int64[] Devices;
                Int32   Res = Radio.EnumPairedDevices(out Devices);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    lbLog.Items.Add("Enumerate paired devices failed: 0x" + Res.ToString("X8"));
                }
                else
                {
                    if (Devices == null || Devices.Length == 0)
                    {
                        lbLog.Items.Add("Not paired devices found");
                        FManager.Close();
                    }
                    else
                    {
                        Int64 Address = 0;
                        foreach (Int64 a in Devices)
                        {
                            wclBluetoothDeviceType Type;
                            Res = Radio.GetRemoteDeviceType(a, out Type);
                            if (Res == wclErrors.WCL_E_SUCCESS && Type == wclBluetoothDeviceType.dtBle)
                            {
                                Address = a;
                                break;
                            }
                        }

                        if (Address == 0)
                        {
                            lbLog.Items.Add("No BLE device found");
                            FManager.Close();
                        }
                        else
                        {
                            FClient.Address = Address;
                            Res             = FClient.Connect(Radio);
                            if (Res != wclErrors.WCL_E_SUCCESS)
                            {
                                lbLog.Items.Add("Connect failed; 0x" + Res.ToString("X8"));
                            }
                        }
                    }
                }

                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    FManager.Close();
                }
            }
        }