关于蓝牙通讯报 The operation is not allowed on non-connected sockets. 错误
来源:互联网 发布:淘宝用网银怎么支付 编辑:程序博客网 时间:2024/06/04 20:02
我在用PDA设备连接蓝牙称进行通讯,总是时不时的报 The operation is not allowed on non-connected sockets. ,后来加了一个Thread.Sleep(500); 解决了此问题
根据我的分析,原因是蓝牙创建连接后,停止半秒后,可以避免此错误。
大 致代码如下:
protected BluetoothDeviceInfo device = null; protected BluetoothClient client = null; Stream peerStream = null; ///蓝牙名 string blueName = BGLms.Handset.Code_JM.Configuration.XmlConfiguration.Instance.BlueName; ///蓝牙密码 string bluePwd = BGLms.Handset.Code_JM.Configuration.XmlConfiguration.Instance.BluePwd;
/// <summary> /// Conn连接蓝牙 /// </summary> public void loadConnectService() { try { if(client==null) client = new BluetoothClient(); BluetoothDeviceInfo[] devices = client.DiscoverDevices(); //获取蓝牙设备扫描的所有服务湍 foreach (BluetoothDeviceInfo d in devices) { if (d.DeviceName == blueName) { device = d; break; } } // device = devices[0]; if (!device.Connected)//创建客户端连接对象 { client.SetPin(device.DeviceAddress, bluePwd); //密码 client.Connect(device.DeviceAddress, BluetoothService.SerialPort); } } catch (Exception ex) { if (peerStream != null) { peerStream.Dispose(); } if (client != null) { client.Close(); } MessageBox.Show("Error:" + ex.Message); } }
<p> /// <summary> /// 获取数据 /// </summary> private void GetMac() { StringBuilder resultdata = new StringBuilder(); //string errorMsg = ""; try { int I = 0; Thread.Sleep(500); //关键代码 if (peerStream == null) peerStream = client.GetStream(); #region MyRegion while (true) //因为我这边蓝牙服务端是连续发送模式,所以通过此方法获取最新的通讯数据; { byte[] buffer = new byte[4096]; // Read Data if (peerStream.CanRead) { peerStream.Read(buffer, 0, 4096); // Convert Data to String string data = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, 4096); data = data.Substring(1, data.Length - 1); string[] arraystr = data.Split('='); if (arraystr.Length < 512) //判断是不否读完 { for (int i = arraystr[arraystr.Length - 2].Length - 1; i >= 0; i--) { resultdata.Append(arraystr[arraystr.Length - 2][i].ToString()); } break; } } if (I > int.MaxValue) { break; } I++; } #endregion } catch (Exception ex) { if (peerStream != null) { peerStream.Close(); peerStream.Dispose(); } if (client != null) { client.Close(); client.Dispose(); } return; }</p><p> }</p>
搜索
搜索
0 0
- 关于蓝牙通讯报 The operation is not allowed on non-connected sockets. 错误
- 关于VMware提示there is not enough space on the file system for the selected operation
- license security violation. operation is not allowed
- license security violation operation is not allowed
- 错误:Operation not allowed after ResultSet closed
- 错误:Operation not allowed after ResultSet closed
- 解决loadrunner 破解错误:license security violation.Operation is not allowed
- 解决loadrunner 破解错误:license security violation.Operation is not allowed
- Delphi - 关于错误E2002 File Type is not allowed here
- jenkins中配置maven一直报is not a directory on the Jenkins master错误
- Eclipse 中的关于The processing instruction target matching _[xX][mM][lL]_ is not allowed的错误
- 关于Operation not allowed after ResultSet closed错误的一种解决方式
- Chrome报表单提交错误,Form submission canceled because the form is not connected
- SQL0668N Operation not allowed for reason code "7" on table
- Linux shell报错: ** is not allowed to run sudo on **
- 解决“ license security violation.Operation is not allowed ”问题
- jquery中.on()或者.off()报is not function错误
- 错误提示 the operation is not applicable to the current selection.Select a field which is not declared a
- apache防止目录列表漏洞
- 螺旋队列问题
- Java实现二叉搜索树节点的删除
- Activity与Fragment数据传递之Fragment之间获取数据
- jQuery入门:选择元素 Selecting Elements
- 关于蓝牙通讯报 The operation is not allowed on non-connected sockets. 错误
- word文档转成pdf实用的方法
- 保存信息到手机里
- Sql Server Job 常用方法
- MFC 窗口自适应分辨率
- 分析 mysql_fetch_assoc()方法参数类型错误的警告信息 的出现原因
- mogodb
- 模式识别算法-支持向量机SVM 2
- C/C++中善用大括号