How to notify user if bluetooth connection has been lost? - android

I am developing and app which uses bluetooth communication. I need to have an icon to show if bluetooth is connected or not at all times in the action bar. How can I implement this?
The check on BluetoothSocket mmSocket
if(mmSocket==null)
can be called in methods. Is there a way to check this continuously while the app is running ?

I might give you some help but I will not explain all the thing to you. You have to search a little.
If you don't understand something, Google it or search it on google dev website :)
There is a broadcast here BluetoothAdapter.ACTION_STATE_CHANGED
Register that broadcast and look at the documentation to know what in the BroadcastReceiver will tell you if the bluetooth is ON or OFF.
Please don't forget to unregister your receiver when your activity is paused / destroyed.
Because the broadcast will not tell you on each first launch of the activity if the Bluetooth is ON/OFF (because it is only fired when Bluetooth state changed!), you might want to look at this solution to check the Bluetooth connection state on the fly
How to check if bluetooth is enabled programmatically?

Related

Can Android Companion Device Pairing wake up the Companion App from background upon Bluetooth Connection

I want my app to be woken up when the device finds a specified bluetooth device. The problem is, that after android O you can't register a broadcast receiver anymore to handle that in the background. Having a constant foreground service is also no option for me. Android Auto does this behaviour already I am trying to implement myself.
I found this: https://developer.android.com/guide/topics/connectivity/companion-device-pairing
It says
After the device is paired, the device can leverage the REQUEST_COMPANION_RUN_IN_BACKGROUND and REQUEST_COMPANION_USE_DATA_IN_BACKGROUND permissions to start the app from the background.
Sadly i cant seem to find how this is used and if this even provides what im looking for. I am happy for all suggestions and hints.
There's an option to associate a device using CompanionDeviceManager and register a listener CompanionDeviceService via https://developer.android.com/reference/android/companion/CompanionDeviceManager#startObservingDevicePresence(java.lang.String), then you will receive notifications from the system https://developer.android.com/reference/android/companion/CompanionDeviceService#onDeviceAppeared(android.companion.AssociationInfo) and execute your service in the background.

Android receive callback when bluetooth device is paired/connected by the system automatically?

Is there any way to receive a callback when Android automatically pairs/connects to a bluetooth device, for instance my car, and not having the app already started/open (some kind of broadcast)? I have had thoughts about a service that periodically scans for devices and sees if they are already bonded, but that feels ineffective as Android already does this: it will automatically connect to for instance my car when I am in range. The idea is to start a specific job when I am in range of my car, and stop the job when I am out of range of my car, without having to start the app or have it open.
My initial thought was that there should be some kind of broadcast one could register to, to know when the device is "connected", and in that listener just start the job for some time or until the device is "disconnected". Note that having your phone in your pocket and not taking it up is a necessity. I have only seen examples that does this if the app is already open, by listening on state changes. Is this possible to not have the app open and only be "event driven" and listen on broadcasts?
Try the following code:
IntentFilter actionChangeFilter = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
mContext.registerReceiver(btDeviceConnectChangedReceiver, actionChangeFilter);
You can listen for the ACTION_STATE_CHANGED broadcast of the BluetoothAdapter.
See here for details
Edit:
The above is for changes of state of adapter. To listen to new connections etc use those listed here: BluetoothDevice

how can i keep listening for bluetooth scanner input in my app when device goes sleep or locked

One of the activity in my app connects to bluetooth scanner and get the input and display it in a listview. I got that working by following the bluetooth chat sample app. now the issue is, whenever screen is locked, or device goes to powersave mode, my connection with bluetooth scanner is getting disconnected. how can continue to keep the connection stays on until app is really closed from the system? I am new to Android . I did some search it says to use wakeful lock but how can i do that? Some samples would be much appreciated! Thanks for your time.
Instead of using wakeful lock, I would recommend you to implement the connection inside a background service.
The service runs until its killed by the system or you stop it.
You can found information including a simple example here:
http://developer.android.com/guide/components/bound-services.html

How to implement always have "Mobile data" ON in android?

I have a requirement to always have mobile data "ON" in android. How to implement it through code ? do i need to register for some broadcast receiver or something like that ?
You can't control when mobile data is on. You can only register a receiver to be told when it changes.
Yes you need the register for CONNECTIVITY_CHANGE reciever,
You can use my connectivity change helper to do this,
https://github.com/talhakosen/ConnectionListener
Maybe the solution could b to when user select always on: if already not connected, connect and if connected register receiver to listen for state change, on disconnect state receive, connect again..please suggest this approach is right?

Does the ACTION_DISCOVERY_FINISHED still come if a call a cancelDiscovery()?

In my android application, I'm looking for a particular bluetooth device. When I found a bluetooth device, I check if it's the one I'm looking for and if it is, I call cancelDiscovery();
My question is : If I cancel the discovery, will I still receive the ACTION_DISCOVERY_FINISHED broadcast or not ?
Thank you !
Yes, as soon as you cancel the discovery ACTION_DISCOVERY_FINISHED broadcast will receive it.

Categories

Resources