I was doing some research and wanted to know if it's possible to handle calls of my phone from a device (smart watch, bluetooth headset) that is connected to my phone via bluetooth.
I intend it be able to answer calls that come to phone and make calls from my phone when I give the command to the device and I want that device to act like the bluetooth speaker during the call.
I've seen use cases where it is done but have no idea on how it's done or where to start, Any ideas on this? :)
What you are looking for is smartwatch with a speaker/microphone. You should be able to find plenty of them on internet.
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.
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.
I need to know from glass, if there is a phone call in process. I can made it easy on android device ... but I have problems with glass, the same code to telephonymanager and so on ... doesn't works on glass.
Any idea how to catch the broadcasting process or PhoneCallManager to know if the phone has is talking?
thanks for advice.
Glass works as a bluetooth headset when making a phone call (see user guide). It is not making the calls itself.
I think you'll have to use the BluetoothHeadset class which will allow you to interact with the Headset bluetooth profile. You can then retrieve the state of the headset connection.
I've got my code working using the isWiredHeadsetOn() method in AudioManager as long as I use the headset that came with my phone. If I use a pair of headphones without an inbuilt microphone, isWiredHeadsetOn() returns false, which I suppose is fair enough, but isn't what I want! Is there a way to detect both headsets and headphones?
The best way is to use a broadcast receiver for ACTION_HEADSET_PLUG. Then you get events when the headset is connected or disconnected.
I think it should work for bluetooth devices but am not 100% sure to be honest.