private void AttemptToConnect() { connector = new SpheroConnector(); connector.Scan(); foreach (var device in connector.DeviceNames) { if (device.ToLower().Contains("sphero")) { Console.WriteLine($"Attempting to connect to {device}"); sphero = connector.Connect(device); return; } } }
//evento chamado na execução da thread separada private void SearchSpheroWorker_DoWork(object sender, DoWorkEventArgs e) { //checar se a variável local Sphero já está preenchida if (m_SpheroConnector.Sphero == null) { //enumerar todos os dispositivos bluetooth e retornar o sphero entre eles m_SpheroConnector.Find(); //caso o sphero tenha sido encontrado if (m_SpheroConnector.DeviceFound) { setStatusText("Sphero: Conectando..."); printToLog("Conectando com o Sphero..."); //tentar conectar com o sphero por até 5 vezes antes de desistir m_SpheroConnector.Connect(5); } } }
static void Main(string[] args) { SpheroConnector spheroConnector = new SpheroConnector(); Sphero sphero = null; string[] parameters = new string[] { "none" }; string command = "none"; while (!string.IsNullOrEmpty(command)) { command = parameters[0]; switch (command) { case "find": spheroConnector.Scan(); var deviceNames = spheroConnector.DeviceNames; for (int i = 0; i < deviceNames.Count; i++) { Console.WriteLine("{0}: {1}", i, deviceNames[i]); } break; case "connect": if (parameters.Length < 2) { break; } int index = -1; if (int.TryParse(parameters[1], out index)) { try { sphero = spheroConnector.Connect(index); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Console.WriteLine("'{0}' is not a valid device index.", parameters[1]); } break; case "close": spheroConnector.Close(); break; case "sleep": sphero.Sleep(); break; case "setcolor": if (parameters.Length < 2) { break; } ChangeColor(sphero, parameters[1]); break; case "getresponses": { if (parameters.Length < 2) { break; } int count; if (int.TryParse(parameters[1], out count)) { lock (sphero.Listener.SyncRoot) { IEnumerable <SpheroResponsePacket> packets = sphero.Listener.GetLastResponsePackets(count); foreach (var packet in packets) { Console.WriteLine(packet); } } } } break; case "getasync": { if (parameters.Length < 2) { break; } int count; if (int.TryParse(parameters[1], out count)) { lock (sphero.Listener.SyncRoot) { IEnumerable <SpheroAsyncPacket> packets = sphero.Listener.GetLastAsyncPackets(count); foreach (var packet in packets) { Console.WriteLine(packet); } } } } break; case "exit": spheroConnector.Close(); Console.WriteLine("See you soon ... ;) "); Thread.Sleep(1500); return; case "sendprogram": { var area = StorageArea.Temporary; IEnumerable <string> programLines = GetOrbBasicLinesFromFile("orbbasic.txt"); foreach (var programLine in programLines) { Console.WriteLine(programLine); } sphero.EraseOrbBasicStorage(area); sphero.SendOrbBasicProgram(area, programLines); } break; case "runprogram": { StorageArea area = StorageArea.Temporary; sphero.ExecuteOrbBasicProgram(area, 10); } break; case "abortprogram": sphero.AbortOrbBasicProgram(); break; case "diagnostics": { sphero.PerformLevelOneDiagnostics(); } break; case "none": Console.WriteLine("Welcome to Sphero CMD controll application"); break; case "help": Console.WriteLine("HELP will be realized soon."); break; case "goForward": if (parameters.Length < 3) { break; } ChangeColor(sphero, "Red"); GoForward(sphero, Byte.Parse(parameters[1]), int.Parse(parameters[2])); break; default: Console.WriteLine("Unknown command. Please type 'help' for getting list of available commands."); break; } Console.Write("> "); parameters = Console.ReadLine().Split(new char[] { ' ' }); } }