How to Auto-Connect to Arduino with Android App? - android

I am currently working on an android application that blocks texting while driving. It connects through Bluetooth to an Arduino Bluno that is connected to a car via the OBD-II port and scans for data received from a car (either 0 for a non-drive state or 1 if the car is going above 0 mph i.e. driving) and blocks texting if the car is in a drive state.
What I would like to do is have the app/phone automatically connect to the Arduino once I enter the car with the phone and start driving. I am not familiar with how to do so, if I need to use the native Android Bluetooth connection data to scan for the hardware or if there is some other approach. Can I get some guidance as to how I can auto-connect?

Once the device is paired you can initialize the communication with your app or Arduino at any point.
So in your case I believe the best way would be to let the arduino initialize the communication with the app once you detect the car starts driving. The phone would have no idea when that happens according to the setup you described above.
To get an idea of how to use Bluetooth in your app (as always) you should have a look at the Android developer website.
Here are some links to get you started:
BluetoothAdapter: Can help you create a socket that listens to other devices ( BluetoothServerSocket)
Basic bluetooth

Related

Does Android app needs to perform Central and Peripheral roles at same time to send and receive data over BLE?

Currently, I am developing an app which is communicating with one BLE Hardware which is receiving commands and responding back the command response. To Simulate hardware, We have developed one iOS Simulator app, which is working in Peripheral and responding back on requested command. But Somehow, As and when I try to write to the write characteristics, I got the status = BluetoothGatt .GATT_REQUEST_NOT_SUPPORTED in the onCharacteristicWrite callback. But somehow, I came to know that we need to implement the Central and Peripheral roles into Android app.
But I am still not sure, Do we need to implement Peripheral role as well to send and receive data in multiple packets.
I am developing the app using following nice blog post:
- https://medium.com/#avigezerit/bluetooth-low-energy-on-android-22bc7310387a
- https://android.jlelse.eu/android-bluetooth-low-energy-communication-simplified-d4fc67d3d26e
- https://www.bignerdranch.com/blog/bluetooth-low-energy-on-android-part-2/
And using following repo as learning point of view:
- https://github.com/bignerdranch/android-bluetooth-testbed/tree/a/android-ble-part-3
Thanks in advance!
Typically your phone app is the central and it communicates with a peripheral device over Bluetooth. If you want to send data to the peripheral, you can write a characteristic in the phone app, given writing is enabled for that characteristic. Your central can get data from the peripheral in 2 ways: it can either read a characteristic (if it's enabled) from the peripheral or receive notifications from the peripheral (if it's enabled). So if all you have is a central (phone app) and a peripheral (some kind of Bluetooth device) and you want to send data back and forth, you don't need to have both central and peripheral roles in the phone app. If you have some special stuff going on, it might be different for you, I don't know. I'm talking about a typical setup.
If you want to send commands to the peripheral, you could write those commands to a characteristic. The peripheral could in turn respond with notifications. This is basically the way I develop a Bluetooth solution, but it can be different depending what you want to achieve.
It takes two devices to communicate with each other.
Device A:
It will be Peripheral device which will be advertising the data. i.e.: Beacons, BLE Hardware
Device B:
It will be Central device which will send request for read,write. i.e.: Mobile
Setup for Device A:
If you don't have Peripheral, there is a way to make your android mobile to act like a Peripheral if your device is supporting that advance feature.
So before beginning, you may simply check that by using following app:
https://play.google.com/store/apps/details?id=com.kyriakosalexandrou.bluetoothsupportcheck
To make your device act like Peripheral, you may install following app which simply simulates the GATT and advertising:
https://play.google.com/store/apps/details?id=com.ble.peripheral.sim
Important: Setup service and characteristics based on your requirements, make sure characteristic is write enabled if you want write data on it.

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.

Pairing android app with Bluetooth tire pressure sensors and getting real time data from it

i m working on an android application which is going to get real time pressure data from Bluetooth enabled tire pressure sensors and display it on screen. From where should i start and how to pair my android app with Bluetooth enabled sensors? and how to get the real time data after pairing?
Please guide me through this, any help is highly appreciated!!!
This is one of those question....
First thing you need to do is to find out what profile the sensors are transmitting data with. For example, if it turns out to be Serial Port Profile - which is the most common one, you can literally copy and paste android chat sample code and change UUID to UUID of Serial Port.
Just replace UUID of the sample with this:
00001101-0000-1000-8000-00805F9B34FB
This is the sample:
https://android.googlesource.com/platform/development/+/eclair-passion-release/samples/BluetoothChat

Android: bluetooth listen thread for incomming data only

i am working on an android app in which I need to only listen for incoming devices an log the data they are sending using Bluetooth, device sending data is basically a digital Bluetooth enabled thermometer which just sends the data. Problem is i don't know where to start as i m new to android programming. Just a step by step Procedure would be enough to guide me on the right track . i tried Bluetooth chat example but i didn't took me any where also i wasn't able to understand it.
Regards,
Rafiq Ahmed
First thing you need is to know how to make the connection with the thermometer. I suppose that, since the device is paired to the smartphone, you only must to manage the connection as a server (On the android-phone). Here you got a summarize:
Change Bluetooth permissions:
http://developer.android.com/guide/topics/connectivity/bluetooth.html#Permissions
Enable and set up the bluetooth: http://developer.android.com/guide/topics/connectivity/bluetooth.html#SettingUp
Find the device: http://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices
Connect with it: http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAServer
And manage the connection:
http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection
And last thing, I recommend you to pair the device before running the application, at the adjust menu of your mobilephone. Since that, follow the previous steps and all will be fine. If you have some doubt ask me, I had the same troubles about bluetooth connections with arduino and HC-06 bluetooth device
Hope it helps

Arduino + Bluetooth module + Android

I'm trying to think of a way to do a simple home automation project that involves an Arduino, a Bluetooth module and an Android phone. The idea is that I have two Arduino boards in different parts of my home. And if I walk into a room then a LED lights up on the board that has discovered my phone. If I walk out of the Bluetooth range, the LED should turn off.
I have tried the DomoticHome project with a router, but it's only for manual on/off and it's based on the Ethernet module.
I don't want to make an Android application from scratch, but I would like to take some existing open-source application that I could extend to use Bluetooth and/or turn on the LED automatically.
Actually, you might skip that Android application requirement. Just make the Arduino board l2ping your phone by Bluetooth periodically and switch your LED based on success or fail.
As a side effect, you could automate with just any phone, that supports Bluetooth. But the devil is in the detail. It might be necessary to pair boards and phone initially.
Additionally, most Android phones switch off visibility after a minute or two. So you will have to check, whether a ping will work then. (I don't know whether l2ping is available on Arduino out of the box, but it should be portable easily.) Trying to open a Bluetooth port on your mobile device may be an alternative to l2ping. So a no-application-solution should be possible.
Best way to do this would be using a handshake signal every 1 second or 2 seconds.. the bluetooth module should send HI if mobile receives that i should reply with HI.. Once the mobile does not reply it should turn the lights off.. If it receives the HI it should turn the light back on..I have a code that can connect to these bluetooth modules using serial communication..its very simple to use..i will check and send you the code.. All the best..

Categories

Resources