void LookWhatYouMadeMeDo(BluetoothLeDevice device) { tmr = new Timer(1000); tmr.Elapsed += TMR_INTV; tmr.Start(); Sport_modebtn.Clicked += Sport_modebtn_Clicked; try { vibr = Vibrator.Vibrators.FirstOrDefault(); } catch { } if (device != null) { this.device = device; client = device.GattConnect(false); //Connect.Clicked += Connect_Clicked; WaiterReaderConnecter(); } else { } try { } catch { } }
public override void Connect(ConnectionConfig config) { if (this.gatt != null) { return; } this.gatt = this.native.GattConnect(config.AutoConnect); }
public override void Connect() { if (this.gatt != null) { return; } this.gatt = this.native.GattConnect(true); // TODO: connecting }
/// <summary> /// This function is called when any discovered device /// is tapped from devicelistview. First it creates a StackLayout /// with UuidListView and GattLabel on a ContentPage. /// Then creates a new NavigationPage using the ContentPage, /// because NaviagtionPage manages navigation and user-experience /// of a stack of other pages. And then it pushes this NaviagationPage /// on top of MainPage. Once the page is rendered it registers for the /// GattConnectionStateChanged event handler. Then it issues /// GATT connection to the tapped device. It waits until GATT connection /// is successfull, then it fetches all the service uuids it supports /// and renders it in the new page. Finally it does GATT disconnect and /// all postprocessing. /// </summary> /// <param name="sender">object</param> /// <param name="e">ItemTappedEventArgs</param> public async void DeviceTapped(object sender, ItemTappedEventArgs e) { if (e.Item != null) { Device device = e.Item as Device; if (UuidListView == null) { CreateUUIDListView(); } ContentPage content = new ContentPage { Title = "UUID List", Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { GattLabel, UuidListView, } }, }; GattLabel.Text = "GattConnect initiated"; GattLabel.FontAttributes = FontAttributes.Bold; GattLabel.FontSize = 24; UuidPage = new NavigationPage(content); await MainPage.Navigation.PushAsync(UuidPage); GattClient = BluetoothGattClient.CreateClient(device.Address); await GattClient.ConnectAsync(false); GattClient.ConnectionStateChanged += Device_GattConnectionStateChanged; await WaitStateChangedFlag(); GattLabel.Text = "GattConnected"; IEnumerable <BluetoothGattService> srv_list; srv_list = GattClient.GetServices(); foreach (BluetoothGattService srv in srv_list) { UuidList.Add(srv.Uuid); } UuidListView.ItemsSource = null; UuidListView.ItemsSource = UuidList; GattClientExit(device.Ledevice); } }
///<summary> /// Called after tapping device from DeviceListView. /// Creates new CirclePage with list of tapped device /// service UUIDs. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event arguments</param> public async void DeviceTapped(object sender, ItemTappedEventArgs e) { if (e.Item != null) { Device device = e.Item as Device; if (UuidListView == null) { CreateUUIDListView(); } UuidList.Clear(); CirclePage content = new CirclePage { RotaryFocusObject = UuidListView, Content = new StackLayout { Padding = new Thickness(0, 30), Children = { GattLabel, UuidListView, } }, }; NavigationPage.SetHasNavigationBar(content, false); GattLabel.Text = "GattConnect\n" + "initiated"; GattLabel.HorizontalTextAlignment = TextAlignment.Center; UuidPage = content; await MainPage.Navigation.PushAsync(UuidPage); device.Ledevice.GattConnectionStateChanged += Device_GattConnectionStateChanged; GattClient = device.Ledevice.GattConnect(false); await WaitStateChangedFlag(); GattLabel.Text = "GattConnected"; foreach (BluetoothGattService srv in GattClient.GetServices()) { UuidList.Add(srv.Uuid); } UuidListView.ItemsSource = null; // Refreshing does not work without it UuidListView.ItemsSource = UuidList; GattClientExit(device.Ledevice); } }
/// <summary> /// Unregisters GattConnectionStateChanged event handler /// and breaks the connection with GATT and GATT Client. /// </summary> /// <param name="leDevice">Bluetooth LE Device</param> public void GattClientExit(BluetoothLeDevice leDevice) { if (leDevice != null) { leDevice.GattConnectionStateChanged -= Device_GattConnectionStateChanged; leDevice.GattDisconnect(); leDevice = null; GattClient.DestroyClient(); GattClient = null; } StateChangedFlag = false; }
/// <summary> /// This function does all post processing. /// First it unregisters the event handler registered /// for GattConnectionStateChanged. Then it iniates GATT /// disconnect. Finally it destroys the GATT Client. /// </summary> /// <param name="leDevice">BluetoothLeDevice</param> public void GattClientExit(BluetoothLeDevice leDevice) { if (!leDevice.Equals(null)) { GattClient.ConnectionStateChanged -= Device_GattConnectionStateChanged; GattClient.DisconnectAsync(); leDevice = null; GattClient.Dispose(); GattClient = null; } StateChanged_flag = false; }
public override void CancelConnection() { this.gatt?.DestroyClient(); this.gatt = null; }