蓝牙库程序下载:http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx
当连接成功,正常发送数据,此时另一方蓝牙关闭再打开,依然能发送成功。另一方却收不到数据。只能断开再连接才行。
哎,一直还没找到解决的办法。
![](https://images.cnblogs.com/cnblogs_com/hqm850/CEZoom0.JPG)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
BluetoothEndPoint ePoint;
3
BluetoothAddress address;
4
private BluetoothClient btClient;
5
private BluetoothListener btListener;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
private bool listening = true;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
System.Threading.Thread t1;
10
//holds the incoming message
11
private string str;
12
13
public FrmMain()
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
15
InitializeComponent();
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
public void receiveLoop()
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
while (listening)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
this.btClient.GetStream();
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
this.Invoke(new EventHandler(UpdateTextBox));
36
}
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
}
39
40
}
41![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
private void WriteMessage(string mes)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
44
txtMessagesArchive.Text += mes;
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
private void UpdateTextBox(object sender, EventArgs e)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
50
txtMessagesArchive.Text += str;
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
private void FrmMain_Closing(object sender, CancelEventArgs e)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
68
if (listBox1.SelectedItem != null)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
MessageBox.Show("请先择发送设备。");
76
}
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
private void sendMessage1( byte[] Buffer)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
81
if (this.btClient.Connected)
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
NetworkStream stream = this.btClient.GetStream();
85
if (stream.CanWrite)
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
MessageBox.Show("请连接设备。");
97
}
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
100
void OnAccept(IAsyncResult ar)
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
102
str = "发送成功!";
103
this.Invoke(new EventHandler(UpdateTextBox));
104
}
105
//连接
106
private void mnuConnect_Click(object sender, EventArgs e)
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
108
if (listBox1.SelectedItem == null)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
MessageBox.Show("请选择连接的设备!", "请选择连接的设备", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
111
}
112
else
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
if (this.btClient.Connected)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
str = "连接成功!";
125
this.Invoke(new EventHandler(UpdateTextBox));
126
}
127![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
}
129
}
130![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
131
private void mnuExit_Click(object sender, EventArgs e)
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
133
Close();
134
}
135![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
136
//搜索
137
private void mnuSearch_Click(object sender, EventArgs e)
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
139
Cursor.Current = Cursors.WaitCursor;
140
//BluetoothDeviceInfo[] bdi = btClient.DiscoverDevices();
141
//cboDevices.DataSource = bdi;
142
//cboDevices.DisplayMember = "DeviceName";
143
if (btClient == null)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
btClient = new BluetoothClient();
146
147
}
148
listBox1.DataSource = btClient.DiscoverDevices();
149
listBox1.DisplayMember = "DeviceName";
150![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
Cursor.Current = Cursors.Default;
153
//timer1.Enabled = true;
154
}
155![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
156![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
157
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
159
if (listBox1.SelectedItem != null)
160![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
161
address = ((BluetoothDeviceInfo)listBox1.SelectedItem).DeviceAddress;
162
}
163
}
164![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
165
private void button1_Click(object sender, EventArgs e)
166![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
167
txtMessagesArchive.Text = "";
168
}
169![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
170
//断开
171
private void mnuDisconnect_Click(object sender, EventArgs e)
172![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
173
listening = false;
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175
this.btClient.Close();
176
this.btClient.Dispose();
177
t1.Abort();
178
Thread.Sleep(100);
179![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
180
str = "已断开连接!";
181
this.Invoke(new EventHandler(UpdateTextBox));
182
}