I'm trying to have my app's service listen for Bluetooth connection and disconnection attempts, so I can dynamically check/support Bluetooth tethering network communication.
First I have two Samsung S4s (running CyanogenMod 10.2, which is Android 4.3.1 based) which I can pair just fine. If I set one device to Bluetooth tether, when the other connects, a new bt-pan network interface is created and DHCP is used to assign IPs. I confirmed this using iwconfig and ifconfig in shell.
I have the following perms in my app: (there's more, I'm just pointing out the BT perms I added)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Here's my Service's onCreate where I set my IntentFilters: (note I've got Toasts here, but I was working with Logging originally)
#Override
public void onCreate() {
super.onCreate();
...
this.mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
}
Here's my BroadcastReceiver implementation:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)) {
Toast.makeText(getApplicationContext(), "BT found", Toast.LENGTH_SHORT).show();
} else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
Toast.makeText(getApplicationContext(), "BT Connected",
} else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "BT Disconnect requested", Toast.LENGTH_SHORT).show();
}
}
}
Now, when I turn Bluetooth off/on, connect/disconnect to a paired device, nothing fires. I've payed around with the devices from both ends. Nothing is broadcast.
Anyone have a suggestion? I really need to receive these bluetooth events. Please don't point to another post/site with the same perms and intent filters. Thanks.
I have a very similar code which works, the only major difference i found was this:
mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
i beleive that registerReceiver should be called from the context you want to get intents to.
try calling the method from this. i.e remove the mLocalBroadcastManager like:
registerReceiver(mMessageReceiver, filter);
If you Register the Receiver on the Manifest instead of the Activity, you can receive broadcasts even if you do not have the app opened, you can do this:
<receiver android:name=".BluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" />
</intent-filter>
</receiver>
Related
Goal
If an already connected bluetooth device disconnects, and an Activity is already running, close the Activity
Problem
When the bluetooth device connection state changes through BluetoothAdapterProperties: CONNECTION_STATE_CHANGE, it seems like a new Activity is created or the current one restarts.
There is nothing in the code that listens and/or should react to bluetooth connection state changes.
The problem manifests itself in the use of BroadcastReceivers which in turn starts the Activity using intents. For some reason the Activity keep running through its lifecycle, spawning up new windows, even if the only change in bluetooth connectivity is BluetoothAdapterProperties: CONNECTION_STATE_CHANGE
I've tested this solely on a Nexus 6P with Android N. I have no idea yet what kind of implications this implementation means for any other devices yet. But I at least need to get this working on one device.
UPDATE
I have done a fair bit of experimentation and found that if I don't register the BroadcastReceiver in AndroidManifest, the problem with onDestroy being called disappears. But, I want to be able to react to Bluetooth connecting devices, so that I can launch my activity and then process input. If the activity gets destroyed every time a new device connects/disconnects, this won't work at all. What's the reasoning for having the BroadcastReceiver finishing an activity if it's already running and can I control that behaviour?
UPDATE 2
I can also conclude that disabling the statically declared BroadcastReceiver using this method https://stackoverflow.com/a/6529365/975641 doesn't improve things. As soon as the Manifest-BroadcastReceiver catches the ACL_CONNECTED intent from Android, and start my custom activity, it will ruthlessly call onDestroy on it when the connection state changes (which is usually just before an ACL_DISCONNECTED). It does not matter if I have ACL_DISCONNECTED declared in the Manifest or not. As long as I have my receiver listening for ACL_CONNECTED intents and I launch my Activity based on that, onDestroy will be called when the connection state changes. So frustrating.
Manifest
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BtActivity"
android:launchMode="singleTop" />
<receiver android:name=".BtConnectionBroadcastReceiver" android:priority="100000">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
</application>
BtConnectionBroadcastReceiver
public class BtConnectionBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "BT";
public static final String BROADCAST_ACTION_CONNECTED = "CONNECTED";
public static final String BROADCAST_ACTION_DISCONNECTED = "DISCONNECTED";
SharedPreferences mSharedPreferences;
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
Log.d(TAG, "DEVICE CONNECTED");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("DEVICE NAME", device.getName());
Log.d("DEVICE ADDRESS", device.getAddress());
Intent i = new Intent(context, BtActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Log.d(TAG, "DEVICE DISCONNECTED");
intent = new Intent();
intent.setAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_DISCONNECTED);
context.sendBroadcast(intent);
}
}
BtActivity
public class BtActivity extends AppCompatActivity {
private static final String TAG = "BT";
Window mWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt);
Log.d(TAG, "onCreate");
IntentFilter filter = new IntentFilter(BtConnectionBroadcastReceiver.INTENT_FILTER);
filter.addAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_CONNECTED);
filter.addAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_DISCONNECTED);
//registerReceiver(mReceiver, filter);
mWindow = getWindow();
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
//params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
params.screenBrightness = 0.2f;
mWindow.setAttributes(params);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
mWindow.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "BROADCAST RECEIVED IN ACTIVITY");
String mac;
if(intent.getAction().equals(BtConnectionBroadcastReceiver.BROADCAST_DEVICE_CONNECTED)) {
Log.d(TAG, "CONNECT BROADCAST RECEIVED");
mac = intent.getStringExtra("mac");
checkConnectedDevice(mac, true); // This adds a device to an internal list
Log.d(TAG, "Activity nr of devices:" +mNrOfDevices);
}
if(intent.getAction().equals(BtConnectionBroadcastReceiver.BROADCAST_DEVICE_DISCONNECTED)) {
Log.d(TAG, "DISCONNECT BROADCAST RECEIVED");
mac = intent.getStringExtra("mac");
checkConnectedDevice(mac, false); // This removes a device from an internal list
Log.d(TAG, "Activity nr of devices:" +mNrOfDevices);
if(mNrOfDevices < 1) {
Log.d(TAG, "No more connected devices");
finish();
}
}
abortBroadcast();
}
};
}
When I run this code, I get the following chain:
Start MainActivity (not included, it only contains an activity with the default main layout, so that the applications receiver is registered)
Switch on a bluetooth device (This has been paired earlier, so android knows about it)
Wait until it connects and get this:
DEVICE CONNECTED
onCreate
onResume
Switch off the bluetooth device and I then get this:
DEVICE DISCONNECTED
onDestroy
onCreate
onResume
I can't grasp why the activity is getting destroyed restarting at this point. The activity is already running, the BroadcastReceiver only sends a broadcast to an already running activity. I can't figure out why there's a reason for the Activity to kill itself and then restart again. This leaves me in a state of the Activity still running, but it is not the original Activity that was started.
I do however see something in the logcats that seem to have something to do with this, and it's in this sequencing;
06-02 15:45:09.156 26431 26431 D BT : DEVICE DISCONNECTED
06-02 15:45:09.213 19547 19547 D BluetoothAdapterService: handleMessage() - MESSAGE_PROFILE_CONNECTION_STATE_CHANGED
06-02 15:45:09.213 26431 26431 D BT : onDestroy
06-02 15:45:09.214 19547 19547 D BluetoothAdapterProperties: CONNECTION_STATE_CHANGE: FF:FF:20:00:C1:47: 2 -> 0
06-02 15:45:09.216 3502 3805 D CachedBluetoothDevice: onProfileStateChanged: profile HID newProfileState 0
06-02 15:45:09.237 414 414 W SurfaceFlinger: couldn't log to binary event log: overflow.
06-02 15:45:09.239 26431 26431 D BT : onCreate
06-02 15:45:09.243 26431 26431 D BT : onResume
In the AndroidManifest.xml add the following for the Activity, it is worked for me.
android:configChanges="keyboard|keyboardHidden"
Having read this https://developer.android.com/guide/components/broadcasts.html#effects_on_process_state I can probably safely conclude that the reason for why onDestroy gets called is because the receiver affects the process in which it is run, effectively meaning when the receiver has run its onReceive method, it will destroy itself and take the Activity with it.
I would of course have wished it was working differently, but I believe this is what effectively is going on and need to take another approach.
I know this answer is very late, but I was facing this issue with my activity tag. In Manifest file I have added below line for configChange.
android:configChanges="keyboard|orientation|screenSize|keyboardHidden|navigation|screenLayout"
Now my application does not kill itself.
Basically im trying 2 things here, Im trying to start a toast when my bluetooth device is connected to a particular device (so need to check if that is the particular bluetooth name), if that is the particular device then i want to show a toast when connected to that particular bluetooth device. I also want to show a toast when my bluetooth is disconnected to that particular bluetooth device.
Here's my code:
in manifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>
Class's code:
public class MyBluetoothReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "RECEIVER CALLED!!", Toast.LENGTH_LONG).show();
if(intent.getAction().equals(
"android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
// code for Bluetooth connect
Toast.makeText(context, "CONNECTED!!", Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(
"android.bluetooth.device.action.ACL_DISCONNECTED")){
//code for Bluetooth disconnect;
Toast.makeText(getApplicationContext(),"DISCONNECTED",Toast.LENGTH_LONG).show();
}
}
}
In my code im getting receiver called toast properly and even the toast for disconnected is also working but toast of connected never works.
Please let me know why CONNECTED toast doesn't work and how to make this code work when connected to a particular device ( I don't want to show this toast for all the devices ).
Change your broadcast receiver to:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//you can get name by device.getName()
} else if (BluetoothAdapter.ACL_DISCONNECTED
.equals(action)) {
}
}
};
I'm trying to detect USB device disconnection. I'm using the code below:
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
UsbManager.ACTION_USB_DEVICE_DETACHED)) {
Toast.makeText(getBaseContext(), "usb was disconneced",
Toast.LENGTH_LONG).show();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(receiver, filter);
But it doesn't work. Nothing happens when I disconnect a USB storage. What is the problem?
Thanks.
You can receive power connected or disconnected events, like this by placing it in the android manifest.xml:
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
and further more about details explanation about USB connect and disconnect follow the example
In the Android 2.3.3 BluetoothChat example with with createInsecureRfcommSocketToServiceRecord() API, users are still prompted to accept the pairing request, even though no PIN code is presented.
Is there a way to automate Bluetooth pairing request without user intervention? Or is this never possible due to security concerns? I have been looking online for 2 days now and haven't really found much, so if anybody knows, please post.
Thanks!
So, I had this cuestion, if some one needs the answer to this working in android 4.4.2
IntentFilter filter = new IntentFilter(
"android.bluetooth.device.action.PAIRING_REQUEST");
/*
* Registering a new BTBroadcast receiver from the Main Activity context
* with pairing request event
*/
registerReceiver(
new PairingRequest(), filter);
and the code for the Receiver
public static class PairingRequest extends BroadcastReceiver {
public PairingRequest() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
try {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
//the pin in case you need to accept for an specific pin
Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
//maybe you look for a name or address
Log.d("Bonded", device.getName());
byte[] pinBytes;
pinBytes = (""+pin).getBytes("UTF-8");
device.setPin(pinBytes);
//setPairing confirmation if neeeded
device.setPairingConfirmation(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
and in the manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
and the broadcastReceiver
<receiver android:name=".MainActivity$PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter>
</receiver>
Not with the standard API, no: if the MAC address is not already in the pairing database there will always be the prompt. I'm told that if you have a device that has been rooted and have public read/write access to the bluetooth service's DBus endpoint you can work around that but I've never seen that actually implemented.
i came across the same problem, i hope the following code will help:
firsly we need:
<receiver android:name=".broadcast.PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter></receiver>
secondly we need the BluetoothDevice class, and:
public class PairingRequest extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
device.setPin(pinBytes);
}
}
}
I'm developing a Android audio application. Now, I'm capturing
and playing audio with sucess and I want to add a new feature, the capture and playing through bluetooh headset.
I have been reading about that, and seems that I must manage the ACTION_MEDIA_BUTTON
event:
Java file:
....
public class audioBroadcastReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()))
{
Log.d("","#### WORKS" );
}
else
{
Log.d("","#### ????" );
}
....
xml file
....
<receiver android:name="audioBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON">
</action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.BLUETOOTH" />
But nothing happens, so, somebody may give me a example or a idea for:
1º Know when the bluetooh has been connected.
2º Routed the audio packets through bluetooh headset.
Thanks!
You need the bluetoothAdapter method.
BluetoothAdapter blueT = BluetoothAdapter.getDefaulAdapter();
// create broadcast receiver
BroadcastReceiver blueReceiver = new BroadcastReceiver(){
// todo switch states to turn on or off or check is on
// check is on is something like this:
case (blueT.STATE_ON) : {
// do something with it
}
if(blueT.isEnabled()){
do something
}