簡體   English   中英

用於Surface pro 3 windows 8.1的藍牙API

[英]Bluetooth api for surface pro 3 windows 8.1

我有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類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM