/// <summary> /// Connect to a BluetoothDevice /// </summary> /// <param name="device">The device you want to connect to</param> /// <returns></returns> public bool Connect(BluetoothDevice device) { if (state != State.NOT_CONNECTED || device == null || paused) { return(true); // Don't try to connect again if the connection is open, don't try to connect to null devices, and don't try to connect when paused } lock ("connectlock") { Console.WriteLine("Connecting to " + device); SetState(State.CONNECTING); this.device = device; sock = device.CreateRfcommSocketToServiceRecord(MY_UUID); try { sock.Connect(); } catch (Java.IO.IOException) { SetState(State.NOT_CONNECTED); handler.UpdateStatus("Connection failed"); Console.WriteLine("Crap, connection failed"); return(false); } Console.WriteLine("Probably connected"); OutStream = sock.OutputStream; InStream = sock.InputStream; SetState(State.CONNECTED); last_ping = DateTime.UtcNow; if (!RecieveThread.IsAlive) { RecieveThread.Start(); } // we don't use Send() here because we want it to be sent first Byte[] handshake = Encoding.UTF8.GetBytes(WifiClient.WifiHandshake(context)); OutStream.Write(handshake, 0, handshake.Length); if (SendQueue.Count > 0) { Send(); // Poke the send thread so we can make sure the queue is proccessed } StartPing(); return(true); } }
/// <summary> /// This method is called as a callback by the bluetooth client when it gets the wifi negotiation handshake. /// </summary> /// <param name="handshake">The wifi negotiation handshake we recieved from the server</param> private void WifiCallback(byte[] handshake) { if (handshake == null) { HasWifi = false; wifi = null; } else { wifi = new WifiClient(handshake); HasWifi = true; if (bt.state == BluetoothClient.State.CONNECTED) { handler.UpdateStatus("Connected (with wifi)"); } } if (bt.state == BluetoothClient.State.CONNECTED) { Screenshot(); // Ask for a screenshot, since the handshake is now complete } }