Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Comunicando nuestras apps con el mundo exterior

1,793 views

Published on

En esta charla vemos varias manera de conectar un dispositivo Windows Phone con una Raspberry Pi así como con un ordenador Windows 8 usando WebSockets, StreamSockets y finalmente, bluetooth

Published in: Software
  • Be the first to comment

  • Be the first to like this

Comunicando nuestras apps con el mundo exterior

  1. 1. Comunicandonuestras appscon el mundo exterior Roberto Luis Bisbé rlbisbe.net @rlbisbe
  2. 2. @rlbisbe •Ingeniero en Informática @ UAM •Desarrollador @ frontiers •WP, W8 & Android dev •rlbisbe.net
  3. 3. Corría el año2006…
  4. 4. Corría el año2006…
  5. 5. Penetrómetro
  6. 6. Penetrómetro
  7. 7. .NET Compact Framework .NET Framework USB RemoteAPI
  8. 8. Comunicarnuestras appscon el mundo exterior
  9. 9. Imagen: https://flic.kr/p/aKN6u6 Examinandola superficie
  10. 10. RaspberryPI •ARM •Linux •USB •Ethernet •Sensores
  11. 11. RaspberryPI •ARM •Linux •USB •Ethernet •Sensores
  12. 12. API REST!
  13. 13. Web Sockets
  14. 14. ! Web Socket •SignalR •.NET •socket.io •node.js •Python •faye •ruby
  15. 15. Qué es WebSocket •Bidireccional •HTTP •Soporte nativo en 8.x
  16. 16. Servidor varserver = http.createServer(app) server.listen(1430) varwss= new WebSocketServer({ server: server }); wss.on("connection", function(ws) { ws.on("message", function(data) { ws.send("response"); }) ws.on("close", function() { //Closed }) }
  17. 17. Servidor varserver = http.createServer(app) server.listen(1430) varwss= new WebSocketServer({ server: server }); wss.on("connection", function(ws) { ws.on("message", function(data) { ws.send("response"); }) ws.on("close", function() { //Closed }) }
  18. 18. Servidor varserver = http.createServer(app) server.listen(1430) varwss= new WebSocketServer({ server: server }); wss.on("connection", function(ws) { ws.on("message", function(data) { ws.send("response"); }) ws.on("close", function() { //Closed }) }
  19. 19. Cliente -Conexión varwebSocket= new MessageWebSocket(); webSocket.Control.MessageType= SocketMessageType.Utf8; webSocket.MessageReceived+= ProcessMessageReceived; webSocket.Closed+= Closed; awaitwebSocket.ConnectAsync(new Uri("ws://IP:9999"));
  20. 20. Cliente -Conexión ver webSocket= new MessageWebSocket(); webSocket.Control.MessageType= SocketMessageType.Utf8; webSocket.MessageReceived+= ProcessMessageReceived; webSocket.Closed+= Closed; awaitwebSocket.ConnectAsync(new Uri("ws://IP:9999"));
  21. 21. Cliente -Conexión ver webSocket= new MessageWebSocket(); webSocket.Control.MessageType= SocketMessageType.Utf8; webSocket.MessageReceived+= ProcessMessageReceived; webSocket.Closed+= Closed; awaitwebSocket.ConnectAsync(new Uri("ws://IP:9999"));
  22. 22. Cliente -Conexión ver webSocket= new MessageWebSocket(); webSocket.Control.MessageType= SocketMessageType.Utf8; webSocket.MessageReceived+= ProcessMessageReceived; webSocket.Closed+= Closed; awaitwebSocket.ConnectAsync(new Uri("ws://IP:9999"));
  23. 23. Cliente -Envío y recepción var_messageWriter= new DataWriter(webSocket.OutputStream); _messageWriter.WriteString(message); await_messageWriter.StoreAsync(); varstream = new StreamReader(args.GetDataStream().AsStreamForRead()); stringmessage= stream.ReadToEnd();
  24. 24. Cliente -Envío y recepción var_messageWriter= new DataWriter(webSocket.OutputStream); _messageWriter.WriteString(message); await_messageWriter.StoreAsync(); varstream = new StreamReader(args.GetDataStream().AsStreamForRead()); stringmessage= stream.ReadToEnd();
  25. 25. Ejemplo completo DEMO C# Node JS WebSocket
  26. 26. WebSocket •Bidireccional •HTTP •Soporte nativo en 8.x
  27. 27. Bajando de nivel Imagen: https://flic.kr/p/aKN6u6
  28. 28. StreamSockets •Flujo de datos sobre TCP •Texto, imágenes, audio, vídeo… •Permisos
  29. 29. Servidor -Conexión varlistenSocket= new StreamSocketListener(); listenSocket.ConnectionReceived+= ConnectionReceived; awaitlistenSocket.BindServiceNameAsync("5000");
  30. 30. Servidor -Recepción DataReaderreader= new DataReader(connectionSocket.InputStream); uintsizeFieldCount= awaitreader.LoadAsync(sizeof(long)); if(sizeFieldCount!= sizeof(long)) return; uintresultLength= (uint)reader.ReadInt32(); uintactualResultLength= awaitreader.LoadAsync(resultLength); if(resultLength!= actualResultLength) return; varbyteResult= new byte[resultLength]; reader.ReadBytes(byteResult);
  31. 31. Servidor -Recepción DataReaderreader= new DataReader(connectionSocket.InputStream); uintsizeFieldCount= awaitreader.LoadAsync(sizeof(long)); if(sizeFieldCount!= sizeof(long)) return; uintresultLength= (uint)reader.ReadInt32(); uintactualResultLength= awaitreader.LoadAsync(resultLength); if(resultLength!= actualResultLength) return; varbyteResult= new byte[resultLength]; reader.ReadBytes(byteResult);
  32. 32. Servidor -Recepción uintsizeFieldCount= awaitreader.LoadAsync(sizeof(long)); if(sizeFieldCount!= sizeof(long)) return; uintresultLength= (uint)reader.ReadInt32(); uintactualResultLength= awaitreader.LoadAsync(resultLength); if(resultLength!= actualResultLength) return; varbyteResult= new byte[resultLength]; reader.ReadBytes(byteResult);
  33. 33. Cliente -Conexión varconnectionSocket= new StreamSocket(); varserverHost= new HostName(server); awaitconnectionSocket.ConnectAsync(serverHost, port);
  34. 34. Cliente -Envío varwriter= new DataWriter(connectionSocket.OutputStream); writer.WriteInt32(bytes.Length); writer.WriteBytes(bytes); awaitwriter.StoreAsync(); writer.DetachStream();
  35. 35. Cliente -Envío varwriter= new DataWriter(connectionSocket.OutputStream); writer.WriteInt32(bytes.Length); writer.WriteBytes(bytes); awaitwriter.StoreAsync(); writer.DetachStream();
  36. 36. Cliente -Envío varwriter= new DataWriter(connectionSocket.OutputStream); writer.WriteInt32(bytes.Length); writer.WriteBytes(bytes); awaitwriter.StoreAsync(); writer.DetachStream();
  37. 37. Ejemplo completo DEMO WinRTC# WinRTC# Socket
  38. 38. StreamSockets •Flujo de datos sobre TCP •Texto, imágenes, audio, vídeo… •Requiere permisos
  39. 39. En las profundidades Imagen: https://flic.kr/p/48XdaQ
  40. 40. PebbleSmartwatch •iOS & Android •APIs •SDK •Bluetooth!
  41. 41. Las cosas no siempre salen como esperamos Imagen: https://flic.kr/p/8bDAS1
  42. 42. Volvamos a la RaspberryPI •ARM •Linux •USB •Ethernet •Sensores
  43. 43. Volvamos a la RaspberryPI •ARM •Linux •USB •Ethernet •Sensores
  44. 44. Volvamos a la RaspberryPI •ARM •Linux •USB + Bluetooth •Ethernet •Sensores
  45. 45. Bluetooth IS EVIL •P2P •Alcance •Consumo •Servicios •Ancho de banda •Pairing, Búsqueda, Conexión
  46. 46. Servidor server_sock=BluetoothSocket( RFCOMM ) server_sock.bind(("",22)) server_sock.listen(22) client_sock, client_info= server_sock.accept() whileTrue: data = client_sock.recv(1024) client_sock.close() server_sock.close()
  47. 47. Servidor server_sock=BluetoothSocket( RFCOMM ) server_sock.bind(("",22)) server_sock.listen(22) client_sock, client_info= server_sock.accept() whileTrue: data = client_sock.recv(1024) client_sock.close() server_sock.close()
  48. 48. Servidor server_sock=BluetoothSocket( RFCOMM ) server_sock.bind(("",22)) server_sock.listen(22) client_sock, client_info= server_sock.accept() whileTrue: data = client_sock.recv(1024) client_sock.close() server_sock.close()
  49. 49. Cliente -Búsqueda chatServiceInfoCollection= awaitDeviceInformation.FindAllAsync( RfcommDeviceService.GetDeviceSelector( RfcommServiceId.SerialPort));
  50. 50. Cliente –Conexión varchatServiceInfo= chatServiceInfoCollection[0]; varservice= awaitRfcommDeviceService.FromIdAsync(chatServiceInfo.Id); awaitchatSocket.ConnectAsync( service.ConnectionHostName, service.ConnectionServiceName); varchatWriter= new DataWriter(chatSocket.OutputStream); varchatReader= new DataReader(chatSocket.InputStream);
  51. 51. Cliente –Conexión varchatServiceInfo= chatServiceInfoCollection[0]; varservice= awaitRfcommDeviceService.FromIdAsync(chatServiceInfo.Id); awaitchatSocket.ConnectAsync( service.ConnectionHostName, service.ConnectionServiceName); varchatWriter= new DataWriter(chatSocket.OutputStream); varchatReader= new DataReader(chatSocket.InputStream);
  52. 52. Cliente –Conexión varchatServiceInfo= chatServiceInfoCollection[0]; varservice= awaitRfcommDeviceService.FromIdAsync(chatServiceInfo.Id); awaitchatSocket.ConnectAsync( service.ConnectionHostName, service.ConnectionServiceName); varchatWriter= new DataWriter(chatSocket.OutputStream); varchatReader= new DataReader(chatSocket.InputStream);
  53. 53. Cliente –Envío y recepción chatWriter.WriteString("open"); awaitchatWriter.StoreAsync(); byte[] buffer = new byte[10]; awaitchatSocket.InputStream.ReadAsync( buffer.AsBuffer(), 10, InputStreamOptions.Partial); stringresult= System.Text.Encoding.UTF8.GetString(buffer, 0, 10);
  54. 54. Cliente –Envío y recepción chatWriter.WriteString("open"); awaitchatWriter.StoreAsync(); byte[] buffer = new byte[10]; awaitchatSocket.InputStream.ReadAsync( buffer.AsBuffer(), 10, InputStreamOptions.Partial); stringresult= System.Text.Encoding.UTF8.GetString(buffer, 0, 10);
  55. 55. Ejemplo completo DEMO C# Python Bluetooth Serial Port
  56. 56. Bluetooth IS STILL EVIL •P2P •Alcance •Consumo •Servicios •Ancho de banda •Pairing, Búsqueda, Conexión
  57. 57. Podemos comunicarnuestras apps con el mundo exterior
  58. 58. WebSockets, sockets, bluetooth, GPS, infrarrojos, sonido, NFC, sensores
  59. 59. Preguntas? Hay regalitos…
  60. 60. Gracias! Enlaces, código y slidesen rlbisbe.net roberto.luis@rlbisbe.net @rlbisbe

×