android - Detect all Bluetooth devices (headsets, phones etc) nearby, without forcing the devices in discoverable mode -
my goal:
to detect nearby bluetooth devices(phones, headsets etc.) android application.
here's nice example developer.android.com discovers bluetooth devices nearby along list of paired devices.
my situation:
i had 2 bluetooth headsets turned on , not being detected after successful bluetooth scan! dig problem , somewhere found bluetooth headset needs switched pairing mode in case of being detected android.
to switch headsets in pairing mode, had long press power button while turning them on. , yes, finally, bluetooth headsets detected scan application.
my problem:
i want headsets detected automatically without switching them in pairing mode. couldn't find way detect nearby bluetooth devices turned on.
so, here's found after reading blogs, threads, documentations, answers etc. regarding android's bluetooth discovery. thought might useful post findings here.
so before starts reading, want clarify that, i not find way detect bluetooth device nearby turned on not discoverable.
detect nearby bluetooth devices
my primary target detect nearby bluetooth devices. so, have bluetoothadapter
class in android has startdiscovery
function scan bluetooth devices nearby. hence, doesn't serves purpose, bluetooth devices need discoverable detected bluetooth scan android. want detect bluetooth devices nearby without forcing them discoverable. started searching solution achieve goal.
i started studying bluetooth protocols , found underlying protocols used bluetooth detection, connection , pairing. android, found no api discover/detect bt devices turned on not in discoverable mode.
bluetooth devices class 0x00 ignored
digging detection problem without forcing bluetooth devices discoverable mode lead point found bluetooth device advertises class 0×00
ignored automatically while running scan nearby bt devices. problem stated in several places. here have listed some.
i tried find work-around of problem , yes, found one, though documentation , reviews along work-around says, doesn't work kind of android devices. anyway, alternate scan same default bt scan, additional works. reads android log after successful bt scan , check if there's bt device skipped through out operation. simple hack described here . there's google group discussing same work-around here.
the work-around above doesn't solve problem either. bluetooth headsets need discoverable being detected bt scan. not skipped while android scanning nearby bt devices. not found.
bluetooth page scan
however, started searching solution again , found interesting again! stackoverflow answer says, it possible know whether bluetooth device around, if in undiscoverable mode, knowing full mac address in first place. i'm quoting again answer future reference,
the technique try page request, sending 6 bytes composing seeked bluetooth host mac identifier. page request allows 1 connect bluetooth slave when knowing bt addr. devices in undiscoverable mode not respond inquiry scan (device discovery intent), respond page scan used device wanting connect known device.
i found hope , started searching for, how can initiate page scan in android failed time too. couldn't find kind of apis initiate page scan. android documentation of bluetoothadapter
class, came know that, when bt scan starts,
this involves inquiry scan of 12 seconds, followed page scan of each new device retrieve bluetooth name.
there indication of page scan in android documentation, but, surprisingly, there no other documentation regarding page scan anywhere. put stackoverflow question inquiry.
can "fool" android appending list of btids "already paired" list?
no, did not find way so. read how android's bluetooth pairing works. here nice flow diagram along detailed reading manual. trying use android's hidden apis , use hidden apis using reflection. started hidden apis might use serve purpose. but, unfortunately, failed find hidden apis add bt device in android's paired list programatically.
here helpful links check bluetooth pairing, hidden apis , how invoke them via reflection future reference.
- how initiate pairing
- how use reflection work android's hidden apis
- here's google group discussion followed, people facing same problem we've here
so pair bt device when in discoverable mode , not in paired list in android. in case, used bluetooth headset. turned on , put in discoverable mode. bt address (mac address) of headset known. called function createbond
code using reflection , worked perfectly. device added in "already-paired" list of android successfully. while roaming around solutions serve purpose found interesting...
interesting observation paired devices
i found that, android preserves paired device in memory if remove already-paired list. let share case study along findings here better understanding. in case headset paired previously.
- unpaired/forget headset android
- then turned headset off.
- then started scanning bluetooth devices nearby
- of course, headset not found
- but went through android logs , found that, android remembers change of bonding state (paring state) of headset.
- it scanned bonded headphone , reported remote device down
here log printed along mac address of headset paired before , unpaired.
prev_bond_state = 11 // bond_bonding (bluetoothdevice.java) bond_state = 10 // bond_none reason = 4 // unbond_reason_remote_device_down
then again, ran same test, time turned bluetooth headset on (not discoverable). headset not found in case too, interesting thing is, time different log printed.
prev_bond_state = 11 // bond_bonding (bluetoothdevice.java) bond_state = 10 // bond_none reason = 2 // unbond_reason_auth_cancelled
the reason
changed , hence can understand android tried connect bluetooth headset. headset not in discoverable mode, yet android find , tried connect in paired list before.
this log printed while bluetooth scan running adding action_bond_state_changed
action intent.addaction
.
can android used beacon?
yes, in case android needs advertise ble too. new in android , not devices support functionality till now. here list of devices support bluetooth le advertisement capability. but, bluetooth le advertisement has low power consumption along high performance, think next big thing in android.
here helpful links future research.
- can android act ibeacon?
- android 4.4 ibeacon
- android beacon library
- altbeacon library in github
- learning more altbeacon
- rooting phone advertise
Comments
Post a Comment