示例#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);
            }
        }
示例#2
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;
        }
示例#3
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);
        }
示例#5
0
 public void Scan()
 {
     bglib.SendCommand(serialAPI, bglib.BLECommandGAPDiscover(1));
 }
示例#6
0
        public override string[] GetAvailableDevices(int scanTime, bool detectBlueGiga)
        {
            List <string>  names          = new List <string>();
            bool           commandStarted = false;
            AutoResetEvent resp           = new AutoResetEvent(false);

            discoveredDevices.Clear();

            if (detectBlueGiga)
            {
                if (/*port == null ||*/ !getBlueGigaStatus())                 //only try to find a BlueGiga dongle if one is not already assigned.
                {
                    if (!ConnectToBluegiga())
                    {
                        return(names.ToArray());
                    }
                }
            }


            DiscoverEventHandler discoverResp = delegate(object sender, DiscoverEventArgs e)
            {
                if (e.result == 0)
                {
                    commandStarted = true;
                }
                resp.Set();
            };

            ScanResponseEventHandler deviceFound = delegate(object sender, ScanResponseEventArgs e)
            {
                if (e.data.Length < 8)
                {
                    return;
                }

                string name = Encoding.UTF8.GetString(e.data, e.data.Length - 8, 8);

                if (!name.Contains("TAPPY"))
                {
                    return;
                }

                foreach (BluetoothDevice device in discoveredDevices)
                {
                    if (device.Name.Equals(name))
                    {
                        return;
                    }
                }

                discoveredDevices.Add(new BluetoothDevice(e.sender, e.bond, e.rssi, name));
            };

            bluetooth.BLEResponseGAPDiscover += discoverResp;

            bluetooth.SendCommand(port, bluetooth.BLECommandGAPDiscover(2));

            resp.WaitOne(200);

            bluetooth.BLEEventGAPScanResponse += deviceFound;
            bluetooth.BLEResponseGAPDiscover  -= discoverResp;

            if (!commandStarted)
            {
                return(names.ToArray());
            }

            Thread.Sleep(scanTime);
            bluetooth.SendCommand(port, bluetooth.BLECommandGAPEndProcedure());

            bluetooth.BLEEventGAPScanResponse -= deviceFound;

            foreach (BluetoothDevice device in this.discoveredDevices)
            {
                names.Add(device.Name);
            }

            return(names.ToArray());
        }
示例#7
0
 private void btnStartScan_Click(object sender, EventArgs e)
 {
     // send gap_discover(mode: 1)
     //serialAPI.Write(new Byte[] { 0, 1, 6, 2, 1 }, 0, 5);
     bglib.SendCommand(serialAPI, bglib.BLECommandGAPDiscover(1));
 }