Android Wi-Fi Direct send data before connection - android

I'm developing an app which uses Wi-Fi direct to connect devices. I read how to connect, send data and disconnect the devices here: http://developer.android.com/guide/topics/connectivity/wifip2p.html. Now, I'd like to send some data BEFORE the connection, in order to show some informations about the device. I've Googled this but the only answer I've found is this: Sending data in Android WiFi Direct service discovery instead of connecting, which uses services discovery.
Is there a way I can send data before the pairing process without using the service discovery? If I can't, can you explain what is the difference between service discovery and normal Wi-Fi P2P?
Thank you.

Unless you have some other medium you can rely on (e.g. some sort of back-end you can use to exchange information), you will need Service Discovery.
I have already written a few answers regarding WifiP2p and Service Discovery, and I suggest you take a look at them first. The last one should probably interest you the most.
Why is discovering peers for Android WifiDirect so unreliable
Wi-fi P2P. Inform all peers available of some event
Wifi P2P service discovery works intermittently
To answer your question about the difference between WifiP2p and ServiceDiscovery, let me use a small analogy:
Imagine you're at the airport, you've just landed in a foreign
country. You're leaving the duty-free zone and you see all the people
that are waiting for people who have landed at the airport just like
you did. You need to find your taxi driver. You know he should be
waiting for you with a small piece of paper with your name on it. You
can see many guys like that, every single one of them is holding a
piece of paper with a name on it. You need to read through many of
them until you find the one with yours. But you don't have to talk to
all those people. You will talk only to your taxi driver, and you will
probably tell much more than just your name.
Now let me explain... Every single person holding a piece of paper represents a Broadcasted Service. Every broadcasted service can carry a small piece of information (a person name). The space for the information is very limited (you can't write much on the paper), and is "relatively permanent" (it takes a bit to change it). When you were searching for the right person, you were performing a Service Discovery. You did not have to talk to all those people to read the names on those pieces of papers - you didn't have to connect to them (WiFiP2p-wise). When you found the one you were interested in, you talked to the man - you established a WiFiP2p connection. Now you can talk to him as much as you want - you can pass as much data as you want through the connection.

Related

How to set up One to Many connections without any user interactions

We found multiple ways to connect two device with a Peer-to-Peer connection using things like Bluetooth and Wifi direct. Problem is, each device need the user to accept or confirm the connection.
We would like to make a One to Many system where everyone can connect to one device. This device need to manage all the connections incoming without any user interaction (really independant) and then display in our activity some informations receive from all the connected device.
We're looking for some good examples on how we can do this. Thanks
Cant you just use basic TCP Server?
EDIT/Answer to comment:
Yes it is possible, here is one of my sample apps, tho servers can be created on ports above 1024

BLE challenge response smartphone authentication

I want to build a simple challenge response protocol in order to authenticate a user to an Arduino via the smartphone (Android and iOS) using BLE. I'm having problems seeing how this would be implemented with BLE. What I want to do:
Arduino sends/broadcasts a challenge
Smartphone receives challenge and computes response
Smarpthone sends response to Arduino
The way I thought about implementing this is to have the Arduino (peripheral and server) start advertising when a button is pressed. Ideally an event would be triggered on the smartphone (central and client) when a certain service shows up. Then the smartphone connects to Arduino, reads the challenge characteristic and writes the response to it. Is this a good approach?
It appears though that it is not possible to trigger an event when a BLE service appears and I would continuously need to scan with the smartphone which is very battery intensive. Not sure how to get around this? Geofencing is not really an option since it can be inside. Maybe detecting if the user is walking, biking, standing still? Any suggestions are greatly appreciated =).
On iOS BLE connectivity looks like this:
initialize CBluetoothManager,
start discovering devices through bluetooth.
when found particular peripheral that u're intrested in, connect to it via core manager and store this peripheral variable in ur Class,
then.. u should STOP discovering cuz it's terrible for batteries ;) (as u said),
after device's found, you can search through services, characteristic and at the end through descriptors <-- IN THIS ORDER otherwise u won't get any of these,
ofc, from now on you're able to store each of these vars in your Class and work with them as you wish to. BLE on iOS's based on delegates and it's own lifecycle, each time when there will be something to read the event will trigger. When you will want to send some msg to Arduino back it shouldn't be a problem. In the partial answer for your question about
'when a certain service shows up. THEN the smarphone connects to
Arduino.'
you have to make steps ABOVE in order to read services. Hope that helps.

Data transfer via bluetooth between paired Android and Raspberry PI

I am working on a project where I need to transfer data between android and raspberry pi via Bluetooth. However, I am new to this and I don't have deep understanding on what happens when two devices are paired. Based on assumption that the two devices of interest are already paired, where would the starting point be for programming for such task? I've been reading on BluetoothSocket, but I am still unsure of where to start. Can anyone help me please?
Thank you so much in advance!
I have been looking into this same issue, here is the reading I found on my end. I was looking to specifically code in python so that's the angle of the first one, the second is C++, but has a really thorough intro.
https://people.csail.mit.edu/albert/bluez-intro/index.html
and this one is really good too, the intro isnt too dense:
http://beej.us/guide/bgnet/output/html/multipage/index.html
The specific parts to look at involve the planning aspect. The intro of the beej programming guide shows specifically what sockets are and how they fit in a network sense. This means streaming sockets and datagram sockets. It also shows which of the sockets are used and which are availible. Chapters 1 through 3 gave me a solid enough reading basis to use the second document to determine a few things.
Chapter 2 of the MIT document goes into specific detail about each of the steps that must occur at a structural level including L2CAP + UDT, RCOMM, and whatever the stream one was. From reading these I was able to determine that the network I wanted to use was an L2CAP. I hope these help point you in the right direction though as far as what network you want to setup and what language you want to program in.
I've been working on the same task little while ago. The point is, that in order to start sending and receiving data you have to establish connection first. There is two side- device which connects (creates socket) and the other receiving connection (bluetooth server socket), giving out connection once connection is complete. After connection is established, you should stop receiving incoming connection or attempting establishing any connections.
From implementation perspective, you should implement few threads for managing all those stages - connection thread, accept thread, communucation thread. There is a great example from Google: https://github.com/googlesamples/android-BluetoothChat . It uses that technic. The only drawback is that it uses Handler (Android feature, allowing thereads to communicate) for informing user about Bluetooth events. I modified it a bit by introducing another thread, receiving status updates and calling methods from callback interface, feel free to use code from project: https://github.com/AlexShutov/LEDLights.

Android WiFi (sending and receiving data without specifying recipient)

I want to make an application is via WiFi would send and receive data without sending them to a specific recipient. I explained to the example of a simple chat: The user sends a message via WiFi, but without a specific destination. As a result, his report available to all users nearby. Similarly other users can receive and send messages. I also want to realize the function of repeater, where each user's device not only receives a message from the other devices, and retransmits them to others allowing increase the coverage area of WiFi connection.
But at the moment it is not possible to send data without a specific recipient. The same WiFi Direct necessarily require pairing before data transfer. Can not specify that the recipient is unknown.
Recently I heard the news about the technology WiFi Aware, which involves greater interaction between WiFI devices. Please reply, WiFi Aware allows to do what I described above? Namely - the transfer of data and we will without a clear indication of the recipient and the need to do additional work (eg preliminary pairing between devices).
If WiFi Aware allows to do what I described - when it is expected to release its API? In particular, for Android devices. And if not - you can tell me how to implement what I need? And in general - it is technically possible?
Thank you for your time and sorry for my bad English.
You should try to look into nsd via wifi-direct. This Network Service discovery via Wifi-Direct. What this helps you to achieve is broadcast information to all listening devices.
On the device which wants to convey the information you addLocalService and on the other devices you should discoverServices. In the ServiceInfo object you can the pass the information you want to communicate to the other devices who are listening for services. Please try to study from the link attached above.
I am not very sure about how repeater will work with this.

Android Bluetooth Chat sample app - multiple connections

Is it possible to set up the Android Bluetooth Chat sample app to connect more than one person at a time, and have a mini chat room? What would that entail?
tl;dr version: Bluetooth sucks for this, don't use it, use wifi instead, probably backed by a web backend.
I have investigated this issue thoroughly throughout the years in the interests of a social wireless network research project. My general advice is: it doesn't work with more than two / three people. Bluetooth just isn't designed with wireless peer to peer networks in mind.
In general, it seems that the cheap Bluetooth controllers included on Android devices (especially HTC's devices, iirc) don't really handle any more than two or three connections at a time. I'm unsure if this is a hardware or firmware problem, but I can recount some basic anecdotes. I was working to implement this idea at the SDK level (i.e., without firmware modifications) around the beginning of 2011, and was able to get a peer to get two additional connections (i.e., three devices, each connecting to the other two) to work for a period of a few minutes to an hour before the connections would suddenly die and the socket would become unusable, requiring reconnection. Unfortunately, 20 minutes was an upper bound, and generally it was impossible to get connections to more than one other device at all reliably.
The goal of the project was to support multiple people interacting with each other silently in the background, but this never materialized, instead we ditched Bluetooth and went with wifi instead, which worked much much better. In the abstract, I think people view Bluetooth as a possible medium for reliable peer to peer communication, but it wasn't really designed that way: it's more of a medium used for short range communication between small devices (think headsets).
Be aware that if you want to do this, the maximum number of devices to which you can connect is fixed, because as per the Bluetooth spec, a piconet supports a maximum of seven devices. (See the wikipedia article.)
The required change is simple: you use a different UUID for each device. This can be implemented a number of ways, using an out of band exchange mechanism, or simple scheme where you assign UUIDs in an increasing fashion and when connecting to the network, try each in succession.
Here are some relevant Google groups threads:
Bluetooth peer to peer networks
Multiple connections on Android Bluetooth
I remember posting a more elaborate one detailing how to do this (with code) that I might dig up as well.., if I can find it. It should be from late 2010 or early 2011.
So the answer is, in the abstract, yes, you can try to do this, by using multiple UUIDs (after you use one, that's it, and you have to try another using some assignment protocol). However, in practice, after a lot of trial and error, this doesn't really work for what you probably want to use it for, and it's a lot better to go with an internet backend instead. By the way, this is also good for another reason, most users don't really like to turn on their Bluetooth for fear of their battery being drained..
Leaving this here, in case it helps someone else.
I was able to make my custom chat room following official bluetooth tutorial and modifying it a little.
Unfortunately, I cannot provide most of my code, but main idea is:
Every device is acts both as server and as a client. When Chat is started, device starts its server thread. Server thread is the same as official but doesn't ends when accept connection. It just keep listening.
Client thread is identical as in tutorial.
Both server and client thread manages connection same. I created separated threads for accepting messages following this tutorial and one for sending them.
private void manageConnectedSocket(BluetoothSocket socket) {
//create thread responsible for sending messages.
SendingThread w = new SendingThread(socket);
MainActivity.addSendingThread(w);
//Creates listener for messages to accept.
MainActivity.addListener(socket);
}
Now in main activity always when user click send button, for each worker (sending thread) send message to remote device. Listening is running asynchronously.
IMPORTANT:
You need to handle exceptions when message send fails and remove sending and recieving thread for device when you detect it is disconected. In my case I used well known UUID "00001101-0000-1000-8000-00805f9b34fb". For every device.
You need to wait 3 second between atempts to connect as client because some devices has weak bluetooth hardware and it is refusing connect as client.
Bt connection is supporting up to 7 -10 connections. So you will be limited in that range. I think it is designed for extensions of main device and not for random comunication
Source: search "bluetooth programming" on google

Categories

Resources