2015-08-27 32 views
7

Ho un pulsante bluetooth dalle reti Radius. L'integrato - "aggiungi un dispositivo bluetooth" lo trova ogni volta.Bluetooth api per Surface Pro 3 Windows 8.1

Ho bisogno dell'API o di uno stack che posso utilizzare dalla mia app. Lo sto facendo in C#. la libreria 32 piedi non è compatibile

+0

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/Dn264587.aspx – ravenx30

risposta

4

Per enumerare i dispositivi RFCOMM Bluetooth collegati a un dispositivo, effettuare:

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

Per connettersi a un primo dispositivo della disposizione, fare:

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 
} 

Per inviare dati al dispositivo e leggere dati indietro, fare:

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 

Questo funzionerà per tutti RFCOMM (normale) dispositivi Bluetooth, se il dispositivo utilizza Bluetooth Low Energy, utilizzare le classi GATT corrispondenti.

+0

https://msdn.microsoft.com/en-us/library/windows.devices.bluetooth .genericattributeprofile.gattdeviceservice.aspx ancora una volta sembra che tu abbia sbagliato. Voglio questo al di fuori di uwp – Joe