I need to continuously poll for getting bluetooth devices connections/disconnections in my activity to update a listView with the currenctly available devices.
I use btAdapter.startDiscovery() but it is not permanent ... how can i correctly get the on/off events for the devices?
I would suggest using a broadcastreceiver to listen for the specific events you are talking about. You could even fire off another discovery mode after it comes out of its current discovery mode to have it keep scanning
BluetoothAdapter.ACTION_DISCOVERY_FINISHED
BluetoothDevice.ACTION_ACL_CONNECTED
BluetoothDevice.ACTION_ACL_DISCONNECTED
You can use the intent extra's to be able to get the name (ect) from the device that connects
I would read http://developer.android.com/guide/topics/wireless/bluetooth.html and
http://developer.android.com/reference/android/content/BroadcastReceiver.html
Related
Can I from my application force android to reconnect to cellular network immediately?
If yes how to do this?
Apparently the functionality as of Android 5 has been moved to system apps only. How to enable mobile data on/off programmatically
But you can still register a listener for network changes.
https://developer.android.com/training/basics/network-ops/reading-network-state
When you get the onLost() event you can start a Wireless Setting Activity so the user can do it manually.
//As seen in the link above.
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
This will be a common occurrence so get use to it. For example you can createBond() for a Bluetooth device, but you can't removeBond() anymore. Thus you have to send the user to the Bluetooth setting activity for the user to
"forget" about the device.
I followed almost exactly the Google Tutorial here https://developer.android.com/guide/topics/connectivity/wifip2p.html up to the Discovering Peers section (with the only change being that I put mChannel as an input param for the discoverPeers method as a variable named 'channel' wasn't created), and I find that onSuccess gets called but no broadcast is sent to the receiver.
I assume the receiver is initialised as it broadcasts WIFI_P2P_STATE_CHANGED_ACTION where I was also able to check that my device is WiFi P2P enabled (I'm using a Oneplus 3 running on Android Nougat) as well as WIFI_P2P_CONNECTION_CHANGED_ACTION and WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.
Help is much appreciated!
In my application I need to start some BLE scans, get the results and then show them in a list or in a grid.
When i start the scan my device start to look for advertisements basing on which ScanFilters I wrote.
Every time a Pheriperal is found I the application trigger the scan callback method and i can add the new device into the list.
If i got an error the app trigger the callback method and i can tell the user about the broblem.
Example
Imagine the Scan going on for 30s.
At the moment it starts i get 3 Ble Pheriperal.
At second 15 one of them stop to advertise and turn bluetooth off.
At the end of the scan my list will have 3 Ble devices but i will not be able to connect to one of them.
How do I know which of them is the one which turned off?
Where do i get his status?
Thanks for the help.
You can try this, technically it is possible as per the documentation. in onScanResult callback, check for the callbackType parameter. If it is CALLBACK_TYPE_MATCH_LOST then get the device from the results and remove it from your cache.
I have a bluetooth device , I want to know how to obtain the method when connected Bluetooth device is out of range
like code
//if bluetooth is Not in the range of connection
{
}
Please give me some solution
To check whether a bluetooth device is connected or not you can use intent filters to listen to the ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECT_REQUESTED, and ACTION_ACL_DISCONNECTED broadcasts. For more details please check this post How to programmatically tell if a Bluetooth device is connected? (Android 2.2)
There is no internal method like DeviceNotInRange() {} hence you need to work it out by creating your customized method. You need to create a method that keep searching on a regular interval and when device is not in range, you can raise an Alert or sound for intimation.
How can i know if a BluetoothSocket is still connected to the endpoint? How can i detect if the socket has been disconnected by the endpoint?
Thanks
In my apps, I keep track of I/O errors. If a successful read() takes place, then I reset the counters. If the error counters go up high enough (4-5 is usually a good number) then I consider the connection dead, and proceed to tear it down and re-build it.
The SDK talks about a state change intent, but I'm not clear whether it's referring to a specific connection, or the bluetooth adapter itself here:
Optionally, your application can also
listen for the ACTION_STATE_CHANGED
broadcast Intent, which the system
will broadcast whenever the Bluetooth
state has changed. This broadcast
contains the extra fields EXTRA_STATE
and EXTRA_PREVIOUS_STATE, containing
the new and old Bluetooth states,
respectively. Possible values for
these extra fields are
STATE_TURNING_ON, STATE_ON,
STATE_TURNING_OFF, and STATE_OFF.
Listening for this broadcast can be
useful to detect changes made to the
Bluetooth state while your app is
running.