示例#1
0
        public bool BleHostConnect()
        {
            CsrBleDll.CsrBleHostStopLESearch();

            if (DevInfoSelected == null)
            {
                Error("BleHostConnect No Device Selected!");
                return(false);
            }

            if (CurDevInfo != null)
            {
                BleHostDisconnect();
            }

            CurDevInfo      = DevInfoSelected;
            DevInfoSelected = null;

            if (CurDevInfo != null)
            {
                string addr = CurDevInfo.Address;
                var    dev  = bleSearchResults.FindAll(r => r.deviceAddress.ToString() == addr);
                if (dev.Count == 0)
                {
                    Error(string.Format("BleHostConnect Find Device Error at {0}", addr));
                    return(false);
                }


                if (CsrBleDll.CsrBleHostSetConnectionParams((ushort)CsrBleDefine.CSR_BLE_PARAMETER_IGNORE,
                                                            (ushort)CsrBleDefine.CSR_BLE_PARAMETER_IGNORE,
                                                            CsrBleDefine.MIN_CONN_INTERVAL,
                                                            CsrBleDefine.MAX_CONN_INTERVAL,
                                                            CsrBleDefine.SLAVE_LATENCY,
                                                            CsrBleDefine.LINK_TIMEOUT))
                {
                    //set parameters is success
                    if (CsrBleDll.CsrBleHostConnect(dev[0].deviceAddress))
                    {
                        if (_dbDiscoverEvent.WaitOne(8000))
                        {
                            Trace(string.Format("CsrBleHostConnect {0} Success!", dev[0].deviceAddress));
                            OnUpdateBleConnectEvent("蓝牙连接成功:" + dev[0].deviceAddress);
                            return(true);
                        }
                    }
                }
                Trace(string.Format("CsrBleHostConnect {0} Fail!", dev[0].deviceAddress));
                OnUpdateBleConnectEvent("蓝牙连接失败:" + dev[0].deviceAddress);
            }
            return(false);
        }
示例#2
0
        public async Task <bool> BleHostConnect(BleGattDevice gattDevice)
        {
            if (!IsBleHosted)
            {
                Error("[CSR]:BleHostInitialize Fail!");
                return(false);
            }

            CsrBleDll.CsrBleHostStopLESearch();
            if (GattConnectDevices.Exists(d => d.Address == gattDevice.Address))
            {
                Warning($"[CSR]:BleHostConnect GattDevice {gattDevice.Address} Already Connected!");
                return(true);
            }

            return(await Task.Run(() =>
            {
                var csrDevice = _csrSearchResults.FindAll(r => r.deviceAddress.ToString() == gattDevice.Address);
                if (csrDevice.Count == 0)
                {
                    Error($"[CSR]:BleHostConnect Find GattDevice Error at {gattDevice.Address}");
                    return false;
                }

                GattSearchDevices.RemoveAll(d => d.Address == gattDevice.Address);

                GattConnectDevices.RemoveAll(d => d.Address == gattDevice.Address);
                GattConnectDevices.Add(gattDevice);
                Debug("[CSR]:GattConnectDevices Add Connecting Device!");

                if (CsrBleDll.CsrBleHostSetConnectionParams((ushort)CsrBleDefine.CSR_BLE_PARAMETER_IGNORE,
                                                            (ushort)CsrBleDefine.CSR_BLE_PARAMETER_IGNORE,
                                                            CsrBleDefine.MIN_CONN_INTERVAL, CsrBleDefine.MAX_CONN_INTERVAL,
                                                            CsrBleDefine.SLAVE_LATENCY, CsrBleDefine.LINK_TIMEOUT))
                {
                    //set parameters is success
                    if (CsrBleDll.CsrBleHostConnect(csrDevice[0].deviceAddress))
                    {
                        Debug("[CSR]:CsrBleHostConnect Start!");
                        if (_connectEvent.WaitOne(5000))
                        {
                            Debug($"[CSR]:CsrBleHostConnect {csrDevice[0].deviceAddress} Success!");
                            if (_databaseEvent.WaitOne(5000))
                            {
                                gattDevice.OnDeviceConnectEvent(true);
                                Debug($"[CSR]:DatabaseDiscover {csrDevice[0].deviceAddress} Success!");
                                OnDeviceConnectEvent("蓝牙连接成功:" + csrDevice[0].deviceAddress);
                                return true;
                            }
                        }
                    }
                }

                GattConnectDevices.Remove(gattDevice);
                gattDevice.OnDeviceConnectEvent(false);
                Debug("[CSR]:GattConnectDevices Remove Connect Fail Device!");

                Debug($"[CSR]:CsrBleHostConnect {csrDevice[0].deviceAddress} Fail!");
                OnDeviceConnectEvent("蓝牙连接失败:" + csrDevice[0].deviceAddress);
                return false;
            }));
        }