public void StopDiscoveringDevices() { if (BtAdapter.IsEnabled) { BtAdapter.CancelDiscovery(); try { BGData.activity.UnregisterReceiver(this); Names.Clear(); } catch (Exception ex) { Names.Clear(); } } if (!BtBeingEnabled) { BtBeingEnabled = true; TryEnableBluetooth(); } }
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(); }); } } } }); }