How to get if android is in hands free mode? - android

I wan't to check if the user is operating the phone while a call is being made by using device's sensors and GPS.
I need to check if we are in hand free mode because in that case I should hear noise but may not see any movements or rotation.
I tried looking in android.telephony but found nothing. Also tried my good old friend google and no luck. Is it even doable?
Thanks!

Here you go. Your problem can be divided into 4 steps. Plus you dont need any sensors or GPS to achieve this.
1) Try to get connected Bluetooth Device list, via BluetoothAdapter class.
2) Check device class, via BluetoothDevice's APIs.
3) If you see Phone is connected to Bluetooth Device with capabilities like A2DP(audio) etc. Then you will know phone is in handsfree mode.
4) also try to find out phone_state of the phone. It shouln't be Idle, if you just want to check in state of making/receiving/during call.

Related

How android system autoconnects to paired device?

What I want to know:
I'm wondering how the android system (like Android smart phone) auto-connects to devices which is paired before.
For example, I pair my bluetooth headset with my android smartphone in the procedure of "turn on scanning/advertising -> click pairing" on day 1. And when I turn on advertising on my headset, it connects automatically on day 2, 3, 4, and so on. The point is, I don't have to make my smartphone scan again to find my (paired) headset.
I can't understand how android system finds that the paired device is turned on. Does the android system scan periodically in background? Even if I don't click "scan" button?
Why I ask:
I want to make my app autoconnect to customized BLE device, after make pairing. I succeed to make pairing(bonding) with createBond() method, but after that, I couldn't find how to make autoconnect. I know I can turn on autoconnect function like this way, connectGatt(XXX, true, XXX), but this autoconnect function doesn't work when the BLE device is disconnected a few days.
So I want to make my app works like android system and Bluetooth headset. But I couldn't find how android bluetooth system works even I dig AOSP codes.
I found many questions (here and here) about problems like mine but there were no answers.
Thanks in advance.
Update:
I found that bt_btif gets activated (with LG smartphone and Nexus 5) when the paired headset is turned on (start advertising). But bt_btif doesn't get activated with my custom BLE device... What can I do?
Executing connectGatt(XXX, true, XXX) is the correct way to go. gatt.connect() also starts an auto connect. Once you execute that, your phone will scan for the device and once it appears it connects to it. The only thing that interrupt this call is either if Bluetooth is turned off on the phone or if your app process is being killed. To avoid your process from being killed, let your app have a foreground service.
One gotcha however, there is a bug in Android which will sometimes make auto connect do a direct connect instead (and cancel after 30 seconds or so). See https://code.google.com/p/android/issues/detail?id=69834. You need to use reflection to avoid this bug.
I think I found a solution.
First, the solution for my question: Android smartphone seems to detect state changes of nearby bluetooth devices from the hardware sides. When the paired bluetooth headset starts to advertise, a callback in HAL (I think) is called.
So I made my app to connectGatt with autoConnection=True to the device that I want, by using MAC address, when the activity is started (in onResume() of MainActivity).
The connection would fail if my BLE peripheral device is not advertising. But the device auto-connects when it starts advertising, because the autoConnection parameter was set to true.
I've done a similar app and i didn't have problem with that. As a last resort for your problem, i would suggest writing the BLE Device Address in a simple DB table and manually connect to it. I've made something like this here
. It is no best practice code, but i hope you can find ideas for you solution.

Is there any method to use to check whether Android Wear is worn or not?

Is there any generic mechanism to check whether the paired Android Wear is worn (and within range)?
The criteria for worn could be one or more of the following:
- Wear within range of Phone (connected).
- Has a pulse (if device has that capability).
- Wear has moved (if device has a motion sensor).
My goal is only to send notifications to the Android Wear from the phone if it is connected and worn. Otherwise it should only be displayed on the phone.
Motion sensors are your best bet. You can try monitoring changes and if there is none for a certain amount of time, you can treat it as a device not being worn.
However, Google already did that for you: https://developer.android.com/reference/com/google/android/gms/location/DetectedActivity.html ActivityRecognitionApi will give you information about motion of the device, so you don't need to figure these things again yourself.
Most specifically, you are looking for this: https://developer.android.com/reference/com/google/android/gms/location/DetectedActivity.html#STILL
You should be able to use this sensor https://developer.android.com/reference/android/hardware/Sensor#STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT
And sensor guide https://developer.android.com/guide/topics/sensors/sensors_motion

Android Awareness of remote Bluetoth-Request with disabled BluetoothAdapter

I'd like to use android's bluetooth for some kind of sensing devices. But I don't want to connect to these devices. As far as I know Devices won't react to scanning when their own bluetooth is disabled. But is there any way to get my app noticed when such a scan has been performed by a remote device, even when my app is running with bluetooth turned off?
I don't want to force toggling bluetooth on, but I need to get some kind of Action started in other devices running the same app. So I'm wondering if some there is any description/data field that can be sent with a bluetooth scan, so if scan is rejected the app has the opportunity to read that data just to know there was this specific call?
I need to leverage context-awareness within my system as to users, not knowing each other, still can interchange content (if they agree). But I need to find some ways of sensing while I also don't want to have all sensors activated all the time.
Hope you can give me a hint, or tell me that this is simply not possible, which would also help me not spending any more time on that.
Thanks.

Manually pairing Bluetooth Decives in Android?

I was reading this
http://developer.android.com/guide/topics/wireless/bluetooth.html#QueryingPairedDevices
which is allot of help on how to pair,connect to a bluetooth device.
I have a situation where I have several BT devices that are in Non-Discover mode always. I know the MAC and the PIN of these devices. IS there a way in Android Dev to manually add devices to the PAIRED list so i can just use the connect as a client.
I understand this maual is written allot for V3. i think i will need to do this on 2.0 ; 2.1- has anybody done this before?
Basically these devices I want to connect to are power saving modules I used pre built BT modules to monitor daylight, another one humidity, etc.. every 3hrs or when interrupted and runs of a single battery for months. So turning off divcory on server saves immense power and prevents other people trying to connect and waste battery.
Not sure what you mean by "manually": Do you mean "manually" as in GUI/user interaction, or "manually" as "I do it in my own application code"?
Some suggestions though:
If you can make your BT devices discoverable at all, you could do it this way:
Make your BT device discoverable
Let Android search for and find the device and then initiate a connection
Android will ask for the PIN for pairing with the device; enter the PIN.
Once pairing was successful, Android stores the pairing information for future use, so that you can
Make your BT device invisible again.
From then on your app should be able to connect to the BT device at any time without further pairing operations.
If the said is not an option for you, maybe you want to go another way:
In current Android versions there are different API routines implemented which are neither documented nor exposed in the normal SDK. A hack kind of solution may be to use some of these "hidden" ("#hide"...) APIs, either via reflection or via modification of your SDK installation.
But be aware that this is always a hack and it may work on a specific device with a specific version of Android and is likely to break your app on another device and/or any other Android version.
Having said that, here comes some reference:
Example of how to access "hidden" bluetooth API.
Then, have a look at the source code for android.bluetooth.BluetoothDevice, e.g. here.
In there, public boolean createBond(){...} may do what you want.

Android Bluetooth without Discovering and discoverability!

My task is, to detect android devices, when reaching the propximity of some BT-Scanners(My PC)
Instead of discovering all visible devices:
all Device BT MAC-Adresses are stored at my PC
Can i look for special devices with the given MAC-adress without turning on discoverability on the android device? I don't want to turn on the discoverability all the time, so maybe I can check with the special MAC-adress for devices. Like the "ping" function in network technik?
I hope you understand my problem and hope for solutions.
If it is just one (or a few) device , the quickest approach is to try to do a connect to it to determine if it is available.

Categories

Resources