[C#] 編程控制筆記本藍牙與外部藍牙設備通信
C#編程使筆記本藍牙和外部設備藍牙通信:
其實配對以後,藍牙就被模擬成了一個端口,我們可以用最簡單的端口通訊來收發信息。首先,在每次啟動時,需要連接端口:
[FORM初始化時獲取所有的COM口,並加入下拉列表]
復制代碼
1 public Form1()
2 {
3 InitializeComponent();
4
5 //Get all port list for selection
6 //獲得所有的端口列表,並顯示在列表內
7 PortList.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 }
復制代碼
[連接按鈕事件:選中list中的被選中的COM口進行連接,如果連接成功就在狀態欄顯示藍牙連接成功]
復制代碼
1 private void ConnectButton_Click(object sender, EventArgs e)
2 {
3 if (!BluetoothConnection.IsOpen)
4 {
5 //Start
6 Status = "正在連接藍牙設備";
7 BluetoothConnection = new SerialPort();
8 ConnectButton.Enabled = false;
9 BluetoothConnection.PortName = PortList.SelectedItem.ToString();
10 BluetoothConnection.Open();
11 BluetoothConnection.ReadTimeout = 10000;
12 BluetoothConnection.DataReceived += new SerialDataReceivedEventHandler(BlueToothDataReceived);
13 Status = "藍牙連接成功";
14 }
15 }
復制代碼
[藍牙接收數據事件響應函數,在按鈕連接事件中聲明的該事件,用於響應藍牙數據接收]
復制代碼
1 private void BlueToothDataReceived(object o, SerialDataReceivedEventArgs e)
2 {
3 //int length = BluetoothConnection.ReadByte();
4 Thread.Sleep(1000);
5 int length = 13;
6 BlueToothReceivedData = DateTime.Now.ToLongTimeString() + "\r\n";
7 BlueToothReceivedData += "收到字節數:" + length + "\r\n";
8
9 byte[] data = new byte[length];
10 BluetoothConnection.Read(data,0,length);
11 for (int i = 0; i < length; i++)
12 {
13 BlueToothReceivedData += string.Format("data[{0}] = {1}\r\n", i, data[i]);
14 }
15 //receive close message
16 if (length == 3 && data[0] == 255 && data[1] == 255 && data[2] == 255)
17 {
18 //Stop
19 Status = "正在斷開藍牙設備";
20 BluetoothConnection.Close();
21 BluetoothConnection.Dispose();
22 BluetoothConnection = null;
23 ConnectButton.Enabled = true;
24 Status = "藍牙斷開成功";
25 }
26 }
復制代碼
這裡第4行讓程序休息1是因為延時等待從設備把數據發送完全。
這裡為了方便我嚴格控制讓發送數據為13Byte。
從設備發送的13Byte數據送至緩沖區,PC端C#程序通過read()函數將緩沖區數據接收到data中,下面是格式輸出一下數據。
[發送數據函數]
復制代碼
1 private void BlueToothDataSend(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 //BluetoothConnection.Write(readData, 0, length + 2);
12 BluetoothConnection.Write(data, 0, 1);
13 //Status = "發送數據字節數:" + length;
14 }
復制代碼
本來是將data[]數據發送出去,因為我從設備設置為只要有數據發送過來就做出響應發送13Byte數據,所以就直接將data的第一byte發送出去了。
[定時器函數:用於刷新狀態欄,和接收數據顯示]
1 private void MonitorTimer_Tick(object sender, EventArgs e)
2 {
3 StatusMessage.Text = Status;
4 BlueToothMessage.Text = BlueToothReceivedData;
5 }
[發送數據按鈕:將SendMessage中的數據獲得發送出去]
復制代碼
1 private void SendButton_Click(object sender, EventArgs e)
2 {
3 byte n;
4 byte.TryParse(SendMessage.Text, out n);
5
6 BlueToothDataSend(new byte[] { n });
7 }