一个基于32feet.net蓝牙封包的客户串口通信程序
蓝牙库程序下载:http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx
当连接成功,正常发送数据,此时另一方蓝牙关闭再打开,依然能发送成功。另一方却收不到数据。只能断开再连接才行。
哎,一直还没找到解决的办法。
Code
1
2 BluetoothEndPoint ePoint;
3 BluetoothAddress address;
4 private BluetoothClient btClient;
5 private BluetoothListener btListener;
6
7 private bool listening = true;
8
9 System.Threading.Thread t1;
10 //holds the incoming message
11 private string str;
12
13 public FrmMain()
14 {
15 InitializeComponent();
16 }
17
18 public void receiveLoop()
19 {
20
21 while (listening)
22 {
23 this.btClient.GetStream();
24
25 byte[] buffer1 = new byte[0x200];
26 Stream stream = this.btClient.GetStream();
27 int num1 = stream.Read(buffer1, 0, 0x200);
28 string str1 = System.Text.Encoding.ASCII.GetString(buffer1, 0, num1);
29 str = btClient.RemoteMachineName + "->" + str1;
30 //str = btClient.RemoteMachineName + "->" + ASCIIByteArrayToString(buffer1);
31
32 //txtMessagesArchive.Text += str;
33 if (!string.IsNullOrEmpty(str1))
34 {
35 this.Invoke(new EventHandler(UpdateTextBox));
36 }
37
38 }
39
40 }
41
42 private void WriteMessage(string mes)
43 {
44 txtMessagesArchive.Text += mes;
45 }
46
47
48 private void UpdateTextBox(object sender, EventArgs e)
49 {
50 txtMessagesArchive.Text += str;
51 }
52
53
54 private void FrmMain_Closing(object sender, CancelEventArgs e)
55 {
56 //停止接收
57 listening = false;
58 //停止监听服务
59 //btListener.Stop();
60 listening = false;
61 t1.Abort();
62 this.btClient.Dispose();
63 Application.Exit();
64 }
65 //发送
66 private void mnuSend_Click(object sender, EventArgs e)
67 {
68 if (listBox1.SelectedItem != null)
69 {
70 //sendMessage(MAX_TRIES, System.Text.Encoding.Unicode.GetBytes(txtMessage.Text), txtMessage.Text.Length * 2);
71 sendMessage1(System.Text.Encoding.Unicode.GetBytes(txtMessage.Text));
72 }
73 else
74 {
75 MessageBox.Show("请先择发送设备。");
76 }
77 }
78
79 private void sendMessage1( byte[] Buffer)
80 {
81 if (this.btClient.Connected)
82 {
83
84 NetworkStream stream = this.btClient.GetStream();
85 if (stream.CanWrite)
86 {
87 stream.BeginWrite(Buffer, 0, Buffer.Length, new AsyncCallback(OnAccept), null);
88 //stream.Seek(0, SeekOrigin.Begin);
89 //stream.Write(Buffer, 0, Buffer.Length);
90 }
91 //Stream stream = this.btClient.GetStream();
92 //stream.Write(Buffer, 0, Buffer.Length);
93 }
94 else
95 {
96 MessageBox.Show("请连接设备。");
97 }
98 }
99
100 void OnAccept(IAsyncResult ar)
101 {
102 str = "发送成功!";
103 this.Invoke(new EventHandler(UpdateTextBox));
104 }
105 //连接
106 private void mnuConnect_Click(object sender, EventArgs e)
107 {
108 if (listBox1.SelectedItem == null)
109 {
110 MessageBox.Show("请选择连接的设备!", "请选择连接的设备", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
111 }
112 else
113 {
114 ePoint = new BluetoothEndPoint(address, BluetoothService.SerialPort);
115 this.btClient = new BluetoothClient();
116 this.btClient.Connect(ePoint);
117 listening = true;
118 //receiveLoop();
119 t1 = new System.Threading.Thread(receiveLoop);
120 t1.Start();
121
122 if (this.btClient.Connected)
123 {
124 str = "连接成功!";
125 this.Invoke(new EventHandler(UpdateTextBox));
126 }
127
128 }
129 }
130
131 private void mnuExit_Click(object sender, EventArgs e)
132 {
133 Close();
134 }
135
136 //搜索
137 private void mnuSearch_Click(object sender, EventArgs e)
138 {
139 Cursor.Current = Cursors.WaitCursor;
140 //BluetoothDeviceInfo[] bdi = btClient.DiscoverDevices();
141 //cboDevices.DataSource = bdi;
142 //cboDevices.DisplayMember = "DeviceName";
143 if (btClient == null)
144 {
145 btClient = new BluetoothClient();
146
147 }
148 listBox1.DataSource = btClient.DiscoverDevices();
149 listBox1.DisplayMember = "DeviceName";
150
151
152 Cursor.Current = Cursors.Default;
153 //timer1.Enabled = true;
154 }
155
156
157 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
158 {
159 if (listBox1.SelectedItem != null)
160 {
161 address = ((BluetoothDeviceInfo)listBox1.SelectedItem).DeviceAddress;
162 }
163 }
164
165 private void button1_Click(object sender, EventArgs e)
166 {
167 txtMessagesArchive.Text = "";
168 }
169
170 //断开
171 private void mnuDisconnect_Click(object sender, EventArgs e)
172 {
173 listening = false;
174
175 this.btClient.Close();
176 this.btClient.Dispose();
177 t1.Abort();
178 Thread.Sleep(100);
179
180 str = "已断开连接!";
181 this.Invoke(new EventHandler(UpdateTextBox));
182 }
1
2 BluetoothEndPoint ePoint;
3 BluetoothAddress address;
4 private BluetoothClient btClient;
5 private BluetoothListener btListener;
6
7 private bool listening = true;
8
9 System.Threading.Thread t1;
10 //holds the incoming message
11 private string str;
12
13 public FrmMain()
14 {
15 InitializeComponent();
16 }
17
18 public void receiveLoop()
19 {
20
21 while (listening)
22 {
23 this.btClient.GetStream();
24
25 byte[] buffer1 = new byte[0x200];
26 Stream stream = this.btClient.GetStream();
27 int num1 = stream.Read(buffer1, 0, 0x200);
28 string str1 = System.Text.Encoding.ASCII.GetString(buffer1, 0, num1);
29 str = btClient.RemoteMachineName + "->" + str1;
30 //str = btClient.RemoteMachineName + "->" + ASCIIByteArrayToString(buffer1);
31
32 //txtMessagesArchive.Text += str;
33 if (!string.IsNullOrEmpty(str1))
34 {
35 this.Invoke(new EventHandler(UpdateTextBox));
36 }
37
38 }
39
40 }
41
42 private void WriteMessage(string mes)
43 {
44 txtMessagesArchive.Text += mes;
45 }
46
47
48 private void UpdateTextBox(object sender, EventArgs e)
49 {
50 txtMessagesArchive.Text += str;
51 }
52
53
54 private void FrmMain_Closing(object sender, CancelEventArgs e)
55 {
56 //停止接收
57 listening = false;
58 //停止监听服务
59 //btListener.Stop();
60 listening = false;
61 t1.Abort();
62 this.btClient.Dispose();
63 Application.Exit();
64 }
65 //发送
66 private void mnuSend_Click(object sender, EventArgs e)
67 {
68 if (listBox1.SelectedItem != null)
69 {
70 //sendMessage(MAX_TRIES, System.Text.Encoding.Unicode.GetBytes(txtMessage.Text), txtMessage.Text.Length * 2);
71 sendMessage1(System.Text.Encoding.Unicode.GetBytes(txtMessage.Text));
72 }
73 else
74 {
75 MessageBox.Show("请先择发送设备。");
76 }
77 }
78
79 private void sendMessage1( byte[] Buffer)
80 {
81 if (this.btClient.Connected)
82 {
83
84 NetworkStream stream = this.btClient.GetStream();
85 if (stream.CanWrite)
86 {
87 stream.BeginWrite(Buffer, 0, Buffer.Length, new AsyncCallback(OnAccept), null);
88 //stream.Seek(0, SeekOrigin.Begin);
89 //stream.Write(Buffer, 0, Buffer.Length);
90 }
91 //Stream stream = this.btClient.GetStream();
92 //stream.Write(Buffer, 0, Buffer.Length);
93 }
94 else
95 {
96 MessageBox.Show("请连接设备。");
97 }
98 }
99
100 void OnAccept(IAsyncResult ar)
101 {
102 str = "发送成功!";
103 this.Invoke(new EventHandler(UpdateTextBox));
104 }
105 //连接
106 private void mnuConnect_Click(object sender, EventArgs e)
107 {
108 if (listBox1.SelectedItem == null)
109 {
110 MessageBox.Show("请选择连接的设备!", "请选择连接的设备", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
111 }
112 else
113 {
114 ePoint = new BluetoothEndPoint(address, BluetoothService.SerialPort);
115 this.btClient = new BluetoothClient();
116 this.btClient.Connect(ePoint);
117 listening = true;
118 //receiveLoop();
119 t1 = new System.Threading.Thread(receiveLoop);
120 t1.Start();
121
122 if (this.btClient.Connected)
123 {
124 str = "连接成功!";
125 this.Invoke(new EventHandler(UpdateTextBox));
126 }
127
128 }
129 }
130
131 private void mnuExit_Click(object sender, EventArgs e)
132 {
133 Close();
134 }
135
136 //搜索
137 private void mnuSearch_Click(object sender, EventArgs e)
138 {
139 Cursor.Current = Cursors.WaitCursor;
140 //BluetoothDeviceInfo[] bdi = btClient.DiscoverDevices();
141 //cboDevices.DataSource = bdi;
142 //cboDevices.DisplayMember = "DeviceName";
143 if (btClient == null)
144 {
145 btClient = new BluetoothClient();
146
147 }
148 listBox1.DataSource = btClient.DiscoverDevices();
149 listBox1.DisplayMember = "DeviceName";
150
151
152 Cursor.Current = Cursors.Default;
153 //timer1.Enabled = true;
154 }
155
156
157 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
158 {
159 if (listBox1.SelectedItem != null)
160 {
161 address = ((BluetoothDeviceInfo)listBox1.SelectedItem).DeviceAddress;
162 }
163 }
164
165 private void button1_Click(object sender, EventArgs e)
166 {
167 txtMessagesArchive.Text = "";
168 }
169
170 //断开
171 private void mnuDisconnect_Click(object sender, EventArgs e)
172 {
173 listening = false;
174
175 this.btClient.Close();
176 this.btClient.Dispose();
177 t1.Abort();
178 Thread.Sleep(100);
179
180 str = "已断开连接!";
181 this.Invoke(new EventHandler(UpdateTextBox));
182 }