Difference between CONNECTION_STATE_CHANGED and STATE_CHANGED - android

What's the difference between Action CONNECTION_STATE_CHANGED and STATE_CHANGED in Android Bluetooth Receiver ?
else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED .equals(action)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
BluetoothAdapter.STATE_DISCONNECTED);
if (state == BluetoothAdapter.STATE_CONNECTED) {
//nothing
} else {
}
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
if (state == BluetoothAdapter.STATE_OFF) {
}
}

According to the docs the difference is the following:
ACTION_CONNECTION_STATE_CHANGED
Intent used to broadcast the change in connection state of the local Bluetooth adapter to a profile of the remote device.
ACTION_STATE_CHANGED
The state of the local Bluetooth adapter has been changed. For example, Bluetooth has been turned on or off.
In other words one Intent is for changes in the connection state and the other one for state changes of the Bluetooth adapter itself.
EDIT:
To detect if a device moves in and out of range you need to use the following intents:
ACTION_ACL_CONNECTED: link to documentation
ACTION_ACL_DISCONNECTED: link to documentation
For both of those you need the normal BLUETOOTH permission and the BLUETOOTH_ADMIN permission:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
The intent filter for your BroadcastReceiver would look something like this:
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
Here is the general documentation about BluetoothDevice.

Related

WiFi direct returns P2P_UNSUPPORTED

I have a real android device (Android 4.1.1). Which supports applications like Xender, ShareIt, Zapya etc which uses WiFi Direct to transfer files.
But when I run my application, it says P2P_UNSUPPORTED.
My manifest permissions are....
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
My MainActivity.java is like this..
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
Receiver receiver=new Receiver();
registerReceiver(receiver,intentFilter);
WifiP2pManager manager=(WifiP2pManager)getSystemService(WIFI_P2P_SERVICE);
channel=manager.initialize(this, Looper.getMainLooper(),null);
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
}
#Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
switch (reasonCode){
case WifiP2pManager.BUSY:Log.e("code","BUSY");break;
case WifiP2pManager.ERROR:Log.e("code","ERROR");break;
case WifiP2pManager.P2P_UNSUPPORTED:Log.e("code","P2P_UNSUPPORTED");break;
}
}
});
In the Log,, I get.
code﹕ P2P_UNSUPPORTED
And my BraodcastReceiver is like this..
#Override
public void onReceive(Context context, Intent intent) {
//Log.e("intent",intent.getAction());
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
Log.e("raw state", ""+state);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.e("state", "ON");
} else if(state == WifiP2pManager.WIFI_P2P_STATE_DISABLED) {
Log.e("state", "DISABLED "+ state);
}
}
And there i get the log as,,
raw state﹕ -1
NOTE : the variable state neither equals WifiP2pManager.WIFI_P2P_STATE_ENABLED nor WifiP2pManager.WIFI_P2P_STATE_DISABLED
It is possible that the applications that you have listed are able to use alternative transports other than Wi-Fi Direct. The P2P_UNSUPPORTED error implies that your device does not have the Wi-Fi Direct feature. You can try confirming this by executing the following: getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT). Alternatively, if your device has Wi-Fi Direct, you can access a Wi-Fi Direct settings activity from the Wi-Fi settings of your device.

How can I catch the event of wifi state changes

I am developing a Power Control Widget, in which I have an ImageButton that when pressed toggles the Wifi and also changes the button's src for visual confirmation. The problem is that I do not know how to detect when the Wifi has been disabled or enabled from other sources, like from the settings or from other power control widget, and change the button's src accordingly.
For example, if I have both my widget and the Android default Power Control Widget on the home screen and I disable the Wifi using my widget, then the Wifi button of the Android default Power Control Widget gets disabled as well, but if I disable the wifi using the stock Power Control Widget, my wifi button's src does not change and still indicates that the wifi is enabled.
Any ideas are appreciated as I can not find a solution to this.
EDIT:
Here is my BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int state = wifi.getWifiState();
switch(state) {
case WifiManager.WIFI_STATE_DISABLED:
remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_off);
break;
case WifiManager.WIFI_STATE_ENABLED:
remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_on);
break;
case WifiManager.WIFI_STATE_DISABLING:
remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_off);
break;
case WifiManager.WIFI_STATE_ENABLING:
remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_on);
break;
}
}
Also the AndroidManifest.xml:
<receiver
android:name="WidgetIntentReceiver"
android:label="widgetBroadcastReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
and the permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Listen to System broadcasts such as ACCESS_NETWORK_STATE or ACCESS_WIFI_STATE by registering a BroadcastReceiver in your manifest.
To listen to these system broadcast you need permissions declared in your manifest. Refer to this documentation for the broadcasts actions and the corresponding permissions required.
I hope you know how to implement a broadcast receiver.
public boolean checkOnlineState() {
ConnectivityManager CManager =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo NInfo = CManager.getActiveNetworkInfo();
if (NInfo != null && NInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}

How to receive intents for Bluetooth devices working?

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>

Android: How to find out name of the bluetooth device connected?

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)) {
}
}
};

Capturing Intent.ACTION_MEDIA_BUTTON event

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
}

Categories

Resources