c#蓝牙连接

一.蓝牙收索
 1. 首先创建一个蓝牙对象
   BluetoothClient Blueclient =new BluetoothClient();
 2.用一个键值对保存收索到的蓝牙地址
   Dictionary<string,BluetoothAddress> deviceAddresses=new Dictionary<string,BluetoothAddress>();
 3.设置蓝牙无线电状态
   BluetoothRadio BlueRadio=BluetoothRadio.PrimaryRadio;
   BlueRadio.Mode=RadioMode.Connectable   //可连接的
 4.监听服务,即监听附近的蓝牙设备并保存
   BluetoothDeviceInfo[] Devices=Blueclient.DiscoverDevices();
 5.将监听到的服务添加到deviceAddresses保存。并与蓝牙的名字相对应。
   divceAddresses[device.DeviceName]=device.DeviceAddress;
二.连接蓝牙
 1.设置匹配码,从txtPwd获取,并去掉前后空白字符Trim()
   Blueclient.SetPin(DeviceAddress, txtPwd.Text.Trim())
 2.连接蓝牙,对不同的设备有不同的服务类型,要匹配
   Blueclient.Connect(DeviceAddress,BluetoothSevice.SerialPort);
三.发送信号
 1.获取蓝牙数据流
   System.IO.Stream stream=Blueclient.GetStream();
  有string型信号message
 2.将string型信号message转换成字节数组,再发送
   byte[] by=System.Text.Encoding.ASCII.GetBytes(message);
   stream.Write(by,0,by.Length);

猜你喜欢

转载自985359995.iteye.com/blog/1969488