async void OnConnectionReceived(StreamSocketListener listener, StreamSocketListenerConnectionReceivedEventArgs args) { try { Debug.WriteLine("Received."); provider.StopAdvertising(); await listener.CancelIOAsync(); StreamSocket socket = args.Socket; Debug.WriteLine("Start connection."); var input_stream = socket.InputStream; while (true) { Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(1024); var n = await input_stream.ReadAsync(buffer, buffer.Capacity, Windows.Storage.Streams.InputStreamOptions.Partial); byte[] bytes = buffer.ToArray(); String text = System.Text.Encoding.UTF8.GetString(bytes, 0, (int)n.Length); Debug.WriteLine($"Received: {text}"); ShowNotification("app_name", "title", text); } } catch (Exception error) { Debug.WriteLine(error.ToString()); } }
async void OnConnectionReceived(StreamSocketListener listener, StreamSocketListenerConnectionReceivedEventArgs args) { try { Log("Accepted."); provider.StopAdvertising(); await listener.CancelIOAsync(); StreamSocket socket = args.Socket; Log("Start connection."); var input_stream = socket.InputStream; while (true) { Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(1024); var n = await input_stream.ReadAsync(buffer, buffer.Capacity, Windows.Storage.Streams.InputStreamOptions.Partial); byte[] bytes = buffer.ToArray(); String text = System.Text.Encoding.UTF8.GetString(bytes, 0, (int)n.Length); Log($"Received: {text}"); var notificationData = JsonConvert.DeserializeObject <NotificationData>(text); ShowNotification(notificationData.app_name, notificationData.title, notificationData.content); } } catch (Exception error) { Log(error.ToString()); } }
public async Task <byte[]> ReadAsync() { var buffer = new Windows.Storage.Streams.Buffer(1024); var bytesRead = await this.serialDevice.InputStream.ReadAsync(buffer, buffer.Capacity, Windows.Storage.Streams.InputStreamOptions.None); return(buffer.ToArray()); }
public static IAsyncOperation <TetheringState> SendBluetooth(DeviceInformation dev, TetheringState state) { return(AsyncInfo.Run(async(cancel) => { for (var tryout = 10; tryout > 0; tryout--) { try { var selector = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(ServiceUuid)); var devices = await DeviceInformation.FindAllAsync(selector); var service = devices.SingleOrDefault(d => d.Id.StartsWith(dev.Id, StringComparison.OrdinalIgnoreCase)); if (service == null) { throw new Exception("Tethermote Service not found"); } using (var socket = await ConnectDevice(service)) { using (var outstream = socket.OutputStream) { await outstream.WriteAsync(new byte[] { (byte)state }.AsBuffer()); } using (var instream = socket.InputStream) { var buf = new Windows.Storage.Streams.Buffer(1); var red = (await instream.ReadAsync(buf, 1, Windows.Storage.Streams.InputStreamOptions.Partial)).Length; if (red == 1) { return (TetheringState)buf.ToArray()[0]; } Debug.WriteLine("No data"); } } } catch (Exception ex) { Debug.WriteLine(ex); try { await PingDevice(dev.Id); } catch (Exception e2) { Debug.WriteLine(e2); } if (tryout != 1) { await Task.Delay(100); } } } return TetheringState.Error; })); }
private async Task Receive() { while (true) { IBuffer inbuffer = new Windows.Storage.Streams.Buffer(4); await _socket.InputStream.ReadAsync(inbuffer, 4, InputStreamOptions.None); int command = BitConverter.ToInt32(inbuffer.ToArray(), 0); //The inbuffer at this point contains: "\u0001\0\0\0", and the BitConverter resolves this to 1 inbuffer = new Windows.Storage.Streams.Buffer(4); await _socket.InputStream.ReadAsync(inbuffer, 4, InputStreamOptions.None); int length = BitConverter.ToInt32(inbuffer.ToArray(), 0); //The inbuffer now contains: "2\0\0\0", and the BitConverter resolves this to "50" inbuffer = new Windows.Storage.Streams.Buffer((uint)length); await _socket.InputStream.ReadAsync(inbuffer, (uint)length, InputStreamOptions.Partial); string path = Encoding.UTF8.GetString(inbuffer.ToArray()); } }