/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void BLEConnectionDisconnectedEvent(object sender, Bluegiga.BLE.Events.Connection.DisconnectedEventArgs e) { Application.Current.Dispatcher.Invoke(delegate { MessageWriter.LogWrite("ble_evt_connection_disconnected: ", string.Format("connection={0}, reason={1}", e.connection, e.reason)); BLEPeripheral peripheral; if (connectedDevices.TryRemoveByConnection(e.connection, out peripheral)) { peripheral.ConnectionState = BLEPeripheral.ConnState.Disconnected; } // Stop advertising cmd = bglib.BLECommandGAPEndProcedure(); MessageWriter.LogWrite("ble_cmd_gap_end_procedure", ""); bglib.SendCommand(SelectedPort, cmd); // Reset GAP Mode cmd = bglib.BLECommandGAPSetMode(0, 0); MessageWriter.LogWrite("ble_cmd_gap_set_mode: ", "discover=0, connect=0"); bglib.SendCommand(SelectedPort, cmd); BLEScan(); }); }
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; }
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); } }
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 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; }
public void StopScan() { bglib.SendCommand(serialAPI, bglib.BLECommandGAPEndProcedure()); }
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()); }
internal void EndProcedure() { Byte[] cmd = bglib.BLECommandGAPEndProcedure(); bglib.SendCommand(serialAPI, cmd); }
private void btnStopScan_Click(object sender, EventArgs e) { // send gap_end_procedure() //serialAPI.Write(new Byte[] { 0, 0, 6, 4 }, 0, 4); bglib.SendCommand(serialAPI, bglib.BLECommandGAPEndProcedure()); }