Android - detect if device connected with another hardware? - android

I've received a requirement for detect whether my android device is connected with any other hardware like (Audio, car etc) and notify user if disconnected with that hardware.
I really don't know if these hardware connects with device either using Bluetooth or Local Wi-Fi? Can someone help please?

If the devices are connected via Bluetooth, register for the BluetoothDevice.ACTION_ACL_DISCONNECTED intent. It will fire whenever a Bluetooth device disconnects. Upon receiving this intent, examine its extras to find out which device disconnected:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
For devices connected via WiFi, it depends on how the connection is established. If the device is a WiFi access point, register for connectivity change intents.

If Bluetooth state is not opened, just only onReceive() Local Wi-Fi connected hardware devices list change!
If Bluetooth state is opened,onReceive() Local Wi-Fi connected hardware devices list change and onReceive() already paired bluetooth devices list change.

Related

Android: I can't get bluetooth paired device

I can discover devices and createbond. It's working fine.
But when the user close the app and open again I'm already in bonded state and I only need open serial socket but I can't find the device!
GetBondedDevice gives all the device connected before and also the active one.
Because of that I can't understand is it realy connected and ready to open serialsocket!
Serial socket is not always active. I'm closing onDestroy or onPause to stop communicate.
To simplfy the question: I want to check if given mac address is paired and ready to open serial socket.
As you mentioned, paired devices and connected devices are two different things as follows:-
Paired devices: these are devices that have exchanged security keys and are therefore 'linked' to your Android device (this makes the reconnection easier). These can be either connected or unconnected.
Connected devices: these are devices that have an active channel with your Android device that can be used to exchange data.
In your case, to get devices that have an active connection to your device, you can use the getConnectedDevices or getConnectionState.
You can find more references here:-
How can I programmatically tell if a Bluetooth device is connected
Android: List connected Bluetooth devices
List connected Bluetooth devices

Auto connect with already paired BT device

Goal:
I'm trying to connect my Android phone to an already paired bluetooth device when its in proximity.
Issue : I am novice to BT technology. I am pairing up my Android phone with a BT device using classic bluetooth API (SPP) as BT device is intent to send large data to mobile app.
The requirement/issue is : Mobile device should automatically connect with already paired BT device when in range. Note that we are using classic BT API and not the GATT.
How does the car audio system automatically connects with driver's phone. I think its the car's BT system who initiate pairing request with phone when car is turn on?
There are a lot of loose ends here. To reconnect to an already paired SPP device depends upon the behavior of the device. If it is passive (always in the slave role) you will have to actively connect to it. I have a passive SPP medical device and to auto reconnect to the device I have to 'continuously' do a discovery in the background (by continuously I mean to a discovery for 5 seconds every 10 seconds or something like that). When I detect the device I create an SPP socket and invoke the connect() method. The alternative is to have a UI that has the user invoke the connect() method when using the device. Hopefully the device is nice and becomes discoverable when it is ready to connect. If it is not discoverable then you need to (yuulk) poll it with connect attempts.
If you have an active device, you can set up a listener socket so the peer can connect to you and you avoid all that background discovery work. At least that is what I have had to deal with.
Yeah by periodic discovery of already paid BT device we can detect and auto pair with them but ar the cost of battery. This is what I had done.

Wifi Direct discovery

I am just getting started with wifi direct. I know that wifi direct works by discovering devices and services. My question is: if I have my wifi enabled on my smartphone and I am not running an application that uses wifi direct, is my phone still discoverable to a phone that is running wifi direct device discovery?
Thanks!
In order to establish a WiFi Direct connection both phones should be running WiFi Direct discovery. In other words, they will see each other when they are both scanning for WiFi direct connections at the same time. This is because the way WiFi Direct works is that when phones are scanning for WiFi Direct connections, they will negotiate with the other peers for the role of Access Point or Slave device. However, when phone A is connected to phone B via WiFi Direct and phone C is scanning for connections, it will detect the connection of A-B since one of them is acting as an Access point.
I will add some point on #Ziad answer. During scan process, one device who started scanning will broadcast 802.11 WiFi Direct probe request and another device will listen to that probe request and send back the probe response, that how they will discover each other.
Scanning will happen on one channel 1, 6 ,11 on 2.4 GHz.

Reconnecting an Android app to a paired bluetooth device

I am developing an Android app that connects beautifully to a Bluetooth device. If that device is turned off, the BT connection is lost, understandably. I would like to reconnect with the device when it is turned back on or comes back in range. Is there an intent sent out by the system when it detects a paired BT device that I can catch with a broadcastreceiver or some other way?
I dont think there is an automatic way to do that.
However, you can have a service scanning periodically for devices and notifying your application when this specific device is found.

BluetoothAdapter notification when a bluetooth device is unboded

Function
BluetoothAdapter.getDefaultAdapter().getBondedDevices()
returns a list of available connected bluetooth devices
Can I get a notification if one of those devices suddenly becomes disconnected?
First of all getBondedDevices() returns not connected devices. These devices are paired or bonded and that's all. Connection establishes by socket.connect().
The second thing. You can register a broadcast receiver for all events such bond state changed or device is connected \ disconnected.
See more information here

Categories

Resources