Surface Pro 3 Windows 8.1 的蓝牙 API

Posted

技术标签:

【中文标题】Surface Pro 3 Windows 8.1 的蓝牙 API【英文标题】:Bluetooth api for surface pro 3 windows 8.1 【发布时间】:2015-11-22 08:59:03 【问题描述】:

我有一个来自 Radius 网络的蓝牙按钮。内置 - “添加蓝牙设备”每次都会找到它。

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

【问题讨论】:

msdn.microsoft.com/en-us/library/windows/apps/xaml/… 【参考方案1】:

要枚举连接到设备的 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 类。

【讨论】:

msdn.microsoft.com/en-us/library/… 看来你又是 uwp。我想在 uwp 之外进行此操作

以上是关于Surface Pro 3 Windows 8.1 的蓝牙 API的主要内容,如果未能解决你的问题,请参考以下文章

surface pro7装哪些软件

有人使用surface pro8成功运行雷电、逍遥、蓝叠、mumu等安卓模拟器吗?特别注意是pro8。

使用 Qt c++ 在 Windows Surface Pro 上获取加速度计数据?

微软surface pro 2可以玩平板电脑的游戏吗

surface pro5配置怎么样 surface pro5配置参数

surface pro4上网慢怎么回事