/// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BLEATTClientProcedureCompletedEvent(object sender, Bluegiga.BLE.Events.ATTClient.ProcedureCompletedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(delegate
            {
                MessageWriter.LogWrite("ble_evt_attclient_procedure_completed: ", string.Format("connection={0}, result={1}, chrhandle={2}",
                                                                                                e.connection,
                                                                                                e.result,
                                                                                                e.chrhandle));

                BLEPeripheral peripheral;
                if (!connectedDevices.TryGetConnection(e.connection, out peripheral))
                {
                    MessageWriter.LogWrite("Unable to find connection " + e.connection);
                    return;
                }

                BLEPerformNextTask(peripheral);

                if (peripheral.Characteristics.ContainsKey("LPM"))
                {
                    if (e.chrhandle == peripheral.Characteristics["LPM"].ValueAttribute.Handle)
                    {
                        // Check sleep mode
                        cmd = bglib.BLECommandATTClientReadByHandle(e.connection, peripheral.Characteristics["LPM"].ValueAttribute.Handle);
                        MessageWriter.LogWrite("ble_cmd_att_client_read_by_handle: ", string.Format("connection={0}, handle={1}",
                                                                                                    e.connection, peripheral.Characteristics["LPM"].ValueAttribute.Handle));
                        bglib.SendCommand(SelectedPort, cmd);
                    }
                }
            });
        }
Beispiel #2
0
        private void btnServoRead_Click(object sender, EventArgs e)
        {
            byte[] cmd;

            cmd = bglib.BLECommandATTClientReadByHandle(adrBLE112, 11);
            bglib.SendCommand(cmd);
        }
Beispiel #3
0
 private void readButton_Click(object sender, EventArgs e)
 {
     Byte[] cmd;
     app_state = STATE_READ_ATTR_VAL;
     // send cmd
     cmd = bglib.BLECommandATTClientReadByHandle(0, (ushort)attrReadHandle);
     // display bytes send
     ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=>   Length:  ({0}) Data:   [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });
     bglib.SendCommand(serialAPI, cmd);
 }
Beispiel #4
0
        private void OpenGATTEditor(object obj)
        {
            // Create window
            MessageWriter.LogWrite("Opening attribute editor...");
            Window editor = new EditGATTValueWindow();

            editor.DataContext = this;

            // Check current value
            byte[] cmd = bglib.BLECommandATTClientReadByHandle(_parentPeripheral.Connection, _handle);
            MessageWriter.LogWrite("ble_cmd_att_client_read_by_handle: ", string.Format("connection={0}, handle={1}",
                                                                                        _parentPeripheral.Connection, _handle));
            MessageWriter.BLEWrite(cmd);

            editor.Show();
        }
Beispiel #5
0
        private async void readBLEPeriodically()
        {
            bool rsp;

            while (true)
            {
                await Task.Delay(1000);

                if (this.bleSerial != null && this.SerialInitialized)
                {
                    try
                    {
                        do
                        {
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandSystemReset(0));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        } while (!rsp);

                        if (rsp)
                        {
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandSystemHello());

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        if (rsp)
                        {
                            byte[] bleAddress = new byte[] { SENSORTAG_ADDRESS_0, SENSORTAG_ADDRESS_1, SENSORTAG_ADDRESS_2, SENSORTAG_ADDRESS_3, SENSORTAG_ADDRESS_4, SENSORTAG_ADDRESS_5 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandGAPConnectDirect(bleAddress, 0, BLECONNINTERVALMIN, BLECINNINTERVANMAX, BLECONNTIMEOUT, BLECONNSLAVELATENCY));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Config temperature sensor period
                        if (rsp)
                        {
                            byte[] temperaturePeriod = new byte[] { 100 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLETEMPPERIODHNDL, temperaturePeriod));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Config light sensor period
                        if (rsp)
                        {
                            byte[] lightPeriod = new byte[] { 100 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLELIGHTPERIODHNDL, lightPeriod));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Start temperature measurement
                        if (rsp)
                        {
                            byte[] temperatureConfig = new byte[] { 0x01 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLETEMPCONFIGHNDL, temperatureConfig));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Start light measurement
                        if (rsp)
                        {
                            byte[] lightConfig = new byte[] { 0x01 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLELIGHTCONFIGHNDL, lightConfig));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        while (rsp)
                        {
                            await Task.Delay(1000);

                            // Read temperature data
                            if (rsp)
                            {
                                await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientReadByHandle(this.bleConnectionHndl, BLETEMPDATAHNDL));

                                rsp = await this.bleSemaphore.WaitAsync(2000);
                            }
                            else
                            {
                                this.ErrorMessage.Text = "Semaphore timeout";
                            }

                            // Read light data
                            if (rsp)
                            {
                                await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientReadByHandle(this.bleConnectionHndl, BLELIGHTDATAHNDL));

                                rsp = await this.bleSemaphore.WaitAsync(2000);
                            }
                            else
                            {
                                this.ErrorMessage.Text = "Semaphore timeout";
                            }
                        }

                        await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandConnectionDisconnect(this.bleConnectionHndl));
                    }
                    catch (Exception ex)
                    {
                        this.ErrorValue.Text   = "BGLib error!";
                        this.ErrorMessage.Text = ex.Message;
                    }
                }
            }
        }
Beispiel #6
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;
                }
            }
        }