加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 用于Surface pro 3 windows 8.1的蓝牙API

发布时间:2020-12-15 07:56:34 所属栏目:百科 来源:网络整理
导读:我有Radius网络的蓝牙按钮.内置 – “添加蓝牙设备”每次都找到它. 我需要api或堆栈,我可以用我的应用程序做.我在c#中这样做.图书馆32英尺不兼容 解决方法 要枚举连接到设备的RFCOMM蓝牙设备,请执行以下操作: var DEVICE_ID = new Guid("{00000000-0000-000
我有Radius网络的蓝牙按钮.内置 – “添加蓝牙设备”每次都找到它.

我需要api或堆栈,我可以用我的应用程序做.我在c#中这样做.图书馆32英尺不兼容

解决方法

要枚举连接到设备的RFCOMM蓝牙设备,请执行以下操作:
var DEVICE_ID = new Guid("{00000000-0000-0000-0000-000000000000}"); //Enter your device's RFCOMM service id (try to find it on manufactorer's website
var services = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
        RfcommDeviceService.GetDeviceSelector(
            RfcommServiceId.FromUuid(DEVICE_ID)));

要连接到第一个可用设备,请执行以下操作:

if (services.Count > 0) 
{
   var service = await RfcommDeviceService.FromIdAsync(services[0].Id);
   //Open a socket to the bluetooth device for communication. Use the socket to communicate using the device's API
   var socket = new StreamSocket();
   await socket.ConnectAsync(service.ConnectionHostName,service.ConnectionServiceName,SocketProtectionLevel
                .BluetoothEncryptionAllowNullAuthentication); //Substitue real BluetoothEncryption
}

要将数据发送到设备并读取数据,请执行以下操作:

var BYTE_NUM = 64 as UInt32; //Read this many bytes
IInputStream input = socket.InputStream;
IOutputStream output = socket.OutputStream;
var inputBuffer = new Buffer();
var operation = input.ReadAsync(inputBuffer,BYTE_NUM,InputStreamOptions.none);
while (!operation.Completed) Thread.Sleep(200);
inputBuffer = operation.GetResults();
var resultReader = DataReader.FromBuffer(inputBuffer);
byte[] result = new byte[BYTE_NUM];
resultReader.ReadBytes(result);
resultReader.Dispose();
//Do something with the bytes retrieved. If the Bluetooth device has an api,it will likely specify what bytes will be sent from the device
//Now time to give some data to the device
byte[] outputData = Encoding.ASCII.GetBytes("Hello,Bluetooth Device. Here's some data! LALALALALA");
IBuffer outputBuffer = outputData.AsBuffer(); //Neat method,remember to include System.Runtime.InteropServices.WindowsRuntime
operation = output.WriteAsync(outputBuffer);
while (!operation.Completed) Thread.Sleep(200);
await output.FlushAsync(); //Now the data has really been written

这适用于所有RFCOMM(普通)蓝牙设备,如果您的设备使用蓝牙低功耗,请使用相应的GATT类.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读