How do I bind to a WiFi direct discovered service in Android - android

I am able to discover local devices via WiFi P2P that advertise an Android service that I am interested in binding to. Once those services are discovered, how do I bind a client to them?
In other words, I'd like to have a client send a Message object to a bound service running on a different device, have that device handle the Message and potentially respond.

The Android documentation has a training exercise on Wifi P2P here and specifically has code to connect to a peer here

Related

Android bluetooth connections issues

I have confusion on bluetooth connections.
Preface: when connecting devices for android over bluetooth you can connect as server or as client
My understand: connecting as the server essentially means that your device will be hosting the connection. Connecting as client means that you are being hosted by another device - and/or the device you are connecting to is the host.
I am trying to build an app that can connect via bluetooth and control a device. My first use case is my TV. so I want to make my app that can connect to my TV and control it as a remote. My initial thought is that the TV would act as the server. If I am connecting to another phone then I would imagine that my device would need to be the server. In most cases, if I want my app to act as a controller to send control signals to the device it connects to - is it correct to assume that my device is client or server?
Assuming that you mean Bluetooth Low Energy (BLE), then there are two separate types of roles that dictate how communication happens in Android:-
Central vs Peripheral - This has to do with the connection establishment and maintenance as follows:-
Peripheral device is the one responsible for advertising its presence as well as accepting incoming connections. Sensors usually fall into this category.
Central device is the one responsible for scanning and establishing the connection with the remote peripheral device. Phones usually fall into this category.
GATT client vs GATT server - This dictates how communication is handled when that connection is made:-
GATT server is the device that hosts all the data. Again, sensors usually fall into this category.
GATT client is the device that reads the data from the GATT server. Again, phones and computers usually take the GATT client role.
The two types of roles are separate (i.e. a Central can be a GATT server or a GATT client, and vice versa), but in the majority of the cases, the central is the GATT client.
And to finally answer your question, I would make the remote controller to be both the central and the GATT client. This way your TV would always be advertising its presence and hosting the date, while the remote scans/connects and reads/writes to that data.
You can find more information in the links below:-
The ultimate guide to Android BLE development
Bluetooth Low Energy: A primer
Android BLE examples
Client because when is server tv, you can connect from more clients (mobiles).
When is server mobile you can controll more tvs, but tv can connect only to defined mobiles.

Android wifi device communication on same router

I am working on an application, where I need to send and receive message on device connected with same router.
Initially i used peer to peer for this. and it is working fine. But my client requirement changed now. he don't want wifi P2P. He want direct communication between device that are connected on same wifi connection or router.
So Is it possible to send and get text between two device connected on same wifi network?
Why not leverage sockets? There is a library called AndroidAsync by koush that will let you create sockets that will listen for data?
It is possible, unless your access point doesn't use a Client Isolation mode, when clients of the same WiFi AP cannot communicate with each other.
Client isolation has been used by the public WiFi APs quite frequently.

How to know target device in WiFi direct?

I am trying to connect to my device from another device and send a simple string via wifi direct.
I was able to get the list of peers available however how do I know that this is my device that I need to send the info to?
The documention at https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html outlines this scenerio.
With peer discovery using discoverPeers(WifiP2pManager.Channel, WifiP2pManager.ActionListener), an application discovers the neighboring peers, but has no good way to figure out which peer to establish a connection with. For example, if a game application is interested in finding all the neighboring peers that are also running the same game, it has no way to find out until after the connection is setup. Pre-association service discovery is meant to address this issue of filtering the peers based on the running services.
Try using addLocalService(), so you peer devices can discover your "simple string" p2p service.

What to do after service discovery using wifi direct on android?

I implemented service discovery over wifi-direct and I am able to successfully discover the service using http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html . But I don't know what to do now? How do I connect to the service? the way I connect to other peers? how do I use a service over wifi-direct? thanks.
After you've discovered the service you should be able to connect to it as if it were a regular WifiP2P client. From there, you can get the IP address of the group owner (which may be either device) and set up a connection (over a Socket, for example).

Android Bluetooth

If I wanted to connect to another device via bluetooth how would I do it?
Nothing I've done seems to work. The phone I'm trying to connect to is non android and has bluetooth on and discoverable.
Bluetooth API is realy poor in this case... let's have look at java-doc:
Hint: If you are connecting to a
Bluetooth serial board then try using
the well-known SPP UUID
00001101-0000-1000-8000-00805F9B34FB.
However if you are connecting to an
Android peer then please generate your
own unique UUID.
from: link text
According to this, your peer should be visible under the 00001101-0000-1000-8000-00805F9B34FB service record.
Now, if you want to create an app which need to connect to Bluetooth devices, I advice you to use Cordova platform and install a plugin that let you connect via bluetooth, list devices, send data, disconnect etc...
In Bluetooth, there is a concept of service profile for an application layer. An application on a peer 1 willing to talk to same application on peer 2 must be discoverable and connectable, the master initiating the connection.
The application is given a unique service/application identifier so that peers can discover each other. If you write a custom application yourself, you are expected to create a new UUID so that the corresponding SDP protocol can perform the necessary discovery.
Instead of connecting with brute force using the BT_ADDR, you would essentially initiate connection via graceful service discovery and then connection follows once the underlying UUID is identified. This, of course, assumes that you would have done the pairing process early, or that your lower layer stack will include pairing process as well.

Categories

Resources