This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Can't display a list of visible nearby Bluetooth devices

 
Greenhorn
Posts: 4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.
So I'm trying to display a list of visible bluetooth devices but it's not working unless I hit refresh in the device's bluetooth manager, and then its showing the same device name and address multiple times.

Here's the main activity code


And here's the manifest file code


And the layout code

To make sure startDiscovery() was working properly I added a TextView that shows a message when the device is discovering, but that's not displaying anything either unless I hit refresh.
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shakku,

Welcome to the Ranch!

I have not tried compiling your program, but I'm curious to know about this part:



Why is that part needed?  It seems to be overly complex and the lack of {} makes following the blocks difficult.
 
Shakku Singh
Greenhorn
Posts: 4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris Barrett wrote:Hi Shakku,

Welcome to the Ranch!

I have not tried compiling your program, but I'm curious to know about this part:



Why is that part needed?  It seems to be overly complex and the lack of {} makes following the blocks difficult.


Hi Chris
I was just trying to make sure that entries in the list are not repeated, although its not working.
 
Master Rancher
Posts: 4663
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters I'd stick some logging in there to try and trace what's going on.

The BluetoothAdapter should only send a FOUND for each device once, so logging might help to show whether that is the case or not.

Remove the check for whether the device is already in the list, since that should not be needed.
Also you should not have to manually get your android device to search.  If you do that then yes, you may well get duplicates.

Can you register for the START and STOP events from the adapter as well?  Log them just to track things.
That might give you some more info.
 
Shakku Singh
Greenhorn
Posts: 4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:For starters I'd stick some logging in there to try and trace what's going on.

The BluetoothAdapter should only send a FOUND for each device once, so logging might help to show whether that is the case or not.

Remove the check for whether the device is already in the list, since that should not be needed.
Also you should not have to manually get your android device to search.  If you do that then yes, you may well get duplicates.

Can you register for the START and STOP events from the adapter as well?  Log them just to track things.
That might give you some more info.


So, as you advised, I used logging, removed the check for device repetition and added the START and STOP action to the intent filter, but I'm getting the same results.
Here's the modified code

I tested it on Lollipop and Jelly Bean platform and on both I have to manually scan for devices, after which I'm getting proper logging messages, although, on Lollipop the devices are repeating, generating log message every time a device is added to the list, whereas on Jelly Bean one device is displayed only once.
Another thing that I don't understand is that the two conditions

and

are not being satisfied as I'm neither getting the log message nor anything on the TextView.
 
Shakku Singh
Greenhorn
Posts: 4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i finally figured out what the problem was.
I needed to add a listener to track the state of the Bluetooth adapter. I was calling the startDiscovery() method but it was not synchronized with the enable() method call, therefore I had to start the discovery manually.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic