• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> How to Programmatically Scan or Discover Android Bluetooth Devices

How to Programmatically Scan or Discover Android Bluetooth Devices

February 20, 2014 by Lorensius Londa 14 Comments

Through Android Bluetooth API, developers can access most of bluetooth functionalities and let the applications wirelessly connect to bluetooth devices. Before connecting to a device, an application must discover or scan available bluetooth devices, request pairing and connect to the device.

android bluetooth discovery    android bluetooth scanning

(apk + source code at the bottom of this post)

To discover a device, first get the bluetooth adapter by calling BluetoothAdapter.getDefaultAdapter(). 

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

To start discover, simply call the startDiscovery() from bluetooth adapter. This process is asynchronous so it will return immediately. To catch the discovery process, we can register a BroadcastReceiver with ACTION_FOUND,  ACTION_DISCOVERY_STARTED,  ACTION_DISCOVERY_STARTED. For each device found, the intent will carry extra field  EXTRA_DEVICE containg the BluetoothDevice object.

IntentFilter filter = new IntentFilter();

filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, filter);
adapter.startDiscovery();

The receiver:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
	    public void onReceive(Context context, Intent intent) {
	        String action = intent.getAction();

	        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
	        	//discovery starts, we can show progress dialog or perform other tasks
	        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
	        	//discovery finishes, dismis progress dialog
	        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                       //bluetooth device found
	        	BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

	        	showToast("Found device " + device.getName());
	        }
	    }
	};

And, don’t forget to unregister the receiver on Activity’s onDestroy method:

@Override
	public void onDestroy() {
		unregisterReceiver(mReceiver);

		super.onDestroy();
	}

Download the sample APK and source code from github

[ad#ad-720×90]

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Programmatically Pair or Unpair Android Bluetooth Device
  2. How to Select and Crop Image on Android
  3. Solution to Make an Android App Visible to All Devices in Google Play When Using Specific Feature
  4. How to Programmatically Show and Hide Soft Keyboard in Android

Filed Under: Android Tagged With: Android, bluetooth, discovery, scanning

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Ivan says

    February 21, 2014 at 2:39 am

    How rotation of screen (or other configuration change) affect bluetooth discovery and scanning dialog? Would you save already found devices nad list them again on activity recreation or just start discovery again with empty list (which is not actually nice solution but more something like workaround). Thank you…

    Reply
    • Lorensius Londa says

      February 21, 2014 at 7:08 am

      Hi Ivan,

      I think it is good to save the already found devices. Then after the activity recreated again, save them (from onSavedInstanceState) on the list then start rescanning. We don’t have to use an empty list, just use the saved list and when a new device found, check if it already exists in the list or not. Also it is a good practice to allow user to cancel scanning using progress dialog with a cancel button.

      Reply
      • Ivan says

        February 22, 2014 at 4:16 am

        Thanks for answer.. I did it already like that but I mess up something with fragment lifecycle and on discovery finished callback to dismiss dialog.. Sometimes I get crash cause dialog is not there and broadcast is initiating it’s dismission..btw I solved canceling BT discovery by dismissing dialog 😀

        Reply
        • Lorensius Londa says

          February 22, 2014 at 11:39 am

          I think it is a good practice to check the progress dialog for its null value before dismissing it.

          Reply
  2. fran says

    March 14, 2014 at 10:28 am

    thank you, I’m new programming with wireless technology so this is what I need
    🙂

    Reply
    • Lorensius Londa says

      March 14, 2014 at 11:43 am

      You’re welcome..happy coding..;)

      Reply
  3. Dhan says

    February 2, 2015 at 4:27 pm

    thank you very much sir.. this app was everything i wanted to know… thank you thank you…. you save me really ^_^

    more power and god bless… btw can we friends in FB? here’s mine.. gersam13@yahoo.com (FB Account)

    thanks again

    Reply
  4. Manish Patel says

    February 17, 2015 at 6:02 pm

    Hi Thanx for your tutorial. It’s perfectly working in my case. Now i want to develop a Bluetooth chat type of app. I successfully developed but in server side code i found one issue. Server want able to connect to my Client using serversocket.accept() method. Do you have any idea to overcome this issue? any help!!!

    Reply
  5. vasthav says

    February 18, 2015 at 11:50 pm

    This is awesome. I been working on something like this for long time
    but was unable to make it work. This help me a lot. Thank you 🙂

    Reply
  6. Aditi says

    September 17, 2016 at 2:34 pm

    Thanks for sharing the code. Here are my observations : this sample app working properly for Android O.S 5.0 L but it not showing any devices after scanning in 6.0 Android M. I am also looking into issue possibility can be we need to add some extra permission. Can you please update it if you get solution for Android M? Thanks.

    Reply
  7. kevin says

    November 28, 2016 at 4:37 am

    hey Londa,
    awesome app.
    I need your help to develop a one click app (no interface) that automatically executes as follows on tap on an android device:
    -scan all available bluetooth device
    -temporary store available bluetooth devices
    -auto initiates a connection on the available bluetooth devices (one by one with a 10sec time out to move on to the next available device )
    -if the other device accepts the connection, pair and save it among paired devices.

    thats all. let me know if you can help

    Reply
  8. Giacomo says

    August 10, 2017 at 10:36 pm

    Hi, really interested in this code, but on my Xiaomi doesn’t work, It can’t find any device. I can’t understand why. I tryed a lot of codes but this is the most complete and still not working for me. Could you help me? Thanks

    Reply
  9. DOTRINH.COM says

    November 10, 2017 at 11:54 am

    NICE POST!
    THANKS!

    Reply
  10. Hussain says

    January 10, 2018 at 3:19 pm

    Truly a Legend (y)

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in