I am developing a client-server bluetooth chat app where multiple clients can connect with the server. If I have multiple servers, I want the client the choose which server it can connect nearby to but I am having problems in knowing which device has the app installed and it is a server. There are multiple bluetooth profiles and I am not sure which one to use it and how. Pre-Storing the MAC id's of the devices with server is one solution but I don't want to do that. Please suggest something. Thanks a lot :)
I assume the intention is to avoid an insecure connection attempt with listenUsingInsecureRfcommWithServiceRecord/createInsecureRfcommSocketToServiceRecord(1).
If you don't mind changing friendly name of the device, just during the device discovery period, you can rename your servers to a pattern of yours with setName(2). On device discovery, you can get the remote device name(3), followed by pattern matching before initiating the connection.
And revert the name, once you are done with discovery.
You use a UUID to create the BluetoothServerSocket. This UUID identifies YOUR application and is retrievable by the bluetooth discovery. This UUID is used to connect to your server device as well so you don't need to encode your app's name in the service name.
Check this answer for a detailed description:
How do Bluetooth SDP and UUIDs work? (specifically for Android)
Related
I would have to build two apps (Android and iOS) and control some equipment via Bluetooth. For clarification, i cannot just use OS device discovery to connect to the equipment because there's hundreds of them and their position is very important (as well as the user's position when issuing commands), thus it's less of a hassle for operators to just point the phone's camera at a QR code and connect than having to go through a long list of devices with mangled names.
I haven't found many details about specifically using a QR code for bluetooth connections, but i figured that people experienced with this kind of communication will be able to say if it can be done. Please correct me if i'm wrong, but my understanding is that a bluetooth socket is not that different from a TCP one and a connection could be established by knowing the server's credentials.
Can I use a QR code to store device credentials that I can use to establish a connection? It doesn't really matter how much information needs storing, the QR code can contain any sensitive information.
Is there anything more, apart from the UUID, that would need storing on the QR code?
Is it simpler to configure the device as a server and the phone as a client for this specific request? There will be multiple operators that will need to work with these devices.
This is specific to Android and iOS, but if the points above were possible, would I get an OS pop-up window for each connection? Would skipping the discovery step save the operator the hassle of having to confirm the connection to the OS?
If the target device is configured as a server, each with its UUID as the QR code, can i scan that code and open a socket to that very device without manually connecting to it from the phone's menu?
I want to understand is it possible to make an app that, after installing and running on 2 different devices, connect with each other in a way, for example, there is a button and a text view in the app and when I click the button on my device, it updates the text view in the other device's app, and same the other way round.
If yes, then please guide me what's it called and where can I read and learn about it.
Thanks in advance :)
Take a look at the section on Connectivity on the Android Developer page. It has a few solutions to this.
In your case, there is a Bluetooth section on communicating between devices. Essentially you have one device act as a server and another as a client.
A description of what is involved as a server from that page.
Connecting as a server
When you want to connect two devices, one must act as a server by
holding an open BluetoothServerSocket. The purpose of the server
socket is to listen for incoming connection requests and when one is
accepted, provide a connected BluetoothSocket. When the
BluetoothSocket is acquired from the BluetoothServerSocket, the
BluetoothServerSocket can (and should) be discarded, unless you want
to accept more connections.
Now you connect to the server phone with your client phone.
Connecting as a client
In order to initiate a connection with a remote device (a device
holding an open server socket), you must first obtain a
BluetoothDevice object that represents the remote device. (Getting a
BluetoothDevice is covered in the above section about Finding
Devices.) You must then use the BluetoothDevice to acquire a
BluetoothSocket and initiate the connection.
Another option on newer phones can make use of WiFi Direct which is built for what you're trying to do as well.
I'm asking because I've been facing that issue for weeks.
I need to develop an Android application that can be able to perform an Bluetooth Connection and send data to an Bluetooth Hardware.
Ok, so here's the point:
is it possible to perform a RFCOMM connection to an 'unknown' device?
I mean, is it possible to do this without having any information about the hardware code?
Because i'm only able to modify the AndroidDevice (cellphone) code, for SENDING purposes.
I want to send a byte and make sure it was received, but there's only a sending code.
Could we have some more details? Is the other device accessible by you, as in can the other device know information about the Android device? And what do you mean by a "sending code"?
The Android device can pick up any active Bluetooth device in its range, and know the name and address of those devices. So, if the Android device doesn't know anything about the Bluetooth device it wants to connect to, you can always sort through the list of devices in range, and get its hardware code through that.
See the documentation about Bluetooth, specifically the Finding Devices section:
http://developer.android.com/guide/topics/connectivity/bluetooth.html
I've done a fair amount of Bluetooth work with Android devices and Arduinos, and the Bluetooth library might be a bit of a pain to work with, but it is powerful.
With bluetooth the two devices have a UUID. If you a writing a program that runs on both devices you have them listen for that UUID. When they connect as a client or a server you have a dataInputStream and a dataOutputStream. Then you can push and pull bytes out of those.
Can any one describe what is wrong with this code. It didn't search devices, and it showed some paired devices.
Project hosted here in Github.
think of the UUID as one identifier for a particular kind of service your device is advertising or accepting. It could have many such identifiers for many different services it can offer or use. It's not an identifier for the actual device.
The UUID you are quoting is the same one I found here and use too. it seems to be the UUID for generic bluetooth SPP service. (but haven't found much actual proof like an official published document quot8ing this number, just posts on here with no refrence) This UUID will allow your android device to connect to things like bluetooth serial port adapters, or anything expecting a generic serial port device... so it's a good one to start with.
If you plan to create a special service that's not the same as bluetooth SPP or has a different reason to exist, like android chat does, then it's a good idea to create your own UUID. Of course all devices to connect your service will have to understand this same special number.
May i achieve that server side application (using android bluetooth API)connect to particular client if i already know client's device name (as saved at servar side app in its database)? How would i handle it? some idea /Code example?
i am making class survey app for android devices using bluetooth ( i am using bluetooth chat example given by android developers) where one device behaves as server (an app for teacher) and all other devices behaves as clients (an app for students) and connect to server and download the quiz.Solve it and get the result back. i have done this part. but i need to know how can i assure that authentication on bluetooth, i mean only well known devices (clients) as saved in my database (at servar side app) can connect to my servar side app and all other clients are ignored and can't distrub the system(because a student might have my client side app and can connect easily but the student is not well known by the app as saved in my database). it is important for my app as i want that only particular group of students can access/connect to server side app. All others are ignored. A code example would be nice . i would really appreciate your time
In standard Android installs (i.e. not in a custom ROM) you can only connect between 2 paired devices. If a student's device is not paired with the teacher's device no connection will be possible.
If a student's device IS paired with a teacher's device already but the student shouldn't be connecting you will need to do the access control yourself using either the Bluetooth MAC address or device name: you probably want to use the MAC address, given that the name can be trivially changed but the MAC address is MUCH harder to change.