private async void Button_spate(object sender, EventArgs e) { IBluetoothAdapter adapter = DependencyService.Resolve <IBluetoothAdapter>(); BluetoothDeviceModel device = (BluetoothDeviceModel)BindingContext; if (device != null) { using (var connection = adapter.CreateConnection(device)) { if (await connection.RetryConnectAsync(retriesCount: 20)) { byte[] buffer = new byte[BufferSize] { (byte)'B' }; if (!await connection.RetryTransmitAsync(buffer, OffsetDefault, buffer.Length)) { await DisplayAlert("Error", "Cannot send data", "Close"); } } else { await DisplayAlert("Error", "Cannot connect.", "Close"); } connection.Dispose(); } } }
private async Task <bool> TryConnect(BluetoothDeviceModel bluetoothDeviceModel) { const bool Connected = true; const bool NotConnected = false; var connection = _bluetoothAdapter.CreateManagedConnection(bluetoothDeviceModel); try { connection.Connect(); App.CurrentBluetoothConnection = connection; return(Connected); } catch (BluetoothConnectionException exception) { await DisplayAlert("Connection error", $"Can not connect to the device: {bluetoothDeviceModel.Name}({bluetoothDeviceModel.Address}).\n" + $"Exception: \"{exception.Message}\"\n" + "Please, try another one.", "Close"); return(NotConnected); } catch (Exception exception) { await DisplayAlert("Generic error", exception.Message, "Close"); return(NotConnected); } }
private async void lvBluetoothBoundedDevices_ItemSelected(object sender, SelectedItemChangedEventArgs e) { BluetoothDeviceModel bluetoothDeviceModel = e.SelectedItem as BluetoothDeviceModel; lvBluetoothBoundedDevices.SelectedItem = null; if (bluetoothDeviceModel != null) { var connected = await TryConnect(bluetoothDeviceModel); if (connected) { await Navigation.PushAsync(new DigitPage()); } } }
public static Tuple <List <WifiDeviceModel>, List <BluetoothDeviceModel> > ReadClusterDevices() { List <WifiDeviceModel> wifiDevices = new List <WifiDeviceModel>(); string[] Lines = File.ReadAllLines(WifiDevicesFileName); foreach (string line in Lines) { string[] data = line.Split("\t"); WifiDeviceModel bt = new WifiDeviceModel() { BSSID = data[0], SSID = data[1], Capabilities = data[2], VenueName = data[3], OperatorFriendlyName = data[4], Level = int.Parse(data[5]), DateTime = DateTime.Parse(data[6]) }; wifiDevices.Add(bt); } Lines = File.ReadAllLines(BluetoothDeviceFileName); List <BluetoothDeviceModel> bluetoothDevices = new List <BluetoothDeviceModel>(); foreach (string line in Lines) { string[] data = line.Split("\t"); BluetoothDeviceModel bluetoothDeviceModel = new BluetoothDeviceModel() { Name = data[0], Type = data[1], RSSI = int.Parse(data[2]), PowerLevel = int.Parse(data[3]), Address = data[4], DateTime = DateTime.Parse(data[5]) }; bluetoothDevices.Add(bluetoothDeviceModel); } return(new Tuple <List <WifiDeviceModel>, List <BluetoothDeviceModel> >(wifiDevices, bluetoothDevices)); }
private void PromptBluetoothOptions(IEnumerable <BluetoothDeviceModel> pairedDevices) { if (pairedDevices == null) { pairedDevices = new List <BluetoothDeviceModel>(); } pairedDevices = pairedDevices.Append(BluetoothDeviceModel.GetDemoDevice()); // specify an adapter var adapter = new BluetoothDeviceAdapter(pairedDevices); adapter.ItemClick += async(s1, arg1) => { await ViewModel.GetDiagnosticDevice(arg1.DeviceAddress); }; DeviceRecyclerView.SetAdapter(adapter); ConnectingLayout.Visibility = Android.Views.ViewStates.Gone; DeviceSelectionLayout.Visibility = Android.Views.ViewStates.Visible; }
public IBluetoothManagedConnection CreateManagedConnection(BluetoothDeviceModel bluetoothDeviceModel) { return(new BluetoothManagedConnection(bluetoothDeviceModel.Address)); }