Esempio n. 1
0
        private void btScanStart_Click(object sender, EventArgs e)
        {
            if (comDev.IsOpen == true)
            {
                if (btScanStart.Text == "StopScan")
                {
                    // update state
                    btScanStart.Text  = "StartScan";
                    btScanStart.Image = Properties.Resources.BMP_GRAY;

                    // stop scanning if scanning
                    bglib.SendCommand(comDev, bglib.BLECommandGAPEndProcedure());
                }
                else
                {
                    listScanDev.Items.Clear();

                    // disconnect if connected
                    bglib.SendCommand(comDev, bglib.BLECommandConnectionDisconnect(0));

                    btScanStart.Text  = "StopScan";
                    btScanStart.Image = Properties.Resources.BMP_GREEN;
                    bglib.SendCommand(comDev, bglib.BLECommandGAPDiscover(1));//gap_discover_generic:
                }
            }
            else
            {
                MessageBox.Show("请先打开串口", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 2
0
        private void btnReset_Click(object sender, EventArgs e)
        {
            // stop everything we're doing, if possible
            Byte[] cmd;

            // disconnect if connected
            cmd = bglib.BLECommandConnectionDisconnect(0);
            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });
            bglib.SendCommand(serialAPI, cmd);
            //while (bglib.IsBusy()) ;

            // stop scanning if scanning
            cmd = bglib.BLECommandGAPEndProcedure();
            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });
            bglib.SendCommand(serialAPI, cmd);
            //while (bglib.IsBusy()) ;

            // stop advertising if advertising
            cmd = bglib.BLECommandGAPSetMode(0, 0);
            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });
            bglib.SendCommand(serialAPI, cmd);
            //while (bglib.IsBusy()) ;

            // enable "GO" button to allow them to start again
            btnGo.Enabled = true;

            // update state
            app_state = STATE_STANDBY;
        }
Esempio n. 3
0
        public void Reset()
        {
            // stop everything we're doing, if possible
            Byte[] cmd;

            // disconnect if connected
            //CJB IM not sure if the 0 here is the connection index, or the connection address. Assume since it is 0 it is connection 0
            //If we have multiples might have to disconnect them all
            cmd = bglib.BLECommandConnectionDisconnect(0);
            // DEBUG: display bytes read
            Console.WriteLine(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine);
            bglib.SendCommand(serialAPI, cmd);
            //while (bglib.IsBusy()) ;

            // stop scanning if scanning
            cmd = bglib.BLECommandGAPEndProcedure();
            // DEBUG: display bytes read
            Console.WriteLine(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine);
            bglib.SendCommand(serialAPI, cmd);
            //while (bglib.IsBusy()) ;

            // stop advertising if advertising
            cmd = bglib.BLECommandGAPSetMode(0, 0);
            // DEBUG: display bytes read
            Console.WriteLine(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine);
            bglib.SendCommand(serialAPI, cmd);
        }
 public override void Disconnect()
 {
     if (device?.ConnectionHandle != null && (port?.IsOpen) != null)
     {
         bluetooth.SendCommand(port, bluetooth.BLECommandConnectionDisconnect((byte)device.ConnectionHandle));
     }
 }
Esempio n. 5
0
        internal void Disconnect()
        {
            if (GetState() == STATE_DISCONNECT)
            {
                return;
            }

            Byte[] cmd = bglib.BLECommandConnectionDisconnect(connection_handle);
            bglib.SendCommand(serialAPI, cmd);
        }
Esempio n. 6
0
        public void Disconnect()
        {
            if (LowPowerMode == LPM.Disabled)
            {
                MessageWriter.LogWrite(Name + " entering LPM...");
                LPMEnter();
            }

            // Disconnect
            byte[] cmd = bglib.BLECommandConnectionDisconnect(Connection);
            MessageWriter.LogWrite("ble_cmd_connection_disconnect: ", string.Format("connection={0}", Connection));
            MessageWriter.BLEWrite(cmd);

            // Stop tracking uptime
            _uptimeDispatcher.IsEnabled = false;
            if (_uptime.IsRunning == true)
            {
                _uptime.Reset();
                NotifyPropertyChanged("Uptime");
            }
        }
Esempio n. 7
0
        //-------------------------------------------------------------------------------------------------------------
        private void btnReset_Click(object sender, EventArgs e)
        {
            // Stop everything we're doing, if possible.

            // Declare an array to hold the command bytes.
            Byte[] cmd;

            // Disconnect if connected
            cmd = bglib.BLECommandConnectionDisconnect(0);
            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });

            // Send the command
            bglib.SendCommand(serialAPI, cmd);

            // Stop scanning if we are scanning.
            cmd = bglib.BLECommandGAPEndProcedure();
            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });

            // Send the command.
            bglib.SendCommand(serialAPI, cmd);

            // Stop advertising if we are advertising.
            cmd = bglib.BLECommandGAPSetMode(0, 0);
            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });

            // Send the command.
            bglib.SendCommand(serialAPI, cmd);

            // Enable the "GO" button to allow the user to start again.
            btnGo.Enabled = true;

            // Update the application state.
            app_state = STATE_STANDBY;
        }
Esempio n. 8
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;
                    }
                }
            }
        }
Esempio n. 9
0
 private void btnDisconn_Click(object sender, EventArgs e)
 {
     bglib.SendCommand(serialAPI, bglib.BLECommandConnectionDisconnect(0));
 }