Connect Android to another device on bluetooth and send data - android

I want to create a connection between my Android and a PC (or another device) just like in the Bluetooth menu from settings.
I have manage to get the list of all bt devices in my range and get their MAC addresses.
So, I have the BluetoothDevice, how can i connect to it or pair with it? I saw a lot of stuff about creating a server on PC, but I don't want that, i just wanna connect with the device.
My attempt were to get the UUID of the device and use createRfcommSocketToServiceRecord...but when i try socket.connect() it cannot connect (because I don't have a server). So practically I need to make the same structure that the OS is doing (connect with the device, if the connected device is trying to send i can receive the info and so on).
If I managed to connect with the device, I want to listen all that it is emitting, no matter the data...I haven't researched this topic, but any advice will be nice.
The connection must be made with any device that supports bluetooth, and I don't have access to it (of course the device is giving me the permission to connect).

Grab the Android sample projects by doing this http://developer.android.com/tools/samples/index.html
They have an app called BluetoothChat that pretty much does just what you want.
The thing about the 'server' is just in the connection, one side needs to open a listening port and that side is arbitrarily the 'server'. Once they are connected they are merely peers and you can write/read data till the cows come home.

Related

How to make an Android application that will connect and read data from an alarm system (local network)?

I need to make an Android app for the problem mentioned in the title. This system already runs on Windows and is connected to the computer via an UTP cable. Now, I need to connect to the local network that I already did by connecting it to the router (I'm not sure if this is enough, but using the Fing app I detected the device in the local network), and build an Android app.
Just to mention I have tried NSD (link below) but I do not think the alarm system supports it.
https://developer.android.com/training/connect-devices-wirelessly/nsd.html
I had a problem defining the type of service. I tried to find all the devices using the code below, but unsuccessfully.
private static final String SERVICE_TYPE = "_services._dns-sd._udp";
Using this type of service I could not find anything, i.e. I did not manage to trigger the method
public void onServiceFound (NsdServiceInfo service)
My question is, should I continue with this approach (is it possible at all)? Otherwise, I need some guidance on how to connect to the device and read data(string type).
For now, I only have the IP address I received using the Fing app.
I found a partial solution for my problem. This is how to connect device and check are data is transferring (in case the someone have similar problem).
You need to do next:
connect device direct to yours computer with Ethernet cable
used WireShark app to find ip address, port and protocol (utp
protocol in my case)
after get device data, disconect device and reconnect directly with yours modem (no need for aditional device like ethernet switch, or to do the port forwarding)
you can use some udp client app on android phone to check if data
are receiving (I used TCP Sender / Receiver). Just set ip address
and port.
My problem was because I used wrong UDP client app. That's the reason it didn't working from the beginning.

Stream data from Android device to desktop computer when they are connected to different networks (Android WiFi, computer Ethernet)?

I am trying to make an android application that will stream live sensor data (e.g. accelerometer, etc) from my android device to my desktop computer.
My android device is connected to a WiFi network and my desktop computer is connected to a network with an Ethernet cable.
There are android applications available that send data from an android device to a PC via WiFi, but both devices must be connected to the same network.
Is there a way to transfer data if the two devices are connected to different networks?
Thank you!
Due to the fact that nearly every network sits behind a firewall (in most cases your home router/nat-device) you need to implement port-forwarding to access network 2 from network 1.
Another aproach would be to set up a VPN (f.i. with SSH) between the 2 hosts.
If your PC is directly connected to the internet, and has a public ip (that means, if you have NOT a RFC 1918 Adress), then you are able to directly connect to that ip by just entering that ip. If not, you need to implement something to get behind the firewall.
Simple answer; to stream data between a PC and an Android device each residing on different networks you need an intermediate server to pass the data through.
Now, if this just a small app that you are doing for your own purpose I suggest using the free version of Firebase (see https://www.firebase.com/). Their sample application that you'll find there, a simple chat service, does pretty much exactly what you need. Simply let the application on your PC (preferably a web app) read the "messages" that you send from your Android device.

Can i connect multiple phone via WiFi in android

I need to connect multiple phones to each other using wifi, without there being any internet though.
So i was thinking if this can be done by turning on the hotspot on one phone and allow the others to connect to it, can this be done ?
Or is there any other way to connect multiple phones via wifi ?
Any help is appreciated .. thnQ
To send and receive UDP messages you can using the following technique:
https://code.google.com/p/boxeeremote/wiki/AndroidUDP
The simplest way to explain this is that the UDP packages can be transmitted on a network with no particular destination set. Applications can pick this up and then decide if they want to handle the message or not.
To connect the devices you can use either a mifi dongle or put one of the phones into tethering mode:
https://support.google.com/nexus/answer/2812516?hl=en
Of course this is limited in range to the hosting mifi or phone.

How to build this android app (Bluetooth technology)

I am a little confused on how to build this android app. Here's how it should work. I have a list of paired bluetooth devices. Lets say paired devices are:
1. A bluetooth head set.
2. A mobile phone.
And my app should automatically connect only to my headset when ever it comes in range.
Thats the basic functionality. I have been doing some research on bluetooth technology and its implementation. I came across the bluetooth chat application, when I started using the application I realized that both phones should have the app on it in order to connect and exchange messages. I tried connecting to my headset but it doesnt connect. And also when I removed the bluetooth chat app from one of my device and tried to connect it from a device which had bluetooth chat app, the devices couldnt connect.
If you have already worked on bluetooth this might look very simple. But how can I connect to a bluetooth device from my app when ever its in range even if the other device doesnt have my app.
Thanks.
This will be a 2 sections answer:
-To connect to a Bluetooth Headset, check my answer on here
-To Always connect to that device, you can save device address to memory then create a service with BroadCastReciever that listens for Bluetooth Connectivity. and check which device gets connected and compare it address to the one save in memory once its finds it connected call ibth.connect(device). ibth is inside the above link.

How to start a local network connection programmatically via Wi-Fi hotspot on Android?

I want to start a local network connection using the built-in Wi-Fi hotspot on Android devices.
Is there a way to accomplish it?
And how to communicate between two devices?
EDIT:
I want to do it programmatically. Then I can transfer my own data between devices.
I haven't done this before, so my answer is all theoretical.
I think what you need to do is declare one device the server and start its hotspot manually with a known name.
Start / stop built-in Wi-Fi / USB tethering from code? (According to this question, you can't start hotspot programatically).
Once you have the hotspot set up, you start up the server app and wait for incoming connections. The server app can show you the ip address or hostname.
On the clients, you can do a simple check and see if the hotspot name is around and automatically connect to it if you find it. See this link for connecting to a network:
Using WifiManager to connect to a network
As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server.
For reference, I found NanoHttpd, which is a webserver for Android. It uses ServerSocket to listen for incoming connections.
Possible answers:
Before ICS and non rooted devices
There is no way you can connect to a device over wifi. There is no API to do. Bluetooth is your best option or user interaction.
Before ICS and rooted devices
I am not sure if someone hacked the code, but when I tried to do an automatic connection to a wifi spot I noticed:
The api is hidden inside the SDK.
The method that does the connection checks the thread who ask for it. If it's not the os thread, it throws an exception.
Using ICS
With ICS there is a new way to connect devices through wifi called Wi-Fi Direct.
Here is a link with some demos.
If you are going to develop your own application for each and every individual terminals(devices) then i think for you socket programming will do the trick .
Yes , android supports socket programming in the same was as java socket programming .
1st google java socket programming tutorials and then you can using the same knowledge in android .
Also do remember to include the uses internet permission in the manifest , actually thats something which ate my brains for a long time :)
You mean communication between wifi enabled devices without any server like p2p? if so its wifi direct which is supported in ICS check this out http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html
Before ICS there is no standard android API for wifi direct, though Galaxy S2 has wifi direct it uses proprietary API's which 3rd party devs can't use.
i dont know programmically create wify LAN,but you can create wify LAN manually,then you can do java tcp/udp program as said by brianestey
"As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server."
for manual connection follow the steps
take settings/wireless&networks/portable wi-fi hotspot settings from any one of the phone
create new hotspot and turn on wi-fi portable hotspot from there
connect all other device to that hotspot including your pc
you will get ip of each device programmically (includig pc,but i dont know)

Categories

Resources