My task is, to detect android devices, when reaching the propximity of some BT-Scanners(My PC)
Instead of discovering all visible devices:
all Device BT MAC-Adresses are stored at my PC
Can i look for special devices with the given MAC-adress without turning on discoverability on the android device? I don't want to turn on the discoverability all the time, so maybe I can check with the special MAC-adress for devices. Like the "ping" function in network technik?
I hope you understand my problem and hope for solutions.
If it is just one (or a few) device , the quickest approach is to try to do a connect to it to determine if it is available.
Related
What I want to know:
I'm wondering how the android system (like Android smart phone) auto-connects to devices which is paired before.
For example, I pair my bluetooth headset with my android smartphone in the procedure of "turn on scanning/advertising -> click pairing" on day 1. And when I turn on advertising on my headset, it connects automatically on day 2, 3, 4, and so on. The point is, I don't have to make my smartphone scan again to find my (paired) headset.
I can't understand how android system finds that the paired device is turned on. Does the android system scan periodically in background? Even if I don't click "scan" button?
Why I ask:
I want to make my app autoconnect to customized BLE device, after make pairing. I succeed to make pairing(bonding) with createBond() method, but after that, I couldn't find how to make autoconnect. I know I can turn on autoconnect function like this way, connectGatt(XXX, true, XXX), but this autoconnect function doesn't work when the BLE device is disconnected a few days.
So I want to make my app works like android system and Bluetooth headset. But I couldn't find how android bluetooth system works even I dig AOSP codes.
I found many questions (here and here) about problems like mine but there were no answers.
Thanks in advance.
Update:
I found that bt_btif gets activated (with LG smartphone and Nexus 5) when the paired headset is turned on (start advertising). But bt_btif doesn't get activated with my custom BLE device... What can I do?
Executing connectGatt(XXX, true, XXX) is the correct way to go. gatt.connect() also starts an auto connect. Once you execute that, your phone will scan for the device and once it appears it connects to it. The only thing that interrupt this call is either if Bluetooth is turned off on the phone or if your app process is being killed. To avoid your process from being killed, let your app have a foreground service.
One gotcha however, there is a bug in Android which will sometimes make auto connect do a direct connect instead (and cancel after 30 seconds or so). See https://code.google.com/p/android/issues/detail?id=69834. You need to use reflection to avoid this bug.
I think I found a solution.
First, the solution for my question: Android smartphone seems to detect state changes of nearby bluetooth devices from the hardware sides. When the paired bluetooth headset starts to advertise, a callback in HAL (I think) is called.
So I made my app to connectGatt with autoConnection=True to the device that I want, by using MAC address, when the activity is started (in onResume() of MainActivity).
The connection would fail if my BLE peripheral device is not advertising. But the device auto-connects when it starts advertising, because the autoConnection parameter was set to true.
I've done a similar app and i didn't have problem with that. As a last resort for your problem, i would suggest writing the BLE Device Address in a simple DB table and manually connect to it. I've made something like this here
. It is no best practice code, but i hope you can find ideas for you solution.
Is there any simple way to check if an existing BluetoothDevice object is still reachable without performing a new scan?
I am writing an app that scans for devices and goes through connecting to each one at a time to populate a device list with some information about each device, which I acquire while connected to the device. Every now and again, I would like to go through my device list and remove any devices that are no longer active/in range. What is the best way to do this?
You could try to connect to the devices to find out if they are still around, but keep in mind, that devices will change their mac address from time to time.
I would suggest to use scanning to find out if the devices are still around. Also you may want to add an identifier into the advertisement packages, so you don't need to relay on the mac address.
i have been trying to develop to an app that scans for nearby WiFi devices. I found lots of similar apps in the market, but they scan only for nearby hotspots(access points). Is it possible to scan and find devices that have just turned WiFi alone ON.
If so please say me how to (the code snippet) .. thanks in advance ..
There are 2 parts to the answer.
Most WiFi devices can operate in AP mode (if proper software installed) and in this case they will appear as a normal AP to anyone scanning around.
If you want to find other WiFi devices that aren't in AP mode you'll need to switch your device to promiscuous (sniffer) mode. This needs to be supported by your device driver/firmware. Operation in sniffer mode causes the device to indicate all packets in the air, including those that aren't addressed to you. If your device supports this op mode, you'll need to instruct it by sending relevant command(s) - that's a proprietary interface that varies per device SW vendor.
These nearby devices must send some signal, which your device can receive in order to detect them. Normally they dont do that I think. But if you can install an app on the other devices, this app could send a broadcast signal, which your device can pick up.
If there is no standard signal, for which you can listen, it becomes difficult. You have to build some kind of network sniffer.
A sniffer is the process of reading a direct port, in this case the listening port wifi. Read a port means pointing to a specified address and read the same buffer. This process is at low level, you must do it in pure C and compile in NDK. If you know of these issues is complicated.
Sometime bluetooth is enabled but no any device connected. I want to know how to determine no device connected to bluetooth. The following is example.
1. Headset disconnected.
2. No file transfer.
Probably, You can just call getBondedDevices() it will give You all connected devices.
However, I think You might actually need to know not only connected (paired) devices, but if there's any active connection with one of them. Unfortunately (and this question seems to confirm it) there's no way to find it out afaik.
I was reading this
http://developer.android.com/guide/topics/wireless/bluetooth.html#QueryingPairedDevices
which is allot of help on how to pair,connect to a bluetooth device.
I have a situation where I have several BT devices that are in Non-Discover mode always. I know the MAC and the PIN of these devices. IS there a way in Android Dev to manually add devices to the PAIRED list so i can just use the connect as a client.
I understand this maual is written allot for V3. i think i will need to do this on 2.0 ; 2.1- has anybody done this before?
Basically these devices I want to connect to are power saving modules I used pre built BT modules to monitor daylight, another one humidity, etc.. every 3hrs or when interrupted and runs of a single battery for months. So turning off divcory on server saves immense power and prevents other people trying to connect and waste battery.
Not sure what you mean by "manually": Do you mean "manually" as in GUI/user interaction, or "manually" as "I do it in my own application code"?
Some suggestions though:
If you can make your BT devices discoverable at all, you could do it this way:
Make your BT device discoverable
Let Android search for and find the device and then initiate a connection
Android will ask for the PIN for pairing with the device; enter the PIN.
Once pairing was successful, Android stores the pairing information for future use, so that you can
Make your BT device invisible again.
From then on your app should be able to connect to the BT device at any time without further pairing operations.
If the said is not an option for you, maybe you want to go another way:
In current Android versions there are different API routines implemented which are neither documented nor exposed in the normal SDK. A hack kind of solution may be to use some of these "hidden" ("#hide"...) APIs, either via reflection or via modification of your SDK installation.
But be aware that this is always a hack and it may work on a specific device with a specific version of Android and is likely to break your app on another device and/or any other Android version.
Having said that, here comes some reference:
Example of how to access "hidden" bluetooth API.
Then, have a look at the source code for android.bluetooth.BluetoothDevice, e.g. here.
In there, public boolean createBond(){...} may do what you want.