private void BthRecv(object peers) { byte[] buffer = new byte[1024]; Socket peer = (Socket)peers; while (true) { try { int n = peer.Receive(buffer); if (n == 0) { Info = "连接断开!"; BthListenBtn.Text = "连接断开"; return; } string data = Encoding.UTF8.GetString(buffer, 0, n); BthRecvBox.AppendText("Receiving data: " + data + "\r\n"); //SetRS(n, RecvOrSend.Recv); } catch//(Exception ex) { //MessageBox.Show(this, ex.Message, "error"); break; } } }
private void BthDispBtn_Click(object sender, EventArgs e) { if (br == null) { br = BluetoothRadio.PrimaryRadio; if (br == null) { BthRecvBox.AppendText("No radio hardware or unsupported software stack\r\n"); return; } // Warning: LocalAddress is null if the radio is powered-off. BthRecvBox.AppendText(String.Format("* Radio, address: {0:C}\r\n", br.LocalAddress)); BthRecvBox.AppendText("Mode: " + br.Mode.ToString() + "\r\n"); BthRecvBox.AppendText("Name: " + br.Name + ", LmpSubversion: " + br.LmpSubversion + "\r\n"); BthRecvBox.AppendText("ClassOfDevice: " + br.ClassOfDevice.ToString() + ", device: " + br.ClassOfDevice.Device.ToString() + " / service: " + br.ClassOfDevice.Service.ToString() + "\r\n"); // Enable discoverable mode br.Mode = RadioMode.Discoverable; BthRecvBox.AppendText("Radio Mode now: " + br.Mode.ToString() + "\r\n"); BthDispBtn.Text = "隐藏设备"; } else { br.Mode = RadioMode.PowerOff; br = null; BthRecvBox.AppendText("Blutooth Device is PowerDown!\r\n"); BthDispBtn.Text = "设备可见"; } }
private void BthListener() { BthClient = listener.AcceptBluetoothClient(); BthRecvBox.AppendText("Got a request!\r\n"); bthsocket = BthClient.Client; string dataToSend = "Hello from service!\r\n"; // Convert dataToSend into a byte array byte[] dataBuffer = Encoding.ASCII.GetBytes(dataToSend); // Output data to stream bthsocket.Send(dataBuffer); SetRS(dataBuffer.Length, RecvOrSend.Send); BthCon = new Thread(BthRecv); BthCon.IsBackground = true; BthCon.Start(bthsocket); }
private void StrSend(string Buf, bool IsBth) { if (BthCon != null && BthCon.IsAlive) { try { Encoding gb = Encoding.GetEncoding("gb2312"); BthRecvBox.AppendText(Buf); byte[] buffer = gb.GetBytes(Buf); bthsocket.Send(buffer); SetRS(buffer.Length, RecvOrSend.Send); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "error"); } } }
private void BthListenBtn_Click(object sender, EventArgs e) { if (BthCon == null && listener == null) { listener = new BluetoothListener(BluetoothService.SerialPort); listener.Start(); BthRecvBox.AppendText("Service started!\r\n"); Bthlisten = new Thread(BthListener); Bthlisten.IsBackground = true; Bthlisten.Start(); BthListenBtn.Text = "关闭连接"; } else if (BthCon == null) { } else if (BthCon.IsAlive) { BthClient.Close(); BthClient = null; BthCon.Abort(); BthCon = null; listener.Stop(); listener = null; BthListenBtn.Text = "监听"; } else if (BthCon != null) { BthCon = null; BthClient.Close(); BthClient = null; BthRecvBox.AppendText("Service started!\r\n"); Bthlisten = new Thread(BthListener); Bthlisten.IsBackground = true; Bthlisten.Start(); BthListenBtn.Text = "关闭连接"; } }