This article walks you through basic implementation of a UWP App with a virtual joystick that can be used to control the Arduino/RPi based projects over bluetooth. Whenever the joystick is moved, the app sends the new joystick position to the connected device. See it in action.
xmlns:JoystickControl="using:JoystickUserControl"
<
JoystickControl:Joystick
x:Name=”MyJoystick”>< /
>
For more info on joystick control, check this article.
Our app must have the following basic features,
Bluetooth device capability must be checked in Package.appxmanifest file under capabilities tab for an app that uses bluetooth.
public
async Task<DeviceInformationCollection> FindPairedDevicesAsync()
{
var aqsDevices = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(aqsDevices);
return
devices;
}
async Task<
bool
> ConnectAsync(
DeviceInformation
device)
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(device.Id);
if
(rfcommService ==
null
)
false
;
Stream =
new
StreamSocket();
try
await Stream.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName);
catch
IsConnected =
true
_btReader =
DataReader(Stream.InputStream);
_btWriter =
DataWriter(Stream.OutputStream);
_btWriter.UnicodeEncoding = UnicodeEncoding.Utf8;
> WriteAsync(
string
str)
(!IsConnected)
var n = _btWriter.WriteString(str);
await _btWriter.StoreAsync();
n > 0;
(Exception)