There is one mystery that has me perplexed. Two identical sets of services are returned for each device. In the Android documentation it states that fetchUuidsWithSdp() will:
If there is an error in getting the SDP records or if the process takes a long time, ACTION_UUID intent is sent with the UUIDs that is currently present in the cache.It is conceivable that the first set of responses is from cache and the second is from the remote device.
This example also demonstrates the use of a BroadcastReceiver, Intent Filters and a scrolling TextArea.
I have included the files that have content that is key to setting up the application.
You should be able to create a new Android project in eclipse and then copy the code included below into the appropriate file. Because fetchUUidsWithSDP() is called directly this example requires ICS or higher.
Below the code attachments you will find a screen capture of what it looks like running on my Acer A100.
If you have any suggestions or questions please post them as comments.
Download Zip Archive of Android Source FilesDiscoverDevicesAndServices.java
goes in <ProjectRoot>\src\com\example\discoverdevicesandservices\
strings.xml
goes in <ProjectRoot>\res\values\
main.xml
goes in <ProjectRoot>\res\layout\
AndroidManifest.xml
goes in <ProjectRoot>
Screen Capture from Acer A100
Notes:
- Due to fetchUuidsWithSDP() this requires API Release 15
- As the SDK Emulator doesn’t emulate bluetooth this example will not run in the emulator.
- This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.
11 comments:
Is it possible to get the full package in a zip file ?
re: Anonymous
Done, see above.
Thanks, im using your code, when i receive of extre.uuid
Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bleutooth.device.extra.UUID");
uuidExtra obj is always null.so im not able to parse it. it gives nullpointerexception. what i m doing wrong?
I'm seeing null for that on some devices and broadcasts, but not for others. I'd recommend just checking for null and skipping that item. That worked for me to prevent crashing enough to list other valid entries.
I wanted to know if I implement a thread in one activity and once the thread get starts and if I change the activity, Whether the thread also goes into On Pause and or it gets killed….? If it happens any other way to handle this..? I am running Bluetooth connection in a thread and if I change the activity then the connection is getting lost.
shilpa,
I believe it gets paused but I don't have an Android device at the moment that I can test with.
You might try looking at making it a service.
digitalhack
Luke,
It should list devices that are in the vicinity.
digitalhack
Hello,
This is really nice code. I am also use this code.
Thank You
error:bluetoothdevice cannot be resolved to a type
for MAC address cheange it to your bluetooth MAC address. it works
I savour, result in I discovered just what I was taking a look for.
You've ended my four day lengthy hunt! God Bless you man. Have a nice day.
Bye
Post a Comment