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);

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值