Пример #1
0
        private void ScanThread()
        {
            DateTime mTimStart = DateTime.Now;

            while (true)
            {
                switch (c_BleDev.State)
                {
                case GhpBle.ACTTION_IDLE:
                    Thread.Sleep(50);
                    break;

#if false
                case GhpBle.ACTIION_ATTR_PAIR_CHECK:
                {
                    mTimStart = DateTime.Now;
                    while (c_BleDev.Busy)
                    {
                        TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart);
                        if (mTimDif.Seconds > 3)
                        {
                            break;
                        }
                    }
                    c_BleDev.Busy = true;
                    byte[] cmd = bglib.BLECommandATTClientReadByType(c_BleDev.ConnHandle, 0x0001, 0xFFFF, new byte[] { 0x03, 0x28 });
                    bglib.SendCommand(comDev, cmd);

                    mTimStart = DateTime.Now;
                    while (c_BleDev.Busy)
                    {
                        TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart);
                        if (mTimDif.Seconds > 3)
                        {
                            break;
                        }
                    }
                    if (c_BleDev.NeedPair)
                    {
                        if (c_BleDev.Paired == false)
                        {
                            c_BleDev.Busy = true;
                            byte[] cmd2 = bglib.BLECommandSMEncryptStart(c_BleDev.ConnHandle, 1);
                            bglib.SendCommand(comDev, cmd2);

                            mTimStart = DateTime.Now;
                            while (c_BleDev.Paired == false)
                            {
                                TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart);
                                if (mTimDif.Seconds > 3)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            c_BleDev.State = GhpBle.ACTIONN_ATTR_PAIR_DONE;
                        }
                    }
                    else
                    {
                        c_BleDev.State = GhpBle.ACTIONN_ATTR_PAIR_DONE;
                    }
                }
                break;

                case GhpBle.ACTIONN_ATTR_PAIR_DONE:
                    if (c_BleDev.NeedPair && c_BleDev.Paired)
                    {
                        c_BleDev.State = GhpBle.ACTTION_SCAN_PRIMSRV;
                    }
                    break;
#endif
                case GhpBle.ACTTION_SCAN_PRIMSRV:
                {
                    c_BleDev.Busy = true;
                    Byte[] cmd = bglib.BLECommandATTClientReadByGroupType(c_BleDev.ConnHandle, 0x0001, 0xFFFF, new Byte[] { 0x00, 0x28 });
                    bglib.SendCommand(comDev, cmd);

                    mTimStart = DateTime.Now;
                    while (c_BleDev.Busy)
                    {
                        TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart);
                        if (mTimDif.Seconds > 10)
                        {
                            break;
                        }
                    }
                    c_BleDev.State = GhpBle.ACTTION_SCAN_PRIMSRV_DONE;
                }
                break;

                case GhpBle.ACTTION_SCAN_PRIMSRV_DONE:
                    break;

                case GhpBle.ACTTION_SCAN_ATTRIB:
                    if (c_BleDev.CurrentPrimSrv != null)
                    {
                        if (c_BleDev.CurrentPrimSrv.AttScanDone == false)
                        {
                            //
                            //scan all attribute first.
                            //
                            c_BleDev.Busy = true;
                            Byte[] cmd = bglib.BLECommandATTClientFindInformation(c_BleDev.ConnHandle, c_BleDev.CurrentPrimSrv.Start, c_BleDev.CurrentPrimSrv.End);
                            bglib.SendCommand(comDev, cmd);

                            mTimStart = DateTime.Now;
                            while (c_BleDev.Busy)
                            {
                                TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart);
                                if (mTimDif.Seconds > 20)
                                {
                                    break;
                                }
                            }

                            //
                            //Now read user description of each attribute;
                            //
                            foreach (CAttribute attr in c_BleDev.CurrentPrimSrv.AttrList)
                            {
                                c_BleDev.Busy        = true;
                                c_BleDev.AttReadDone = false;
                                cmd = bglib.BLECommandATTClientReadByHandle(c_BleDev.ConnHandle, attr.UserDescHandle);
                                bglib.SendCommand(comDev, cmd);
                                while (c_BleDev.Busy && c_BleDev.AttReadDone == false)
                                {
                                }
                                if (c_BleDev.AttReadDone == true)
                                {
                                    attr.AttName = Encoding.UTF8.GetString(c_BleDev.AttReadValue);
                                }
                            }
                            c_BleDev.CurrentPrimSrv.AttScanDone = true;
                        }
                    }
                    c_BleDev.State = GhpBle.ACTTION_SCAN_ATTRIB_DONE;
                    break;

                case GhpBle.ACTTION_SCAN_ATTRIB_DONE:
                    break;

                case GhpBle.ACTTION_ATTR_READ:
                    while (c_BleDev.Busy == false && c_BleDev.AttReadDone == false)
                    {
                        ;
                    }
                    c_BleDev.State = GhpBle.ACTIONN_ATTR_READ_DONE;
                    break;

                case GhpBle.ACTIONN_ATTR_READ_DONE:
                    break;

                case GhpBle.ACTTION_ATTR_WRITE:
                    break;

                default:
                    break;
                }
            }
        }