private async void ConnectBTN_Click(object sender, RoutedEventArgs e) { if (txtbxDeviceName.Text != "") { if (ConnectBTN.Tag.ToString() == "connect") { gloablvalue.DeviceName = txtbxDeviceName.Text; ConnectBTN.IsEnabled = false; if (gloablvalue.DeviceState == "-1")//device not connect { BluetoothConnection.ConnectToDevice(); Devicestate_txt.Text = "Connecting..."; newrec_txt.Text = gloablvalue.checksend(gloablvalue.RecivedText); Hide.Begin(); } } else if (ConnectBTN.Tag.ToString() == "Dissconnect") { if (gloablvalue.DeviceState == "1")//device connected { ConnectBTN.IsEnabled = false; try { BluetoothConnection.CloseDevice(); Devicestate_txt.Text = "' " + gloablvalue.DeviceName + " ' " + " Dissconnected"; ConnectBTN.Content = "connect"; ConnectBTN.Tag = "connect"; ConnectBTN.IsEnabled = true; //gloablvalue.DeviceState = "-1"; } catch (Exception) { // ... } } } } else { var dlg = new MessageDialog("please type a device name.."); await dlg.ShowAsync(); } }