//选择蓝牙设备(打开串口/按钮) private void link_Click(object sender, EventArgs e) { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); //dialog.ShowRemembered = true;//显示已经记住的蓝牙设备 dialog.ShowAuthenticated = true;//显示已经认证过的蓝牙设备 //dialog.ShowUnknown = true;//显示未知蓝牙设备 if (dialog.ShowDialog() == DialogResult.OK) { address = dialog.SelectedDevice.DeviceAddress;//获取选择的蓝牙地址 if (!BluetoothConnection.IsOpen) { BluetoothConnection = new SerialPort(); link.Enabled = false; BluetoothConnection.PortName = PortList.SelectedItem.ToString(); BluetoothConnection.Open(); BluetoothConnection.BaudRate = 115200;//波特率 BluetoothConnection.ReadTimeout = 1000; BluetoothConnection.DataReceived += new SerialDataReceivedEventHandler(Version); //timer1.Elapsed += Version; //timer1.Start(); //timer1.AutoReset = false; //timer2.Start(); //GC.Collect(); BluetoothConnection.DataReceived += new SerialDataReceivedEventHandler(timer2_Tick); //这是个弹窗 //MessageBox.Show("蓝牙连接成功!\n地址:" + address.ToString() + "\n设备名:" + dialog.SelectedDevice.DeviceName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); prompt.Text = "蓝牙连接成功! 蓝牙 Mac 地址:" + address.ToString() + " 设备名称:" + dialog.SelectedDevice.DeviceName; } } }
private void SelectDeviceButton_Click(object sender, EventArgs e) { try { var result = SelectBluetoothDeviceDialog.ShowDialog(); if (result == DialogResult.OK) { var device = SelectBluetoothDeviceDialog.SelectedDevice; if (!device.Authenticated) { throw new AuthenticationException("设备未认证或不支持,请先打开系统设置,与设备配对并认证后再选择。"); } UpdateDeviceDisplay(device.DeviceName, device.DeviceAddress.ToString()); MainPreferences.Default.DeviceName = device.DeviceName; MainPreferences.Default.DeviceAddress = device.DeviceAddress.ToString(); } } catch (Exception exception) { MessageBox.Show(exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private bool PairDevice() { using (var discoverForm = new SelectBluetoothDeviceDialog()) { if (discoverForm.ShowDialog(this) != DialogResult.OK) { return(false); } var deviceInfo = discoverForm.SelectedDevice; if (!deviceInfo.Authenticated) // previously paired? { if (!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress, _myPin)) { return(false); } } // device should now be paired with the OS so make a connection to it asynchronously var client = new BluetoothClient(); client.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, BluetoothClientConnectCallback, client); return(true); } }
public BluetoothDeviceInfo SelectDevice() { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); var result = dialog.ShowDialog(); return(dialog.SelectedDevice); }
private void button4_Click(object sender, EventArgs e) { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); // dialog.ShowAuthenticated = true; dialog.ShowRemembered = true; dialog.ShowUnknown = true; OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { System.Uri uri = new Uri("obex://" + selectedDevice.DeviceInfo.DeviceAddress + "/" + ofd.FileName); ObexWebRequest request = new ObexWebRequest(uri); request.ReadFile(ofd.FileName); ObexWebResponse response = (ObexWebResponse)request.GetResponse(); MessageBox.Show(response.StatusCode.ToString()); response.Close(); Cursor.Current = Cursors.Default; } else { MessageBox.Show("File Not Selected"); } }
private void btnBluetooth_Click(object sender, System.EventArgs e) { // use the new select bluetooth device dialog SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog(); sbdd.ShowAuthenticated = true; sbdd.ShowRemembered = true; sbdd.ShowUnknown = true; if (sbdd.ShowDialog() == DialogResult.OK) { if (ofdFileToBeam.ShowDialog() == DialogResult.OK) { Cursor.Current = Cursors.WaitCursor; System.Uri uri = new Uri("obex://" + sbdd.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(ofdFileToBeam.FileName)); ObexWebRequest request = new ObexWebRequest(uri); request.ReadFile(ofdFileToBeam.FileName); ObexWebResponse response = (ObexWebResponse)request.GetResponse(); MessageBox.Show(response.StatusCode.ToString()); response.Close(); Cursor.Current = Cursors.Default; } } }
private BluetoothDeviceInfo DiscoverBtDevice() { try { #if true BluetoothSearch dlgSearch = new BluetoothSearch(); DialogResult result = dlgSearch.ShowDialog(); if (result != DialogResult.OK) { return(null); } return(dlgSearch.GetSelectedBtDevice()); #else SelectBluetoothDeviceDialog dlg = new SelectBluetoothDeviceDialog { ShowAuthenticated = true, ShowRemembered = false, ShowUnknown = true }; dlg.ClassOfDevices.Clear(); dlg.ClassOfDevices.Add(new ClassOfDevice(DeviceClass.Uncategorized, ServiceClass.None)); DialogResult result = dlg.ShowDialog(_form); if (result != DialogResult.OK) { return(null); } return(dlg.SelectedDevice); #endif } catch (Exception) { return(null); } }
private void button1_Click(object sender, EventArgs e) { MessageBox.Show("ObexWebRequest does not support GET."); // // SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog(); sbdd.ShowAuthenticated = true; sbdd.ShowRemembered = true; sbdd.ShowUnknown = true; if (sbdd.ShowDialog() == DialogResult.OK) { Cursor.Current = Cursors.WaitCursor; System.Uri uri = new Uri("obex://" + sbdd.SelectedDevice.DeviceAddress.ToString()); ObexWebRequest request = new ObexWebRequest(uri); request.Method = "GET"; request.ContentType = InTheHand.Net.Mime.MediaTypeNames.ObjectExchange.Capabilities; //request.ReadFile("C:\\t4s.log"); //request.ContentType = InTheHand.Net.Mime.MediaTypeNames.Text.Plain; ObexWebResponse response = (ObexWebResponse)request.GetResponse(); response.Close(); Cursor.Current = Cursors.Default; } }
public void ChooseDevice() { var dialog = new SelectBluetoothDeviceDialog(); dialog.ShowAuthenticated = true; dialog.ShowRemembered = true; dialog.ShowUnknown = true; BluetoothClient client = new BluetoothClient(); if (dialog.ShowDialog() == DialogResult.OK) { deviceInfo = dialog.SelectedDevice; deviceInfo.SetServiceState(BluetoothService.ObexObjectPush, true); // var ep = new BluetoothEndPoint(deviceInfo.DeviceAddress, BluetoothService.ObexObjectPush); // client.Connect(ep); if (deviceInfo.Connected) { label4.Visible = true; label4.Text = "Połączono z " + deviceInfo.DeviceName; } else { } } else { this.Close(); }; }
private async Task AddDeviceAsync() { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog() { ShowRemembered = false, ShowAuthenticated = true, ShowUnknown = true }; DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { BluetoothDeviceInfo selectedDevice = dialog.SelectedDevice; if (selectedDevice != null && !BluetoothDevices.Any(d => (d as BluetoothDeviceItem)?.DeviceInfo.DeviceAddress == selectedDevice.DeviceAddress)) { BluetoothDeviceItem item = await BluetoothDeviceItem.GetBluetoothDeviceItemAsync(selectedDevice); if (item == null) { MessageBox.Show("该设备没有可连接的服务", "错误"); } else { BluetoothDevices.Add(item); foreach (var s in item.Services) { allBluetoothItem.Insert(s); } } } } }
/// <summary> /// 连接函数 /// </summary> /// <returns></连接失败返回false> private bool connect() { // 创建选择框选择要连接的设备 using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog()) { bldialog.ShowRemembered = false; if (bldialog.ShowDialog() == DialogResult.OK) { if (bldialog.SelectedDevice == null) { MessageBox.Show("No device selected"); return(false); } BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice; if (!selecteddevice.Authenticated) { if (!BluetoothSecurity.PairRequest(selecteddevice.DeviceAddress, "0000")) { MessageBox.Show("PairRequest Error"); return(false); } } try { // 对指定的目标进行蓝牙连接请求 client = new BluetoothClient(); client.Connect(selecteddevice.DeviceAddress, BluetoothService.SerialPort); stream = client.GetStream(); stream.Write(System.Text.Encoding.ASCII.GetBytes("teacher"), 0, 7); } catch { return(false); } StateOfRunning(); stream = client.GetStream(); // 标记stream已经接受对象实例化 SIGN_STREAMSETUP = true; // 启动接收图片的线程 receiving = true; ReceiveThread = new System.Threading.Thread(ReceiveLoop); ReceiveThread.Start(); // 启动信息发送线程 sendDataThread = new System.Threading.Thread(SendDataLoop); closeSendData(); sendDataThread.Start(); sendfileThread = new System.Threading.Thread(SendFileLoop); return(true); } return(false); } }
private void pushObexFileToBeam() { // use the new select bluetooth device dialog SelectBluetoothDeviceDialog mSelectBluetoothDeviceDialog = new SelectBluetoothDeviceDialog(); mSelectBluetoothDeviceDialog.ShowAuthenticated = true; // 顯示已經記住的藍牙設備 mSelectBluetoothDeviceDialog.ShowRemembered = true; // 顯示認證過的藍牙設備 mSelectBluetoothDeviceDialog.ShowUnknown = true; // 顯示位置藍牙設備 if (mSelectBluetoothDeviceDialog.ShowDialog() == DialogResult.OK) { OpenFileDialog ofdFileToBeam = new OpenFileDialog(); // 選擇要傳送文件的目的地後, 通過OpenFileDialog選擇要傳輸的文件 ofdFileToBeam.Filter = "Only Text File (*.txt)|*.txt"; // ofdFileToBeam.Filter = "All Files (*.*)|*.*"; if (ofdFileToBeam.ShowDialog() == DialogResult.OK) { Cursor.Current = Cursors.WaitCursor; System.Uri uri = new Uri("obex:// " + mSelectBluetoothDeviceDialog.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(ofdFileToBeam.FileName)); ObexWebResponse response = null; try { // ObexWebRequest 的實現模式和HttpWebRequest類似, 都是發送請求, 等等回應, 回應封裝在ObexWebResponse 類裡面. ObexWebRequest request = new ObexWebRequest(uri); // 通過ObexWebRequest 來傳送文件到目標機器 request.ReadFile(ofdFileToBeam.FileName); response = request.GetResponse() as ObexWebResponse; txtCompareFileResult.ForeColor = Color.Green; txtCompareFileResult.Text = "PASS"; if (IsDebugMode) { Trace.WriteLine("File transfer was successful."); } checkTestStatus("PASS"); } catch (Exception ex) { txtCompareFileResult.ForeColor = Color.Red; txtCompareFileResult.Text = "FAIL"; if (IsDebugMode) { Trace.WriteLine("File transfer failed. Path : " + uri); } checkTestStatus(ex.Message); } finally { if (response != null) { response.Close(); } } Cursor.Current = Cursors.Default; } } }
public void InitAtBluetoothDialog() { SelectBluetoothDeviceDialog dlg = new SelectBluetoothDeviceDialog(); if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { InitPhone(dlg.SelectedDevice.DeviceName); } }
private void btnNewDevice_Click(object sender, EventArgs e) { selDia = new SelectBluetoothDeviceDialog(); selDia.ShowUnknown = true; selDia.AddNewDeviceWizard = true; selDia.ShowDialog(); selDia.AddNewDeviceWizard = false; selDia.ShowDialog(); }
private BluetoothAddress BluetoothSelect() { SelectBluetoothDeviceDialog btDeviceDialog = new SelectBluetoothDeviceDialog(); DialogResult dlgResult = btDeviceDialog.ShowDialog(); if (dlgResult != DialogResult.OK) { AddMessage(MessageSource.Info, "Cancelled select device."); return(null); } return(btDeviceDialog.SelectedDevice.DeviceAddress); }
public static BluetoothDeviceInfo GetDeviceDialog() { var dlg = new SelectBluetoothDeviceDialog(); DialogResult result = dlg.ShowDialog(); if (result != DialogResult.OK) { return null; } BluetoothDeviceInfo device = dlg.SelectedDevice; BluetoothAddress addr = device.DeviceAddress; return device; }
BluetoothAddress BluetoothSelect() { var dlg = new SelectBluetoothDeviceDialog(); var rslt = dlg.ShowDialog(); if (rslt != DialogResult.OK) { //AddMessage(MessageSource.Info, "Cancelled select device."); return null; } var addr = dlg.SelectedDevice.DeviceAddress; return addr; }
private void buttonSelectBluetooth_Click(object sender, EventArgs e) { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true; //显示已经记住的蓝牙设备 dialog.ShowAuthenticated = true; //显示认证过的蓝牙设备 dialog.ShowUnknown = true; //显示位置蓝牙设备 if (dialog.ShowDialog() == DialogResult.OK) { sendAddress = dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址 labelAddress.Text = "地址:" + sendAddress.ToString() + " 设备名:" + dialog.SelectedDevice.DeviceName; } }
private bool connect() { using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog()) { bldialog.ShowRemembered = false; if (bldialog.ShowDialog() == DialogResult.OK) { if (bldialog.SelectedDevice == null) { MessageBox.Show("No device selected"); return(false); } BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice; if (!selecteddevice.Authenticated) { if (!BluetoothSecurity.PairRequest(selecteddevice.DeviceAddress, "0000")) { MessageBox.Show("PairRequest Error"); return(false); } } try { client = new BluetoothClient(); client.Connect(selecteddevice.DeviceAddress, BluetoothService.SerialPort); } catch { return(false); } stream = client.GetStream(); // 标记stream已经接受对象实例化 SIGN_STREAMSETUP = true; // 启动接收图片的进程 receiving = true; receiveThread = new System.Threading.Thread(Receiveloop); receiveThread.Start(); // sendDataThread = new System.Threading.Thread(SendData); closeSendData(); sendDataThread.Start(); return(true); } return(false); } }
// 连接电脑端蓝牙 private bool connect() { using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog()) { bldialog.ShowRemembered = false; if (bldialog.ShowDialog() == DialogResult.OK) { if (bldialog.SelectedDevice == null) { MessageBox.Show("No device selected"); return(false); } BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice; if (!selecteddevice.Authenticated) { if (!BluetoothSecurity.PairRequest(selecteddevice.DeviceAddress, "0000")) { MessageBox.Show("PairRequest Error"); return(false); } } try { client = new BluetoothClient(); client.Connect(selecteddevice.DeviceAddress, BluetoothService.SerialPort); stream = client.GetStream(); stream.Write(System.Text.Encoding.ASCII.GetBytes("student" + LOCALBLUETOOTHNAME), 0, 7 + LOCALBLUETOOTHNAME.Length); } catch { return(false); } StateOfRunning(); stream = client.GetStream(); // 标记stream已经接受对象实例化 SIGN_STREAMSETUP = true; // 启动接收图片的进程 receiving = true; // sendfileThread = new System.Threading.Thread(SendFileLoop); return(true); } return(false); } }
BluetoothAddress BluetoothSelect() { var dlg = new SelectBluetoothDeviceDialog(); var rslt = dlg.ShowDialog(); if (rslt != DialogResult.OK) { AddMessage(MessageSource.Info, "Cancelled select device."); return(null); } var addr = dlg.SelectedDevice.DeviceAddress; return(addr); }
void Discovery2() { ///...and var dlg = new SelectBluetoothDeviceDialog(); DialogResult result = dlg.ShowDialog(this); if (result != DialogResult.OK) { return; } BluetoothDeviceInfo device = dlg.SelectedDevice; BluetoothAddress addr = device.DeviceAddress; }
/// <summary> /// Allow the user to select a Bluetooth device in a Windows dialog. /// </summary> /// <returns></returns> public static BluetoothDevice SelectDeviceInDialog(IWin32Window window) { var dlg = new SelectBluetoothDeviceDialog(); var result = dlg.ShowDialog(window); if (result != DialogResult.OK || dlg.SelectedDevice == null) { return(null); } var bluetoothDevice = new BluetoothDevice(dlg.SelectedDevice.DeviceAddress); DeviceStorage.SaveLastDevice(bluetoothDevice); return(bluetoothDevice); }
private void btnQueryBluetooth_Click(object sender, EventArgs e) { btnQueryBluetooth.Enabled = false; radio = BluetoothRadio.PrimaryRadio; //获取当前PC的蓝牙适配器 if (radio == null) //检查该电脑蓝牙是否可用 { MessageBox.Show("Bluetooth is not available for this device!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information); btnQueryBluetooth.Enabled = true; return; } else { blueClient = new BluetoothClient(); SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true; //显示已经记住的蓝牙设备 dialog.ShowAuthenticated = true; //显示认证过的蓝牙设备 dialog.ShowUnknown = true; //显示位置蓝牙设备 if (dialog.ShowDialog() == DialogResult.OK) { sendAddress = dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址 } if (sendAddress == null) { MessageBox.Show("Did not select the Bluetooth device!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information); btnQueryBluetooth.Enabled = true; return; } else { string pin = "1234";//默认的蓝牙pin值 if (Connect(sendAddress, pin)) { MessageBox.Show("Connect Bluetooth successfully"); btnPrint.Enabled = true; btnPrintTestPage.Enabled = true; btnLogout.Enabled = true; btnOneDimensionalCode.Enabled = true; btnPrintQRCode.Enabled = true; } else { btnQueryBluetooth.Enabled = true; MessageBox.Show("Failed to connect to Bluetooth"); } } } }
/// <summary> /// handler of device click handler /// </summary> /// /// <param name="sender"> /// sender of the event /// </param> /// /// <param name="e"> /// event arguments /// </param> private void SelectDeviceClickHandler(object sender, EventArgs e) { SelectBluetoothDeviceDialog selDia; selDia = new SelectBluetoothDeviceDialog(); if (selDia.ShowDialog() != DialogResult.OK) { return; } m_deviceAddr = selDia.SelectedDevice.DeviceAddress; m_deviceTextbox.Text = selDia.SelectedDevice.DeviceName; m_settings.DeviceName = selDia.SelectedDevice.DeviceName; m_settings.DeviceAddress = selDia.SelectedDevice.DeviceAddress.ToString(); m_settings.Save(); }
private void Scan_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var dlg = new SelectBluetoothDeviceDialog(); dlg.DeviceFilter = delegate(BluetoothDeviceInfo info) { return(info.DeviceName.Contains("Buds")); }; DialogResult result = dlg.ShowDialog(); if (result != DialogResult.OK) { return; } BluetoothDeviceInfo device = dlg.SelectedDevice; _address = device.DeviceAddress; _device = device; DevName.TextDetail = device.DeviceName; DevAddress.TextDetail = BytesToMacString(_address.ToByteArrayBigEndian(), 0); /* We do not support neobeans! */ if (device.DeviceName.Contains("Galaxy Buds Live")) { DevModel.TextDetail = "Galaxy Buds Live (2020)"; } else if (device.DeviceName.Contains("Galaxy Buds+")) { DevModel.TextDetail = "Galaxy Buds+ (2020)"; } else if (device.DeviceName.Contains("Galaxy Buds")) { DevModel.TextDetail = "Galaxy Buds (2019)"; } else { /* Set field to "Unknown". (Yes, I know that's a dirty solution but I'm too lazy right now :$ ) */ DevModel.TextDetail = Loc.GetString("settings_cpopup_position_placeholder"); } BottomNavBar.Visibility = Visibility.Visible; }
private void regDevice_Click(object sender, EventArgs e) { selDia = new SelectBluetoothDeviceDialog(); //selDia.ShowUnknown = true; //selDia.AddNewDeviceWizard = true; //selDia.ShowDialog(); selDia.AddNewDeviceWizard = false; if (selDia.ShowDialog() != DialogResult.OK) { return; } deviceAddr = (selDia.SelectedDevice.DeviceAddress).ToString(); deviceName = selDia.SelectedDevice.DeviceName; result.AppendText(deviceAddr + " " + deviceName); ds = new DeviceScanner(deviceAddr); ds.Start(); }
private void button1_Click(object sender, EventArgs e) { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true; //show memorized devices dialog.ShowAuthenticated = true; //show authorized devices dialog.ShowUnknown = true; //show unknown devices if (dialog.ShowDialog() == DialogResult.OK) { txtMAC.Text = ""; for (int i = 0; i < 6; i++) { txtMAC.Text += dialog.SelectedDevice.DeviceAddress.ToByteArray()[5 - i].ToString("x2").ToUpper();//To get the iverse order of the addr if (i < 5) { txtMAC.Text += ":"; } } } }
public void Discovery2F() { EventHandler realWork = delegate { ///...and var dlg = new SelectBluetoothDeviceDialog(); dlg.DeviceFilter = FilterDevice; DialogResult result = dlg.ShowDialog(); if (result != DialogResult.OK) { return; } BluetoothDeviceInfo device = dlg.SelectedDevice; BluetoothAddress addr = device.DeviceAddress; }; ///// Application.Idle += realWork; Application.Run(); }
private void ChooseServer_Click(object sender, EventArgs e) { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true; //显示已经记住的蓝牙设备 dialog.ShowAuthenticated = true; //显示认证过的蓝牙设备 dialog.ShowUnknown = true; //显示位置蓝牙设备 if (dialog.ShowDialog() == DialogResult.OK) { sendAddress = dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址 labelAddress.Text = "地址:" + sendAddress.ToString() + " 设备名:" + dialog.SelectedDevice.DeviceName; //BluetoothEnd. BluetoothClient Blueclient = new BluetoothClient(); BluetoothDeviceInfo[] Devices = Blueclient.DiscoverDevices(); //Blueclient.SetPin(sendAddress, "288063"); Blueclient.Authenticate = false; Blueclient.Connect(sendAddress, BluetoothService.Handsfree); this.buttonSend.Enabled = true; // Blueclient.EndConnect(); } }
private void DoSecurityActionWithPinInput(string name, ActionSecurityWithPin action) { try { SelectBluetoothDeviceDialog mDeviceDialog = new SelectBluetoothDeviceDialog(); // 選擇藍牙裝置清單 mDeviceDialog.ShowRemembered = true; // 顯示已經記住的藍牙設備 mDeviceDialog.ShowAuthenticated = true; // 顯示認證過的藍牙設備 mDeviceDialog.ShowUnknown = true; // 顯示位置藍牙設備 if (mDeviceDialog.ShowDialog() == DialogResult.OK) { selectedDevice = (BluetoothDeviceInfo)mDeviceDialog.SelectedDevice; UpdateBTDeviceDetails("\r\nDevice Name : " + mDeviceDialog.SelectedDevice.DeviceName.ToString()); UpdateBTDeviceDetails("Address : " + mDeviceDialog.SelectedDevice.DeviceAddress.ToString());// 獲取選擇的遠程藍牙地址 bool result = action(selectedDevice.DeviceAddress, ""); UpdateBTDeviceDetails("Bluetooth Pair Request, Result : " + result); if (result) { UpdateFindDeviceResult("Device connected."); buttonPairRequest.Enabled = false; buttonAudioTest.Enabled = true; } else { SetFailEvent(); } } } catch (SocketException sex) { UpdateBTDeviceDetails("Connect failed : " + sex.Message + ", Code : " + sex.SocketErrorCode.ToString("D")); SetFailEvent(); } catch (Exception ex) { UpdateBTDeviceDetails(ex.Message); SetFailEvent(); } }
private void Scan_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var dlg = new SelectBluetoothDeviceDialog(); dlg.DeviceFilter = delegate(BluetoothDeviceInfo info) { return(info.DeviceName.Contains("Buds")); }; DialogResult result = dlg.ShowDialog(); if (result != DialogResult.OK) { return; } BluetoothDeviceInfo device = dlg.SelectedDevice; _address = device.DeviceAddress; DevName.TextDetail = device.DeviceName; DevAddress.TextDetail = BytesToMacString(_address.ToByteArrayBigEndian(), 0); BottomNavBar.Visibility = Visibility.Visible; }
private bool connect() { using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog()) { bldialog.ShowRemembered = false; if (bldialog.ShowDialog() == DialogResult.OK) { if (bldialog.SelectedDevice == null) { MessageBox.Show("No device selected"); return false; } BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice; if (!selecteddevice.Authenticated) { if (!BluetoothSecurity.PairRequest(selecteddevice.DeviceAddress, "0000")) { MessageBox.Show("PairRequest Error"); return false; } } try { client = new BluetoothClient(); client.Connect(selecteddevice.DeviceAddress, BluetoothService.SerialPort); } catch { return false; } stream = client.GetStream(); // 标记stream已经接受对象实例化 SIGN_STREAMSETUP = true; // 启动接收图片的进程 receiving = true; receiveThread = new System.Threading.Thread(Receiveloop); receiveThread.Start(); // sendDataThread = new System.Threading.Thread(SendData); closeSendData(); sendDataThread.Start(); return true; } return false; } }
private BluetoothAddress BluetoothSelect() { SelectBluetoothDeviceDialog btDeviceDialog = new SelectBluetoothDeviceDialog(); DialogResult dlgResult = btDeviceDialog.ShowDialog(); if (dlgResult != DialogResult.OK) { AddMessage(MessageSource.Info, "Cancelled select device."); return null; } return btDeviceDialog.SelectedDevice.DeviceAddress; }
/// <summary> /// 连接函数 /// </summary> /// <returns></连接失败返回false> private bool connect() { // 创建选择框选择要连接的设备 using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog()) { bldialog.ShowRemembered = false; if (bldialog.ShowDialog() == DialogResult.OK) { if (bldialog.SelectedDevice == null) { MessageBox.Show("No device selected"); return false; } BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice; if (!selecteddevice.Authenticated) { if (!BluetoothSecurity.PairRequest(selecteddevice.DeviceAddress, "0000")) { MessageBox.Show("PairRequest Error"); return false; } } try { // 对指定的目标进行蓝牙连接请求 client = new BluetoothClient(); client.Connect(selecteddevice.DeviceAddress, BluetoothService.SerialPort); stream = client.GetStream(); stream.Write(System.Text.Encoding.ASCII.GetBytes("teacher"), 0, 7); } catch { return false; } StateOfRunning(); stream = client.GetStream(); // 标记stream已经接受对象实例化 SIGN_STREAMSETUP = true; // 启动接收图片的线程 receiving = true; ReceiveThread = new System.Threading.Thread(ReceiveLoop); ReceiveThread.Start(); // 启动信息发送线程 sendDataThread = new System.Threading.Thread(SendDataLoop); closeSendData(); sendDataThread.Start(); sendfileThread = new System.Threading.Thread(SendFileLoop); return true; } return false; } }
// 连接电脑端蓝牙 private bool connect() { using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog()) { bldialog.ShowRemembered = false; if (bldialog.ShowDialog() == DialogResult.OK) { if (bldialog.SelectedDevice == null) { MessageBox.Show("No device selected"); return false; } BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice; if (!selecteddevice.Authenticated) { if (!BluetoothSecurity.PairRequest(selecteddevice.DeviceAddress, "0000")) { MessageBox.Show("PairRequest Error"); return false; } } try { client = new BluetoothClient(); client.Connect(selecteddevice.DeviceAddress, BluetoothService.SerialPort); stream = client.GetStream(); stream.Write(System.Text.Encoding.ASCII.GetBytes("student" + LOCALBLUETOOTHNAME), 0, 7 + LOCALBLUETOOTHNAME.Length); } catch { return false; } StateOfRunning(); stream = client.GetStream(); // 标记stream已经接受对象实例化 SIGN_STREAMSETUP = true; // 启动接收图片的进程 receiving = true; // sendfileThread = new System.Threading.Thread(SendFileLoop); return true; } return false; } }
private void btnBluetoothShare_Click(object sender, EventArgs e) { try { if (thrSend.IsAlive == true) { DialogResult dr = MessageBox.Show("Do you want to cancel the share?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { try { thrSend.Abort(); } catch { } backgroundWorker1.CancelAsync(); backgroundWorker1.Dispose(); this.progressBar1.Visible = false; btnBluetoothShare.Image = Properties.Resources.Bluetooth_icon.GetThumbnailImage(16, 16, null, new IntPtr()); TrayPopUp("File Share", "Bluetooth file sharing cancelled!", 10); return; } else return; } } catch { } if (PlayerEngine.isPlaying) { BluetoothRadio br = BluetoothRadio.PrimaryRadio; if (br != null) { if (br.Mode == RadioMode.PowerOff) { br.Mode = RadioMode.Connectable; } sbdd = new SelectBluetoothDeviceDialog(); sbdd.ShowAuthenticated = true; sbdd.ShowRemembered = true; sbdd.ShowUnknown = true; if (sbdd.ShowDialog() == DialogResult.OK) { btnBluetoothShare.Image = Properties.Resources.cancel_icon.GetThumbnailImage(16, 16, null, new IntPtr()); backgroundWorker1.WorkerSupportsCancellation = true; backgroundWorker1.RunWorkerAsync(); } } else { TrayPopUp("Error", "No bluetooth device found!\nEither your bluetooth device is OFF or not supported\nSupported devices are:\nMicrosoft, Widcomm/Broadcom, BlueSoleil, Bluetopia and few others", 10); // MessageBox.Show("No bluetooth device found!\nEither your bluetooth device is OFF or not supported\nSupported devices are:\nMicrosoft, Widcomm/Broadcom, BlueSoleil, Bluetopia and few others"); } } else { } }