Witam wszystkich bardzo serdecznie.
Stworzyłem wg. różnych poradników bibliotekę bluetooth:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Devices.Enumeration;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
// ########################################################################## //
namespace WPBTController {
class BTConnector {
DeviceInformationCollection devices;
bool IsConnected = false;
StreamSocket Stream = null;
DataReader _btReader = null;
DataWriter _btWriter = null;
// ------------------------------------------------------------------ //
public BTConnector() { }
// ------------------------------------------------------------------ //
public async Task<DeviceInformationCollection> FindPairedDevicesAsync() {
var aqsDevices = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
this.devices = await DeviceInformation.FindAllAsync(aqsDevices);
return this.devices;
}
// ------------------------------------------------------------------ //
public async Task<bool> ConnectAsync(DeviceInformation device) {
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(device.Id);
if (rfcommService == null) { return false; }
StreamSocket Stream = new StreamSocket();
try {
await Stream.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName);
} catch (Exception ex) {
return false;
}
IsConnected = true;
_btReader = new DataReader(Stream.InputStream);
_btWriter = new DataWriter(Stream.OutputStream);
_btReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
_btWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
return true;
}
// ------------------------------------------------------------------ //
public async Task<bool> WriteAsync(string str) {
if (!IsConnected) { return false; }
try {
var n = _btWriter.WriteString(str);
await _btWriter.StoreAsync();
return n > 0;
} catch (Exception ex) {
return false;
}
}
// ------------------------------------------------------------------ //
public async Task<String> ReadAsync() {
if (!IsConnected) { return null; }
var receivedStrings = "";
try {
uint size = await _btReader.LoadAsync(sizeof(uint));
if (size < sizeof(uint)) {
Disconnect();
return null;
}
while (_btReader.UnconsumedBufferLength > 0)
{
uint bytesToRead = _btReader.ReadUInt32();
receivedStrings += _btReader.ReadString(bytesToRead) + "\n";
}
return receivedStrings;
} catch (Exception ex) {
return null;
}
}
// ------------------------------------------------------------------ //
private async void Disconnect() {
IsConnected = false;
try {
await Stream.CancelIOAsync();
Stream.Dispose();
Stream = null;
Stream.Dispose();
Stream = null;
} catch (Exception ex) {
MainPage.MessageBox( "Disonnection Failed", ex.Message, "OK" );
}
}
// ------------------------------------------------------------------ //
}
}
// ########################################################################## //
I nie mam pojęcia jak zaimplementować to Page'a tak aby działało
(chodzi mi o zwykły komunikator WindowsMobile < - - - > Bluetooth <serial> Arduino <serial> PC)
i tak aby sterować arduino przez polecenia String ( /polecenie )
public async Task<DeviceInformationCollection> FindPairedDevicesAsync() to ComboBox
public async Task<bool> WriteAsync(string str) from TextBox
public async Task<String> ReadAsync() to TextBlock
Dopiero zaczynam zabawę z UWP i nie mam na razie za bardzo zaawansowanego pojęcia dot. wątków a tym bardziej dla mnie magia to sockety.