Пример #1
0
        private void btnGo_Click(object sender, EventArgs e)
        {
            // start the scan/connect process now
            Byte[] cmd;

            // set scan parameters
            cmd = bglib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1); // 125ms interval, 125ms window, active scanning
            // PRINT_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()) ;

            // begin scanning for BLE peripherals
            cmd = bglib.BLECommandGAPDiscover(1); // generic discovery mode
            // PRINT_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()) ;

            // update state
            app_state = STATE_SCANNING;

            // disable "GO" button since we already started, and sending the same commands again sill not work right
            btnGo.Enabled = false;
        }
Пример #2
0
        private void btnGo_Click(object sender, EventArgs e)
        {
            byte[] cmd;

            if (app_state == STATE_SCANNING)
            {
                btnGo.Enabled = false;

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

                // update state
                app_state = STATE_STANDBY;

                btnGo.Text    = "start search...";
                btnGo.Enabled = true;
            }
            else
            {
                // start the scan/connect process now
                btnGo.Enabled = false;

                // set scan parameters
                cmd = bglib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1); // 125ms interval, 125ms window, active scanning
                ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });
                bglib.SendCommand(cmd);

                // begin scanning for BLE peripherals
                cmd = bglib.BLECommandGAPDiscover(1); // generic discovery mode
                ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); });
                bglib.SendCommand(cmd);

                // update state
                app_state = STATE_SCANNING;

                // disable "GO" button since we already started, and sending the same commands again sill not work right
                btnGo.Text    = "stop search...";
                btnGo.Enabled = true;
            }
        }
        private void BLEScan()
        {
            // Set scan parameters
            int  scan_interval = 125; // in ms
            int  scan_window   = 125; // in ms
            byte active        = 1;

            cmd = bglib.BLECommandGAPSetScanParameters(
                Convert.ToUInt16(scan_interval * 1000 / 625),
                Convert.ToUInt16(scan_window * 1000 / 625),
                active); // 125ms interval, 125ms window, active scanning
            MessageWriter.LogWrite("ble_cmd_gap_set_scan_parameters: ", string.Format("scan_interval={0}; scan_window={1}, active={2}", scan_interval, scan_window, active));
            bglib.SendCommand(SelectedPort, cmd);

            // Begin discovery mode
            byte mode = 1;

            cmd = bglib.BLECommandGAPDiscover(mode); // generic discovery mode
            MessageWriter.LogWrite("ble_cmd_gap_discover: ", string.Format("mode={0}", mode));
            bglib.SendCommand(SelectedPort, cmd);
        }