9

When you work with Bluetooth or WiFi Direct in Android, at the end of all of the handshaking and such, you wind up with sockets.

With TCP/IP, we have a zillion-plus-one libraries that layer on top of sockets, for high-level protocols: HTTP, XMPP, IMAP, etc. Courtesy of these libraries, we can deal with more domain-specific abstractions of an operation (e.g., "download this file"), with low-level socket plumbing handled by the library.

Question: Are there equivalents, for any high-level protocol, that are known to work (or are likely to work) with the sockets produced via Android's Bluetooth and/or WiFi Direct layers?

Right now, I'm not fussy about the specific protocol -- I'm just looking for examples of this sort of protocol layer, to make using these sorts of connectivity options easier for developers.

For example, it looks like I could create a fork or add-on for OkHTTP that uses an alternative source for sockets, and I could probably create a Java HTTP server that does the same. Given those, app developers would write HTTP apps that talked over Bluetooth or WiFi Direct (and, on the client side at least, the coding should be fairly "natural" in feel, once the connectivity-specific pairing and handshaking has gone on).

IOW, going back to dealing with raw sockets feels so two decades ago... :-)

Thanks!


UPDATE

Based on Kristopher Micinski's comment on the ZeroMQ answer, I figured some clarification might be in order.

It's easier to say what I don't want: I don't want to touch sockets, after creating them. Something else at a higher level should handle those for me, plus handle what I'd consider a "protocol" to be (e.g., determining when some communications operation has ended, beyond a socket closing).

Mostly, this is for my book. Most book examples for low-level socket stuff are unrealistic, such as "we open a socket to the server and immediately start blasting the bytes representing some image to be uploaded, then close the socket when we're done". While the examples work, you'd never write something like that in real life:

  • If you're really working at the socket level, you'd be implementing some protocol that has the hopes of addressing authentication, error handling, etc., even if you're rolling the protocol yourself

  • Few developers work directly with sockets today for Internet operations

Now, it'd be cool if the protocol offered by the layer were something developers were used to (e.g., HTTP) or had heard of even if they haven't used it (e.g., XMPP). And I'll settle for simple scenarios (e.g., N-way support is cool but not necessary). In this respect, based on preliminary research (conducted by a sleep-deprived brain), ZeroMQ isn't a bad option. It lacks a bit of "brand recognition" compared to, say, an XMPP stack that could work with arbitrary sockets. But off the cuff it seems to meet what else I'm looking for.

I recognize that these stacks will have limitations imposed by the underlying transport (e.g., Bluetooth works well for N-way only for small values of N). And I certainly don't want to portray -- here or in my book -- that whatever solution I portray is the be-all and end-all of socket based communication.

I just want something that has a prayer of being more realistic for actual use. Bonus points if it is something that I can grok, as I have always used higher-level protocols for TCP/IP communications, and so I'm short on experience with direct socket manipulation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491

2 Answers2

2

I found ZeroMQ to be useful for managing socket connection. They have a support in multiple languages which includes JAVA. You may use this to manage the sockets once you establish the connection over wifi-direct or BT.

Sukumar
  • 1,303
  • 10
  • 15
  • This has possibilities, though it still feels a bit low-level, at least at first glance. Let me do some more research on this one. Many thanks! – CommonsWare Jun 11 '13 at 11:33
  • ZeroMQ is a good conduit in the area of message queues, but doesn't specifically address the hell that ensues when using Bluetooth / WifiDirect. ZeroMQ is also most useful for when you want to roll your own protocol. I think @CommonsWare wants something where you can do the opposite: shove Android's stuff under existing things. (Still, ZeroMQ rocks!) – Kristopher Micinski Jun 11 '13 at 13:16
  • @KristopherMicinski: I elaborated on my question in hopes of addressing your points. – CommonsWare Jun 11 '13 at 13:28
  • @Sukumar: OK, I now understand ZeroMQ a bit more. However, it appears that their Java layer is just a JNI bridge to their native implementation. Since their transports are implemented in native code, I fail to see how we can pass in a Java socket (that we get from Bluetooth or WiFi Direct) and have it be managed by ZeroMQ. Have you done this already? If so, do you have any tips? Thanks! – CommonsWare Jun 11 '13 at 23:16
  • @CommonsWare Yes, JAVA layer is a bridge to their native implementation. But, we dont need to worry about it and also about creating a java socket connection. ZeroMQ takes care about Socket creation and communication based on three patterns (Publisher-subscriber, Messaging, Request-Reply). Use whichever suits your requirements. Please have look into this link, this explains all three supported patterns. http://fireinside.me/post/3551747763/zeromq-messaging-patterns – Sukumar Jun 12 '13 at 09:55
  • @Sukumar: "ZeroMQ takes care about Socket creation" -- that is the problem. ZeroMQ does not know how to create a `BluetoothSocket` on Android. I might be able to get ZeroMQ to work with WiFi Direct though. – CommonsWare Jun 12 '13 at 10:47
  • @CommonsWare Apology. I'm not aware of BluetoothSocket on Android. I used ZeroMQ in Wifi-Direct p2p network and found it to be useful. Did you find any java Socket class related to wifi-direct just as BluetoothSocket on Android? – Sukumar Jun 12 '13 at 11:27
  • @Sukumar: It looks like WiFi Direct will use regular sockets, just with a particular host and port, so there's a chance that ZeroMQ will work with that. I'll spend some time on it in the upcoming days. – CommonsWare Jun 12 '13 at 11:40
0

I know it's a somewhat old question and already answered but I would like to contribute.

I did this app: https://play.google.com/store/apps/details?id=com.budius.WiFiShoot and although the WiFi Direct connection n handshake is somewhat broken and it's what causes most of my unhappy users, I'm handling all the communication using the excellent https://github.com/EsotericSoftware/kryonet

and my code is pretty much what you see on their examples, create kryo, register classes, open server, connect client to server IP and shoot objects across with the file information and later I shoot the actual files using this code https://code.google.com/p/kryonet/source/browse/trunk/kryonet/test/com/esotericsoftware/kryonet/InputStreamSenderTest.java

hope it helps.

Budius
  • 39,391
  • 16
  • 102
  • 144