Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 适用于surface pro 3 windows 8.1的蓝牙api_C#_Windows_Windows 8_Bluetooth_Surface - Fatal编程技术网

C# 适用于surface pro 3 windows 8.1的蓝牙api

C# 适用于surface pro 3 windows 8.1的蓝牙api,c#,windows,windows-8,bluetooth,surface,C#,Windows,Windows 8,Bluetooth,Surface,我有一个来自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'

我有一个来自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 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类。

同样,您似乎是uwp。我想在uwp之外解决这个问题