一个基于32feet.net蓝牙封包的客户串口通信程序

蓝牙库程序下载:http://32feet.codeplex.com/SourceControl/ListDownloadableCommits.aspx

当连接成功,正常发送数据,此时另一方蓝牙关闭再打开,依然能发送成功。另一方却收不到数据。只能断开再连接才行。

哎,一直还没找到解决的办法。

 

ContractedBlock.gif ExpandedBlockStart.gif 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()
 14ExpandedBlockStart.gifContractedBlock.gif        {
 15            InitializeComponent();
 16        }

 17
 18         public void receiveLoop()
 19ExpandedBlockStart.gifContractedBlock.gif        {
 20
 21            while (listening)
 22ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 23                this.btClient.GetStream();
 24
 25                byte[] buffer1 = new byte[0x200];
 26                Stream stream = this.btClient.GetStream();
 27                int num1 = stream.Read(buffer1, 00x200);
 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))
 34ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 35                    this.Invoke(new EventHandler(UpdateTextBox));
 36                }

 37
 38            }
 
 39            
 40        }

 41
 42        private void WriteMessage(string mes)
 43ExpandedBlockStart.gifContractedBlock.gif        {
 44            txtMessagesArchive.Text += mes;
 45        }

 46
 47
 48        private void UpdateTextBox(object sender, EventArgs e)
 49ExpandedBlockStart.gifContractedBlock.gif        {
 50            txtMessagesArchive.Text += str;
 51        }

 52
 53
 54        private void FrmMain_Closing(object sender, CancelEventArgs e)
 55ExpandedBlockStart.gifContractedBlock.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)
 67ExpandedBlockStart.gifContractedBlock.gif        {
 68            if (listBox1.SelectedItem != null)
 69ExpandedSubBlockStart.gifContractedSubBlock.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
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 75                MessageBox.Show("请先择发送设备。");
 76            }

 77        }

 78
 79        private void sendMessage1( byte[] Buffer)
 80ExpandedBlockStart.gifContractedBlock.gif        {
 81            if (this.btClient.Connected)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 83
 84                NetworkStream stream = this.btClient.GetStream();
 85                if (stream.CanWrite)
 86ExpandedSubBlockStart.gifContractedSubBlock.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
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 96                MessageBox.Show("请连接设备。");
 97            }

 98        }

 99
100        void OnAccept(IAsyncResult ar)
101ExpandedBlockStart.gifContractedBlock.gif        {
102            str = "发送成功!";
103            this.Invoke(new EventHandler(UpdateTextBox));
104        }

105        //连接
106        private void mnuConnect_Click(object sender, EventArgs e)
107ExpandedBlockStart.gifContractedBlock.gif        {
108            if (listBox1.SelectedItem == null)
109ExpandedSubBlockStart.gifContractedSubBlock.gif            {
110                MessageBox.Show("请选择连接的设备!""请选择连接的设备", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
111            }

112            else
113ExpandedSubBlockStart.gifContractedSubBlock.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
122                if (this.btClient.Connected)
123ExpandedSubBlockStart.gifContractedSubBlock.gif                {
124                    str = "连接成功!";
125                    this.Invoke(new EventHandler(UpdateTextBox));
126                }

127
128            }

129        }

130
131        private void mnuExit_Click(object sender, EventArgs e)
132ExpandedBlockStart.gifContractedBlock.gif        {
133            Close();
134        }

135
136        //搜索
137        private void mnuSearch_Click(object sender, EventArgs e)
138ExpandedBlockStart.gifContractedBlock.gif        {
139            Cursor.Current = Cursors.WaitCursor;
140            //BluetoothDeviceInfo[] bdi = btClient.DiscoverDevices();
141            //cboDevices.DataSource = bdi;
142            //cboDevices.DisplayMember = "DeviceName"; 
143            if (btClient == null)
144ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
158ExpandedBlockStart.gifContractedBlock.gif        {
159            if (listBox1.SelectedItem != null)
160ExpandedSubBlockStart.gifContractedSubBlock.gif            {
161                address = ((BluetoothDeviceInfo)listBox1.SelectedItem).DeviceAddress;
162            }

163        }

164
165        private void button1_Click(object sender, EventArgs e)
166ExpandedBlockStart.gifContractedBlock.gif        {
167            txtMessagesArchive.Text = "";
168        }

169
170        //断开
171        private void mnuDisconnect_Click(object sender, EventArgs e)
172ExpandedBlockStart.gifContractedBlock.gif        {
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        }

转载于:https://www.cnblogs.com/hqm850/archive/2009/05/18/1459403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值