Skip to content

Instantly share code, notes, and snippets.

@peterfoot
Created April 23, 2018 09:34
Show Gist options
  • Save peterfoot/ea07a026a6c9d978fd0f27c2431bc745 to your computer and use it in GitHub Desktop.
Save peterfoot/ea07a026a6c9d978fd0f27c2431bc745 to your computer and use it in GitHub Desktop.
Sample Bluetooth printing from Unity
var devices = DeviceInformation.FindAll(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
var deviceInfo = devices[0]; // this makes some assumptions about your paired devices so really the results should be enumerated and checked for the correct device
var device = BluetoothDevice.FromDeviceInformation(deviceInfo);
var serResults = device.GetRfcommServices(BluetoothCacheMode.Cached);
foreach(RfcommDeviceService serv in serResults.Services)
{
if(serv.ServiceId == RfcommServiceId.SerialPort)
{
var stream = serv.OpenStream();
byte[] buff = System.Text.Encoding.ASCII.GetBytes("Testing\r\n");
stream.Write(buff, 0, buff.Length);
stream.Flush();
stream.Close();
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment