[C #] programming to control the communication between a laptop bluetooth device and an external bluetooth device
C # programming enables Bluetooth communication between laptops and external devices: in fact, after pairing, Bluetooth is simulated as a port. We can use the simplest port communication to send and receive information. First, you need to connect to the Port during each startup: [get all comports during FORM initialization, and add them to the drop-down list] Copy code 1 public Form1 () 2 {3 InitializeComponent (); 4 5 // Get all port list for selection 6 // obtain the list of all ports and display 7 PortList in the list. items. clear (); 8 string [] Ports = SerialPort. getPortNames (); 9 10 for (int I = 0; I <Ports. length; I ++) 11 {12 string s = Ports [I]. toUpper (); 13 Regex reg = new Regex ("[^ COM \ d]", RegexOptions. ignoreCase | RegexOptions. multiline); 14 s = Reg. replace (s, ""); 15 16 PortList. items. add (s); 17} 18 if (Ports. length> 1) PortList. selectedIndex = 1; 19} copy the Code [connection button event: select the selected comport in the list to connect, if the connection is successful, the Bluetooth connection is displayed on the status bar.] copy the Code 1 private void ConnectButton_Click (object sender, EventArgs e) 2 {3 if (! BluetoothConnection. isOpen) 4 {5 // Start 6 Status = "connect to Bluetooth device"; 7 connecting thconnection = new SerialPort (); 8 ConnectButton. enabled = false; 9 then thconnection. portName = PortList. selectedItem. toString (); 10 then thconnection. open (); 11 initiate thconnection. readTimeout = 10000; 12 listen thconnection. dataReceived + = new SerialDataReceivedEventHandler (incluthdatareceived); 13 Status = "Bluetooth connection successful"; 14} 15} copy the Code [Bluetooth receives data event response function, which is declared in the button connection event to respond to Bluetooth data reception] Copy code 1 private void incluthdatareceived (object o, serialDataReceivedEventArgs e) 2 {3 // int length = descrithconnection. readByte (); 4 Thread. sleep (1000); 5 int length = 13; 6 then threceiveddata = DateTime. now. toLongTimeString () + "\ r \ n"; 7 bytes th1_eddata + = "number of received Bytes:" + length + "\ r \ n "; 8 9 byte [] data = new byte [length]; 10 bytes thconn Ection. read (data, 0, length); 11 for (int I = 0; I <length; I ++) 12 {13 bytes th1_eddata + = string. format ("data [{0}] = {1} \ r \ n", I, data [I]); 14} 15 // receive close message16 if (length = 3 & data [0] = 255 & data [1] = 255 & data [2] = 255) 17 {18 // Stop19 Status = "disconnecting the bluetooth device"; 20 then thconnection. close (); 21 then thconnection. dispose (); 22 fig = null; 23 ConnectButton. enabled = True; 24 Status = "Bluetooth disconnected successfully"; 25} 26} copy the code. Here, line 1 breaks the program. 1 is delayed because the data is completely sent from the device. Here, we strictly control the sending data to 13 bytes for convenience. 13 bytes of data sent from the device are sent to the buffer zone. The pc c # program receives data from the buffer zone through the read () function. below is the output data in the format. [Data sending function] Copy code 1 private void incluthdatasend (byte [] data) 2 {3 // int length = data. length; 4 // byte [] readData = new byte [length + 2]; 5 // readData [0] = (byte) (length % 255 ); 6 // readData [1] = (byte) (length/255); 7 // for (int I = 0; I <length; I ++) 8 // {9 // readData [I + 2] = data [I]; 10 //} 11 // descrithconnection. write (readData, 0, length + 2); 12 rows thconnection. write (data, 0, 1); 13 // S Tatus = "sent data bytes:" + length; 14} copy the code to send data [] data, because I set the device to send 13 bytes of data as long as there is data sent, the first byte of data is sent directly. [Timer function: Used to refresh the status bar and display received data] 1 private void MonitorTimer_Tick (object sender, EventArgs e) 2 {3 StatusMessage. text = Status; 4 receive thmessage. text = fig eddata; 5} [send data button: Get and send the data in SendMessage] Copy code 1 private void SendButton_Click (object sender, EventArgs e) 2 {3 byte n; 4 byte. tryParse (SendMessage. text, out n); 5 6 then thdatasend (new byte [] {n}); 7}