Windows 8.1でWindows Runtimeについてもいくつか強化ポイントがありますが、そのなかの1つにBluetooth接続の強化があります。
Mindstorm EV3ともBluetoothで接続できるので試しにやってみたところうまく接続ができました。
変数宣言
private RfcommDeviceService DeviceService; private StreamSocket BTPort; private DataWriter BTWriter; private DataReader BTReader;
接続
public async Task Connect(string portName) { var servicesInfos = await DeviceInformation.FindAllAsync( RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); foreach (var serviceInfo in servicesInfos) { if (serviceInfo.Name == "EV3") { this.DeviceService = await RfcommDeviceService.FromIdAsync(serviceInfo.Id); break; } } this.BTPort = new StreamSocket(); await BTPort.ConnectAsync( DeviceService.ConnectionHostName, DeviceService.ConnectionServiceName, SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication); this.BTWriter = new DataWriter(BTPort.OutputStream); this.BTReader = new DataReader(BTPort.InputStream); }
コマンド送信
Byte[] mergedData = new Byte[header.Length + data.Length]; Array.Copy(header, mergedData, header.Length); Array.Copy(data, 0, mergedData, header.Length, data.Length); this.BTWriter.WriteBytes(mergedData); var sendResult = await this.BTWriter.StoreAsync();
Package.appxmanifestへの追記
<Capabilities> <Capability Name="internetClient" /> <m2:DeviceCapability Name="bluetooth.rfcomm"> <m2:Device Id="any"> <m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB"/> </m2:Device> </m2:DeviceCapability> </Capabilities>