最近在弄蓝牙数据通信,有点小进展,写着以后当资料
蓝牙通信,我知道的能发送字节数据的有两种
1、需要配对的,然后将WINDOWS端蓝牙配置传入的COM串口,我们只要操作这个串口就可以数据的交互了
首先是配对,首先得注册一个事件
public EventHandler<BluetoothWin32AuthenticationEventArgs> handler;
BluetoothWin32Authentication authenticator;
handler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleRequests);
authenticator = new BluetoothWin32Authentication(handler);
在handleRequests里面处理配对的事件
public void handleRequests(Object thing, BluetoothWin32AuthenticationEventArgs args)
{
args.Pin //将这个pin值设置成你发起连接段一样的pin码,就能配对成功
}
然后连接
BluetoothClient bluetoothClient = new BluetoothClient();
bluetoothClient.Connect(bluetoothDevice.DeviceAddress, BluetoothService.Handsfree);
这样就连接上了
串口段的接收数据,也就是WINDOWS端
public void ReceivedComData(object sender, SerialDataReceivedEventArgs e)
{
try
{
int nCount = serialPort.BytesToRead;
if (nCount == 0)
{
return;
}
byte[] btAryBuffer = new byte[nCount];
serialPort.Read(btAryBuffer, 0, nCount);
string messageReceive = Encoding.UTF8.GetString(btAryBuffer);
this.Invoke(new EventHandler(delegate { textBox1.Text = messageReceive; }));
this.Invoke(new Action(() => { textBox1.Text = messageReceive; }));
string sendMessage = textBox1.Text;
btAryBuffer = Encoding.UTF8.GetBytes(sendMessage);
serialPort.Write(btAryBuffer, 0, btAryBuffer.Length);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
发送数据
public void SendComData(byte[] btSendArray)
{
if (serialPort.IsOpen)
{
try
{
serialPort.Write(btSendArray, 0, btSendArray.Length);
}
catch(Exception ec)
{
}
}
}
这样就可以相互通信了
2、不需要配对的,这时我们走Obex协议
Obex协议其实发送文件比较合适,因为它不需要配对
首先我们来一个监听类
private ObexListener listener;
初始化蓝牙适配器
InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Discoverable;
发送的关键代码
SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog();
sbdd.ShowAuthenticated = true;
sbdd.ShowRemembered = true;
sbdd.ShowUnknown = true;
if (sbdd.ShowDialog() == DialogResult.OK)
{
Cursor.Current = Cursors.WaitCursor;
//这个位置一般URI会配置设备地址跟文件名(发送文件),我们这边不要文件名
Uri uri = new Uri("obex://" + sbdd.SelectedDevice.DeviceAddress.ToString());ObexWebResponse response = null;
ObexWebRequest request = new ObexWebRequest(uri);
Stream stream = request.GetRequestStream();
byte[] btarray = new byte[1024];
btarray = Encoding.UTF8.GetBytes(msgSend.Text);
stream.Write(btarray, 0, btarray.Length);
response = request.GetResponse() as ObexWebResponse;
MessageBox.Show(response.StatusCode.ToString());
}
接收数据
首先开启监听
listener = new ObexListener(ObexTransport.Bluetooth);
listener.Start();
//开启接收线程
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(DealWithRequest));public void DealWithRequest()
{
while (listener.IsListening)
{
try
{
ObexListenerContext olc = listener.GetContext();
ObexListenerRequest olr = olc.Request;
这就是接收数据了
int len = (int)olr.InputStream.Length;byte[] btarray = new byte[len];
olr.InputStream.Read(btarray, 0, len);
//这种是接收文件直接保存
//string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' }));
//olr.WriteFile(Environment.SpecialFolder.MyDocuments + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename);
BeginInvoke(new MethodInvoker(delegate ()
{
//this.msgList.Items.Add("Received " + filename);
//因为我是用手机与之通信发送数据,所以用UTF8看个人平台,不然会有乱码的哦
this.msgList.Items.Add(Encoding.UTF8.GetString(btarray));}));
}
catch (Exception e)
{
BeginInvoke(new MethodInvoker(delegate ()
{
this.msgList.Items.Add(e.Message);
}));
continue;
}
}
}
大概也就这样,第二次发帖,写的不好,不要喷我哈