How to build this android app (Bluetooth technology) - android

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.

Related

Securely connect Bluetooth device to Android

I am developing a device that needs to communicate securely to an Android app via Bluetooth. I need that only a predefined phone to be able to communicate with my device for security reason. And I also need to assure that this is not an intruder device that is trying to communicate falsified data to the app.
Looking at Bluetooth specs, I had the impression the only way to do that is with out of band (OOB) authentication with a pre-provisioning temporary key (TK). This looked very promising but after trying to find how to use OOB on an Android app, it seems like it is not supported.
Is it still possible to use OOB authentification on Android ?
If no, is there another alternative to solve my issue ?
This is doable but it depends on the BLE peripheral that you are developing and the features that it supports. Namely, your device needs to support Whitelisting and bonding. This is how it should work.
Upon first connection to the BLE device, the BLE device should accept the connection regardless.
Once the connection is made, the central device (Android phone) needs to initiate bonding. This doesn't need to be Out of Band (OOB) bonding, and the standard Just Works bonding should do.
Once bonding is succesful, the BLE peripheral should add the Android device's IRK to the list of whitelisted devices.
From this point onwards, the BLE peripheral will activate the whitelist, and therefore only devices in the whitelist will be able to connect to your BLE peripheral, and in your case it will be the Android phone that you used to make the very first connection.
It is not straight-forward and requires an initial secure-less connection with the target phone, but if that is not an issue then this should work.
You can find more information about this in the links below:-
Set and activate whitelist with Android phone
Android BLE server - allow only previously connected device to connect
Whitelisting with resolvable random addresses

Can I connect bluetooth device automatically with the device which already paired without showing choose dialog

I am developing a solution for connection between OBD2(Vehicle ECU scanner) with the mobile app.
I want that when ever that OBD device is found and it is already paired then the application automatically connect with the device and create a socket for transmission of data . without showing the dialog box to choose a paired device.
You can connect to any paired Bluetooth device, to do so, you have to know device's address and UUID. Embedded devices may have some defined uuid (for example, hc05 adapter), you can get that uuid from documentation.
The tricky part here is that you have to monitor failure and loss of connection so you can retry again. I'm implementing that feature in my home project, fell free to see code: https://github.com/AlexShutov/LEDLights

Android Wifi Direct Multicast issue

I am currently working on using multicast on a network to send a message (from one Android Device) on the network to all other Android devices including itself.
It works as required when both phones are connected via the local WiFi network. I.e Phone A send message and both Phone A and phone B receive the message. However, the ultimate goal is to have the phones connected via WiFi-Direct.
Therefore I connected both devices together and tried the same process again but this time only the phone that sends the packet that I'm broadcasting receives it. I.e. Phone A sends the message and receives it but phone B does not receive the message from phone A.
My ultimate question therefore is does Multicast work when using WiFi-Direct between Android devices or not? Has anyone else experienced problems with WiFi-Direct and Multicast on the Android platform?
A similar question has been asked, please see my answer there: Android Wi-Fi Direct Network
To sum up: Multicast should work on Wi-Fi Direct in theory, but some Android devices block incoming non-unicast traffic on the stack level. Since you already have it working on the devices when using normal Wi-Fi, it seems to be another issue. Do make sure that you acquire a MulticastLock. I provided an alternative solution using unicast in the linked answer.

Connect Android to another device on bluetooth and send data

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.

Activate Bluetooth SPP in Android?

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.

Categories

Resources