protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (requestCode == 2) { if (resultCode != Result.Ok) { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show("Bluetooth Must Be Enabled To Play Game"); BGData.btManager.BtBeingEnabled = true; BGData.btManager.TryEnableBluetooth(); } else { //BGData.btManager.BtBeingEnabled = false; //if (BGStuff.settingUpGame == false && BGStuff.Reconnecting == false && BGStuff.ConnectionSetup == false) //{ // BGStuff.mainPage.GetKnownDevices(); // BGStuff.mainPage.StartScan(); // BGData.btManager.ReceivingConnection = true; // BGData.btManager.ReceivePair(); //} } } }
public void ReceivePair() { Task.Run(() => { if (BtAdapter.IsEnabled && !ReceivingConnection) { BluetoothServerSocket tmp = null; try { // MY_UUID is the app's UUID string, also used by the client code. tmp = BtAdapter.ListenUsingRfcommWithServiceRecord("BattleShots", UUID.FromString("00001101-0000-1000-8000-00805f9b34fb")); } catch (Exception e) { Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show(e.Message); }); } mmServerSocket = tmp; ReceivingConnection = true; CheckForPair(); } }); }
public async void SendMessage(string message) { await Task.Run(() => { try { if (Socket.IsConnected) { var buffer = new byte[message.Length]; buffer = Encoding.ASCII.GetBytes(message); outputStream.Write(buffer, 0, message.Length); } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => { ToastLoader toast = new ToastLoader(); toast.Show("Connection Lost"); Reconnect(); }); } }); }
public void SetupChat() { if (Socket.IsConnected) { try { inputStream = Socket.InputStream; outputStream = Socket.OutputStream; } catch (Exception ex) { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show("Failed to SetupChat"); } } }
public async void ReadMessage() { await Task.Run(() => { try { var buffer = new byte[1028]; int numOfBytes; while (Socket.IsConnected && !Reading) { numOfBytes = inputStream.Read(buffer, 0, buffer.Length); Reading = true; if (numOfBytes > 0) { string message = Encoding.ASCII.GetString(buffer); if (BGStuff.settingUpGame) { if (message.Contains("resume")) { Device.BeginInvokeOnMainThread(() => { BGStuff.setupGame.ReceiveResume(); }); } else if (message.Contains("accept")) { Device.BeginInvokeOnMainThread(() => { BGStuff.setupGame.AcceptResume(); }); } else if (message.Contains("reject")) { Device.BeginInvokeOnMainThread(() => { BGStuff.setupGame.RejectResume(); }); } else if (message.Contains("Setup2")) { Device.BeginInvokeOnMainThread(() => { SetupGame.StatGoToSetup2(); }); } else if (message.Contains(",")) { string[] split; split = message.Split(','); if (split[1].Contains("grid")) { Device.BeginInvokeOnMainThread(() => { SetupGame.StatSetPicker(split[0]); }); } else if (split[1].Contains("num")) { Device.BeginInvokeOnMainThread(() => { SetupGame.StatSetNumOfShotsEntry(split[0]); }); } else if (split[1].Contains("nam")) { Device.BeginInvokeOnMainThread(() => { SetupGame.StatSetEnemyName(split[0]); }); } } } else if (BGStuff.settingUpGame2) { if (message.Contains("unready")) { Device.BeginInvokeOnMainThread(() => { BGStuff.setUpGame2.SetEnemyReady(false); }); } else if (message.Contains("ready")) { Device.BeginInvokeOnMainThread(() => { BGStuff.setUpGame2.SetEnemyReady(true); }); } } else if (BGStuff.InGame) { if (message.Contains("hit")) { Device.BeginInvokeOnMainThread(() => { BGStuff.game.ReceiveHit(true); }); } else if (message.Contains("miss")) { Device.BeginInvokeOnMainThread(() => { BGStuff.game.ReceiveHit(false); }); } else if (message.Contains("ready")) { Device.BeginInvokeOnMainThread(() => { BGStuff.game.Ready(); }); } else if (message.Contains("endgame")) { Device.BeginInvokeOnMainThread(() => { BGStuff.game.EndGame(false); }); } else if (message.Contains(",")) { string[] split = message.Split(','); int len = split[0].Length; string temp = ""; for (int i = 0; i < len; i++) { temp += split[1][i]; } Device.BeginInvokeOnMainThread(() => { BGStuff.game.ReceiveCheck(split[0] + "," + temp); }); } else if (message.Contains("0") || message.Contains("1")) { Device.BeginInvokeOnMainThread(() => { BGStuff.game.GoesFirst(int.Parse(message)); }); } buffer = new byte[1028]; } } Reading = false; } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => { ToastLoader toast = new ToastLoader(); toast.Show("Connection Lost"); if (BGStuff.settingUpGame) { BGStuff.setupGame.Reconnect(); } else if (BGStuff.settingUpGame2) { BGStuff.setUpGame2.Reconnect(); } else if (BGStuff.InGame) { BGStuff.game.Reconnect(); } }); Reconnect(); } }); }
private void CheckForPair() { BluetoothSocket socket = null; // Keep listening until exception occurs or a socket is returned. while (ReceivingConnection) { try { socket = mmServerSocket.Accept(); if (socket != null) { // A connection was accepted. Perform work associated with // the connection in a separate thread. //ManageMyConnectedSocket(socket); ReceivingConnection = false; Socket = socket; Master = false; mmServerSocket.Close(); FileManager file = new FileManager(); file.SaveDevice(Socket.RemoteDevice.Name); SetupChat(); if (BGStuff.Reconnecting) { Device.BeginInvokeOnMainThread(() => { ToastLoader toast = new ToastLoader(); toast.Show("Reconnected With Player"); }); Device.BeginInvokeOnMainThread(() => { if (BGStuff.reconnectionPage != null) { BGStuff.reconnectionPage.Navigation.PopAsync(); } }); BGStuff.Reconnecting = false; ReadMessage(); } else { Device.BeginInvokeOnMainThread(() => { MainPage.StatSetupGame(); ToastLoader toast = new ToastLoader(); toast.Show("Connected With Player"); }); } } } catch (Exception e) { Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show(e.Message); }); } } }
public void PairToDevice() { Task.Run(async() => { if (BtAdapter.IsEnabled) { if (BtAdapter.IsDiscovering) { BtAdapter.CancelDiscovery(); } List <BluetoothDevice> devices = BtAdapter.BondedDevices.ToList(); BluetoothDevice tempDevice = null; string tempstring = ""; FileManager file = new FileManager(); tempstring = file.GetDevice(); await Task.Delay(300); Device.BeginInvokeOnMainThread(() => { ToastLoader toast = new ToastLoader(); toast.Show(tempstring); }); await Task.Delay(500); for (int i = 0; i < devices.Count; i++) { if (tempstring == devices[i].Name) { tempDevice = devices[i]; } } ReceivingConnection = false; UUID uuid = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"); BluetoothSocket temp = null; try { if ((int)Android.OS.Build.VERSION.SdkInt >= 10) // Gingerbread 2.3.3 2.3.4 { temp = tempDevice.CreateInsecureRfcommSocketToServiceRecord(uuid); } else { temp = tempDevice.CreateRfcommSocketToServiceRecord(uuid); } Socket = temp; } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show(ex.Message); }); } if (Socket != null) { try { await Socket.ConnectAsync(); Master = true; Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show("Reconnected To Player"); }); SetupChat(); if (BGStuff.Reconnecting) { Device.BeginInvokeOnMainThread(() => { BGStuff.reconnectionPage.Navigation.PopAsync(); }); await Task.Delay(500); BGStuff.Reconnecting = false; Pairing = false; BGStuff.reconnectionPage = null; ReadMessage(); } else { MainPage.StatSetupGame(); } } catch (Exception ex) { if (!BGStuff.Reconnecting) { Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show(ex.Message); }); ReceivingConnection = true; ReceivePair(); } else { Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show(ex.Message); }); } } } } }); }
public void PairToDevice(string name, bool known) { Task.Run(async() => { if (BtAdapter.IsEnabled) { if (BtAdapter.IsDiscovering) { BtAdapter.CancelDiscovery(); } ReceivingConnection = false; FileManager file = new FileManager(); file.SaveDevice(name); BluetoothDevice Device = null; if (known) { foreach (BluetoothDevice device in PairedDevices) { if (device.Name == name) { Device = device; } } } else { foreach (BluetoothDevice device in BGData.bluetoothDevices) { if (device.Name == name) { Device = device; } } } UUID uuid = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"); if ((int)Android.OS.Build.VERSION.SdkInt >= 10) // Gingerbread 2.3.3 2.3.4 { Socket = Device.CreateInsecureRfcommSocketToServiceRecord(uuid); } else { Socket = Device.CreateRfcommSocketToServiceRecord(uuid); } if (Socket != null) { try { await Socket.ConnectAsync(); Master = true; Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show("Connected To Player"); BGStuff.mainPage.IsLoading = false; SetupChat(); MainPage.StatSetupGame(); }); } catch (Exception ex) { Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { ToastLoader toastLoader = new ToastLoader(); toastLoader.Show(ex.Message); BGStuff.mainPage.IsLoading = false; ReceivingConnection = true; ReceivePair(); }); } } } }); }