C# 如何接收蓝牙字符数据

fanslion 2014-06-24 11:43:24
公司买了个蓝牙吊秤,想将重量传到系统,我在网上找了一些资料,有以下一段代码:
设备是之前与蓝牙匹配了的,可以找到
蓝牙GUID也是正确的

但在接收数据时,在这句代码停住了,也不报其它错误
bluetoothClient = bluetoothListener.AcceptBluetoothClient();

应该是不能连接蓝牙
不知道有没有大神做过类似的程序,帮忙指点一二
感激不尽!!


BluetoothListener bluetoothListener ;
Thread listenThread;
BluetoothClient bluetoothClient = new BluetoothClient();
bool isConnected;

public bool OpenPort()
{
InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;
InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();
InTheHand.Net.Sockets.BluetoothDeviceInfo[] devices = cli.DiscoverDevices();
foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices) //设备搜寻
{
device.Update();
device.Refresh();
if (device.DeviceName == "BF10-A")
{
this.textBox2.Text = "设备已找到";
break;
}
}

Thread ReceiveThread = new Thread(ReceiveData);
ReceiveThread.Start();
return true;
}

public void ReceiveData()
{
#region

try
{
Guid mGUID = Guid.Parse("00001101-0000-1000-8000-00805f9b34fb");
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
isConnected = true;
}
catch (Exception ex)
{
isConnected = false;
MessageBox.Show(ex.ToString());
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
Stream peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
textBox1.Text = receive;
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
#endregion
}
...全文
1378 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jqj2013 2015-08-16
  • 打赏
  • 举报
回复
串口监听就行~还是不明白的联系我 求356873548
jqj2013 2015-08-16
  • 打赏
  • 举报
回复
不用这样,直接用串口通讯就行啊。http://download.csdn.net/detail/jqj2013/8900495 看这个资源,如果是计算机的话,更简单。都不用IntheHand
bznfeng 2015-08-15
  • 打赏
  • 举报
回复
大哥,你这程序后来是怎么实现的,最近我也在做这个功能,求指教
save4me 2014-06-26
  • 打赏
  • 举报
回复
那有没有确认设备已经配对,并且连接上了?
引用 6 楼 fanslion 的回复:
跟厂家要了一个程序,连接上了就自动接收数据 这蓝牙吊秤是自动发送数据的
fanslion 2014-06-25
  • 打赏
  • 举报
回复
引用 3 楼 Z65443344 的回复:
设备厂家没给通信协议?确定不需要你先发送什么请求,设备就直接不停给你传数据? 按理说这类设备应该都是作为从站,只有主站发送指令才会有响应. 否则如果你买了2个秤,还必须弄两个带蓝牙的电脑去读数据?
跟厂家要了一个程序,连接上了就自动接收数据 这蓝牙吊秤是自动发送数据的
於黾 2014-06-25
  • 打赏
  • 举报
回复
设备厂家没给通信协议?确定不需要你先发送什么请求,设备就直接不停给你传数据? 按理说这类设备应该都是作为从站,只有主站发送指令才会有响应. 否则如果你买了2个秤,还必须弄两个带蓝牙的电脑去读数据?
mjp1234airen4385 2014-06-25
  • 打赏
  • 举报
回复
估计bluetoothListener.AcceptBluetoothClient(); 这个会阻塞当前的线程。 要写在线程里面,等有蓝牙客户端连接时,就会触发bluetoothListener.AcceptBluetoothClient(); 的
save4me 2014-06-25
  • 打赏
  • 举报
回复
也可以参考一下开源项目32feet.NET中关于蓝牙连接的介绍General Bluetooth Data Connections
save4me 2014-06-25
  • 打赏
  • 举报
回复
代码里面有发现其他蓝牙设备的代码,但是好像没有看到蓝牙设备配对的代码,发送接收数据是以连接上两台设备为基础的 摘自C#实现电脑蓝牙接收手机信息并处理

             BluetoothDeviceInfo bd = new BluetoothDeviceInfo(devices[0].DeviceAddress);            
             bluetoothClient = new BluetoothClient();
             Guid mGUID = Guid.Parse("fa87c0d0-afac-11de-8a39-0800200c9a66");            
             bluetoothClient.Connect(devices[0].DeviceAddress, mGUID);//客户端对地址实现连接,这是一个阻塞线程,需要服务器端的回应 
             Thread ReceiveThread = new Thread(ReceiveData);           
             ReceiveThread.Start();
devmiao 2014-06-24
  • 打赏
  • 举报
回复
客户端有没有匹配成功?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧