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.
Related
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.
I'am trying to make an app that receives some binary data from a other device which is not running android. The Android phone should not transfer any data to this device. So over to my questions:
Should the phone act as the server which establishes the RCOMM channel?
Is it possible to connect the phone and this device without paring, as long as I know the MAC-address of the device? If so, how?
And yes, I have read http://developer.android.com/guide/topics/connectivity/bluetooth.html so no need to only answer with that link. :)
Thanks in advance!
1)
Phone should act as a receiver.
I found that the tricky part is to use correct UUID.
If you connect two phones you should be generating those and everything work fine, but on your hardware you can't change UUID that easily.
Take a look at these reserved UUIDs.
For a Serial Port Profile's (SPP) communication use UUID 00001101-0000-1000-8000-00805F9B34FB.
2)
You can connect to a device without pairing if you already know the MAC address, you don't even need to use BlueTooth discovery.
To get device using MAC address call getRemoteDevice(String)
and then createRfcommSocketToServiceRecord(UUID) to get BlueToothSocket.
Also take a look at checkBluetoothAddress(String) to validate MAC address.
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)
Can I setup a pc to act as a virtual serial port to mimic a device using SPP bluetooth profile that I need to talk to an android phone?
The device would not have no way receiving the UUID that it needs to be listening for to connect to my phone.
I saw the "reflection" method being used to avoid UUID. Is this how to communicate with devices agnostic to android's communication process?
I don't really understand the final part of your question.
For the first part however, you can. You just need a BT adapter inside your PC/laptop, Windows,Python installed, and a little library called PyBluez.
After installing everything, you can communicate via SPP with client-server roles established as you like (PC server & Android client or viceversa). You have examples for both those:
PyBluez server example and client example
Android examples here
You have to have a UUID, but, in the examples above, it is the standard UUID for SPP. Basically SPP is RFCOMMM with this UUID:
00001101-0000-1000-8000-00805F9B34FB
Don't forget to vote if useful!
I am trying to use Bluetooth SPP to communicate over it. In some Android phones, SPP profile is not activated. I faced the problem in the application that SPP was not activated and so the connection could not be established over bluetooth and when I started another app that is 3g hotspot which I think activates SPP and I was able to connect over bluetooth in my app.
So, how can we actually activate SPP profile of bluetooth in Android devices? And does all the android devices has SPP profile?
In developer.android.com BluetoothSocket:
The most common type of Bluetooth
socket is RFCOMM, which is the type
supported by the Android APIs. RFCOMM
is a connection-oriented, streaming
transport over Bluetooth. It is also
known as the Serial Port Profile
(SPP).
This might answer your question ..
https://source.android.com/devices/bluetooth/services
SPP (or ability to use Java API's to establish RFCOMM channels) is available from Android (2.0/2.1) release (Eclair) onwards
On the Android phone you will probably need to run an application that initiates the service over SPP.
**
public BluetoothServerSocket
listenUsingRfcommWithServiceRecord
(String name, UUID uuid)
** API can be used to create a service with specified UUID to listen,
Doing this should make this service visible to other devices which can then connect to it.
I just called Samsung help regarding bluetooth SPP on an S8+ phone. They tell me wait for Android Oreo as it isn't available till then. My previous Sony Xperia used Ntrip Client to talk to an external Geneq GPS to receive corrected coordinates and worked really well. Seems we went backwards somewhere?
Ntrip client on the phone collects an internet data stream of GPS corrections every five seconds and sends these to the GPS. The GPS uses the corrections and sends back adjusted coordinates to the phone to use in any application.
I would appreciate someone brighter than me pointing out a quick fix I just load and run to bring back this functionality.