I'm scratching my head trying to find a way to detect bluetooth headset connect and disconnect events for Android 2.1. I see in API Level 11 that there are some explicit ones, but how do I do it in API level 7? I just want to know when the user connects or disconnects a headset or car stereo capable of playing audio, so that I can pause the sounds I'm playing.
There is no public APIs,
This answer might help where the author used private APIs using reflections.
The author has also posted a comment on how he got it to work.
This looks like a good option to detect bluetooth connect/disconnect.
If that didn't work, another good option is to set a timer in a service that calls AudioManager.isBluetoothA2dpOn() to check if the bluetooth is connected or disconnected.
Not sure if this works in 2.1, but it works in 2.2 and 2.3.
It will capture Bluetooth-Headset connection state changes:
Declare the following intent-filter
<intent-filter >
<action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
</intent-filter>
and in your Receiver in onReceive check for:
if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}
and save the int as a static variable. Access it anytime you want to know if BT audio is connected(1) / disconnected(0). Not pretty, but gets the job done.
Also check out:
https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java
You have to setup a BroadcastReceiver for android.bluetooth.headset.action.STATE_CHANGED action. The Intent's extra android.bluetooth.headset.extra.STATE contains current state (disconnected, connecting, connected). More info the Android source code
Related
So my android app is behaving as a beacon, means, it will be advertising and other BLE devices will be connecting to it. Well, this is how our project is working so please don't raise questions on this as why am i using my app as a beacon and not as a scanner. Anyways, It behaves as a beacon and starts advertising and now I want to know that if a device connected to it. I cant find a way how to do this.
Of course, I am using this flutter package. beacon_broadcast 0.3.0
This is my code.
void startAdvertising() {
BeaconBroadcast beaconBroadcast = BeaconBroadcast();
beaconBroadcast
.setUUID(advertisingUUID)
.setMajorId(1)
.setMinorId(100)
.start();
}
First, Flutter is just a UI toolkit and has no support for other system APIs such as Bluetooth.
You should therefore look what the official Android APIs offer in the first place. Usually when using BluetoothLeAdvertiser for advertising, one often also adds an instance of BluetoothGattServer in order to handle connections. If you have created a BluetoothGattServer using openGattServer, you will get a onConnectionStateChange callback whenever a device connects or disconnects. So that answers your question how an Android app can get notified when a device connects. You probably also want to use the same API to add a GATT service so that the other device can communicate with your app. Other alternatives is to use the GATT client API if it's the other device that has a GATT server, or you might want to use the L2CAP CoC API.
Note that if Bluetooth is turned off/disabled/restarted, your BluetoothGattServer object will automatically die and you need to recreate it. To get notified when this happens, use a state change intent receiver for BluetoothAdapter.ACTION_STATE_CHANGED as explained in this example https://stackoverflow.com/a/9694138/556495 to recreate your BluetoothGattServer (and advertiser) when state is changed to STATE_ON.
Now, since you want to use Flutter but Flutter uses Dart, you cannot directly consume the Android APIs. Instead you need to write a bridge/plugin, to bridge your Dart code and Java code. See https://docs.flutter.dev/development/platform-integration/platform-channels for a tutorial how to do this. If you're lucky, someone else might have already created such a package that does exactly what you want. Unfortunately, the beacon_broadcast package you found, only implements BluetoothLeAdvertiser and not BluetoothGattServer, as can be seen by the source code here: https://github.com/pszklarska/beacon_broadcast/tree/master/android/src/main/kotlin/pl/pszklarska/beaconbroadcast.
How can we listen for special BT device commands like redial from our app? For now, I'm only able to listen to the only one - play/pause/start/end call button (KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE).
Using common BroadcastReceiver for "android.intent.action.MEDIA_BUTTON" doesn't help.
Solution with BluetoothSocket, BluetoothServerSocket won't help too since it requires our code to be invoked on BT device side.
During my redial button tests I see the next line in the logs:
01-20 05:52:30.486 942-1060/com.android.bluetooth E/bt-rfcomm: PORT_DataInd, p_port:0x5526c200, p_data_co_callback is null
It looks like there is something sending an event from BT device to the android device. But how can we catch it on app side, what should we use? I work on some system app by the way and theoretically can do very specific, low-level and system things, so maybe there could be some solution.
afaik, this isn't possible, sadly...
I've been working on custom handling BT headset keys, like VOL UP, DOWN, eventually ANSWER/DISCONNECT/REDIAL. Even made rich question, but without single answer or comment...
After some research (days, weeks...) and digging into Android source I've found that these buttons are sending some AT commands. I've also found methods which are checking these AT commands and if system is able to respond/handle them then it TRY to do it and further won't pass any event to any app/socket/rfcomm/anything... E.g. under VOL UP button we have some well-known AT command, system can handle it, so try to do so, even when we already have volume set to max. Any app won't be noticed that this happened...
btw. I don't think this logcat line posted in question is strictly relevant to button press (but may be indirectly), but you have bt-rfcomm keyword in there, so you may try to establish some RFCOMM connection with Bluetooth device, maybe you will get some luck on this topic... (personally I gave up...)
I have a list of BluetoothDevice and I want to pair programmatically with one of them that has a PIN.
I have read several posts here in which the subject is discussed, but I've found two very different approachs.
FIRST OPTION: You call the device.createBond() method. Then, on a BroadcastReceiver, you listen the BluetoothDevice.ACTION_PAIRING_REQUEST action and there you call
device.setPin(PIN_BYTES);
device.setPinConfirmation(true);
You can see the complete example & post here: How to pair Bluetooth device programmatically Android
SECOND OPTION: What if you call device.setPin(PIN_BYTES) and device.setPinConfirmation(true) first, and then device.createBond()? Eg:
if(connConfig!=null && connConfig.bluetooth!=null){
device.setPin(connConfig.bluetooth.pass);
device.setPairingConfirmation(true);
device.createBond();
}
And then you forget about listening the BluetoothDevice.ACTION_PAIRING_REQUEST action on your BroadcastReceiver and only pay attention to BluetoothDevice.ACTION_BOND_STATE_CHANGED events? Android + Pair devices via bluetooth programmatically
See in the first answer the code. As far as I get it, that dude isn't using any PIN for pairing, so I also need to use the setPin method.
Which of both you find better?
Am I missing something? BTW: I'm not using reflection because Im not targeting older platforms.
I want to determine if a paired device is currently active. I don't really need to connect to it, just determine if it has a heartbeat.
I'm fighting a series of IO errors while trying to connect a bluetooth socket, but I'm not sure I really need to.
Assuming you are using Android
If you have a BluetoothDevice object to this device, you can register to listen for the Broadcast Actions - ACL_CONNECTED or ACL_DISCONNECTED and keep track of the connection state.
If you want to find out wether a BT-Headset is currently actively connected (audio being routed to it), do the following:
Declare the following intent-filter
<intent-filter >
<action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
</intent-filter>
and in your Receiver in onReceive check for:
if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}
and save the int as a static variable. Access it anytime you want to know if BT audio is connected(1) / disconnected(0). Not pretty, but gets the job done.
Also check out:
https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java
I'm working on a research project which involves Bluetooth and the Android OS. I need to make Bluetooth discoverable indefinitely in order for the project to continue.
The Problem:
Android limits discoverability to 300 seconds.
I cannot ask the user every 300 seconds to turn discoverability back on as my application is designed to run in the background without disturbing the user.
As far as I am aware, there is no way to increase the time though Android's GUI. Some sources have called this a safety feature, others have called this a bug. There may be a bit of truth in both...
What I'm Trying / Have Tried:
I'm trying to edit a stable release of cyanogenmod to turn the discoverability timer off (it's possible; there's a configuration file that needs to have a single number changed). This isn't working because I'm having verification problems with the resulting package.
During the past week, I downloaded the cyanogenmod source code, changed a relevant class in the hope that it would make Bluetooth discoverable indefinitely, and tried to recompile. This did not work because (a) the repo is frequently changed, leading to an unstable code base which fails to compile (OR, it could be that I'm using it incorrectly; just because it looked like it was the code's fault in many instances doesn't mean I should blame it for all the problems I encountered!) and (b) the repo decides to periodically "ignore" me (but not always, as I have gotten the code base before!), replying to my synchronization/connection attempts with:
fatal: The remote end hung up unexpectedly
As you might imagine, the above two issues are problematic and very frustrating to deal with.
More Info:
I'm running Android 2.1 via cyanogenmod (v5 I believe). This means the phone is also rooted.
I have a developer phone, which means that the bootloader is unlocked.
My phone is an HTC Magic (32B).
The Big Question:
How can I make Bluetooth indefinitely discoverable on Android?
See the following link:
http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices
Specifically, the last sentence in the paragraph below:
Enabling discoverability
If you would like to make the local device discoverable to other devices, call startActivityForResult(Intent, int) with the ACTION_REQUEST_DISCOVERABLE action Intent. This will issue a request to enable discoverable mode through the system settings (without stopping your application). By default, the device will become discoverable for 120 seconds. You can define a different duration by adding the EXTRA_DISCOVERABLE_DURATION Intent extra. The maximum duration an app can set is 3600 seconds, and a value of 0 means the device is always discoverable.
So, this should work:
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivity(discoverableIntent);
If you check out the BluetoothAdapter class
you will find the hidden method:
public void setDiscoverableTimeout(int timeout)
Now you only have to find out how to use it. You have to do a method invocation to do so.