Bluetooth make device discoverable

Android Bluetooth Device Discoverable with Examples

In android, Bluetooth is a communication network protocol, which allow a devices to connect wirelessly to exchange the data with other Bluetooth devices.

Generally, in android applications by using Bluetooth API’s we can implement Bluetooth functionalities, such as enable or disable Bluetooth, searching for available Bluetooth devices, connecting with the devices and managing the data transfer between devices within the range.

In android, we can perform Bluetooth related activities by using BluetoothAdapter class in our applications. To know more about BluetoothAdapter, check this Android Bluetooth with Examples.

Android Bluetooth Enable Discoverability

To make the device discoverable to other devices, we need to start the new activity by calling startActivityForResult(intent, int) with the ACTION_REQUEST_DISCOVERABLE intent.

Following is the code snippet to enable the system’s discoverable mode to make sure that the device discoverable to other devices.

Intent dIntent = new Intent(BluetoothAdapter. ACTION_REQUEST_DISCOVERABLE );
dIntent.putExtra(BluetoothAdapter. EXTRA_DISCOVERABLE_DURATION , 300 );
startActivity(dIntent);

If you observe above code snippet, we are making sure our device discoverable to other devices using ACTION_REQUEST_DISCOVERABLE. By default, the device becomes discoverable for 120 seconds. We can extend the device discoverable duration up to 3600 seconds (1 hour), by adding the EXTRA_DISCOVERABLE_DURATION extra.

As we discussed in previous tutorial Android Bluetooth with Examples, we need to set Bluetooth permissions in our android manifest file as shown below to use Bluetooth features in our android applications.

Android Bluetooth Device Discoverable Example

Following is the example of making device discoverable to other nearby Bluetooth devices on button click in android applications.

Create a new android application using android studio and give names as BluetoothDiscoverableExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.

activity_main.xml

< RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= «match_parent»
android :layout_height= «match_parent»
android :paddingLeft= «10dp»
android :paddingRight= «10dp» >
< Button
android :id= «@+id/btnOn»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Turn On» android :layout_marginLeft= «50dp» android :layout_marginTop= «200dp»/>
< Button
android :id= «@+id/btnDiscoverable»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_alignBottom= «@+id/btnOn»
android :layout_toRightOf= «@+id/btnOn»
android :text= «Discoverable»/>
< Button
android :id= «@+id/btnOFF»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_alignBottom= «@+id/btnDiscoverable»
android :layout_toRightOf= «@+id/btnDiscoverable»
android :text= «Turn OFF»/>

Now open your main activity file MainActivity.java from \java\com.tutlane.bluetoothdiscoverableexample path and write the code like as shown below

Читайте также:  How to connect with bluetooth android

MainActivity.java

package com.tutlane.bluetoothdiscoverableexample;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity @Override
protected void onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button btntOn = (Button)findViewById(R.id. btnOn );
Button btntOff = (Button)findViewById(R.id. btnOFF );
Button btnDisc = (Button)findViewById(R.id. btnDiscoverable );
final BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
btntOn.setOnClickListener( new View.OnClickListener() @Override
public void onClick(View v) if ( bAdapter == null )
Toast.makeText(getApplicationContext(), «Bluetooth Not Supported» ,Toast. LENGTH_SHORT ).show();
>
else if (! bAdapter .isEnabled()) startActivityForResult( new Intent(BluetoothAdapter. ACTION_REQUEST_ENABLE ), 1 );
Toast.makeText(getApplicationContext(), «Bluetooth Turned ON» ,Toast. LENGTH_SHORT ).show();
>
>
>
>);
btntOff.setOnClickListener( new View.OnClickListener() @Override
public void onClick(View v) bAdapter .disable();
Toast.makeText(getApplicationContext(), «Bluetooth Turned OFF» , Toast. LENGTH_SHORT ).show();
>
>);
btnDisc.setOnClickListener( new View.OnClickListener() @Override
public void onClick(View v) if (! bAdapter .isDiscovering()) startActivityForResult( new Intent(BluetoothAdapter. ACTION_REQUEST_DISCOVERABLE ), 1 );
Toast.makeText(getApplicationContext(), «Making Device Discoverable» ,Toast. LENGTH_SHORT ).show();
>
>
>);
>
>

If you observe above code, we are making sure our device discoverable to other devices using ACTION_REQUEST_DISCOVERABLE.

As discussed, we need to set Bluetooth permissions in android manifest file (AndroidManifest.xml) to access Bluetooth features in android applications. Now open android manifest file (AndroidManifest.xml) and write the code like as shown below

AndroidManifest.xml

If you observe above code, we added required Bluetooth permissions in manifest file to access Bluetooth features in android applications.

Output of Android Bluetooth Device Discoverable Example

When we run the above program in android studio we will get the result as shown below.

Android Bluetooth Device Discoverable with Example Result

When we click on Turn ON or Turn OFF buttons, we can make the device Bluetooth turn ON or OFF and then click on Discoverable button, our device discoverable to other Bluetooth devices.

This is how we can make our device discoverable to other nearby Bluetooth devices in android applications based on our requirements.

Источник

Pair a Bluetooth device in Windows

You can pair all kinds of Bluetooth devices with your PC—including keyboards, mice, phones, speakers, and a whole lot more. To do this, your PC needs to have Bluetooth. Some PCs, such as laptops and tablets, have Bluetooth built in. If your PC doesn’t, you can plug a USB Bluetooth adapter into the USB port on your PC to get it.

Before you start, make sure that your Windows 11 PC supports Bluetooth. For more info on how to check, see Fix Bluetooth problems in Windows. If you need help adding a device without Bluetooth capabilities, see Add a device to a Windows PC.

Turn on Bluetooth

After you’ve checked that your Windows 11 PC supports Bluetooth, you’ll need to turn it on. Here’s how:

  • In Settings: Select Start >Settings >Bluetooth & devices , and then turn on Bluetooth .
  • In quick settings: To find the quick setting for Bluetooth, select the Network, Sound, or Battery icons ( ) next to the time and date on the right side of your taskbar. Select Bluetooth to turn it on. If it’s turned on without any Bluetooth devices connected, it might appear as Not connected .

The Bluetooth button in

To pair a Bluetooth device

  1. Turn on your Bluetooth device and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufacturer’s website to learn how.
  2. On your PC, select the Network , Sound, or Battery icons ( ) next to the time and date on the right side of your taskbar.
  3. Select Manage Bluetooth devices on the Bluetooth quick setting, then select your device under New devices.

The Manage Bluetooth devices button in Quick Settings.

  • Follow additional instructions if they appear, then select Done.
  • Your Bluetooth device and PC will usually automatically connect anytime the two devices are in range of each other with Bluetooth turned on.

    Tip: If you don’t see the Bluetooth device you want displayed in the list, you may need to set the Bluetooth devices discovery setting to Advanced. To learn how, see Fix Bluetooth problems in Windows.

    To pair a Bluetooth printer or scanner

    1. Turn on your Bluetooth printer or scanner and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufacturer’s website to learn how.
    2. Select Start >Settings >Bluetooth & devices >Printers & scanners >Add device. Wait for it to find nearby printers, choose the one you want to use, then select Add device.

    If you’re having problems installing your printer or scanner, see either Fix printer problems or Install and use a scanner in Windows.

    To pair a Bluetooth device using Swift Pair

    Swift Pair in Windows 11 lets you quickly pair a supported Bluetooth device with your PC. If the Bluetooth device supports Swift Pair, you’ll receive a notification when it’s nearby and you put it into pairing mode to make it discoverable.

    1. Turn on a Bluetooth device that supports Swift Pair and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufacturer’s website to learn more.
    2. If this is your first time using Swift Pair, select Yes when asked if you want to get notifications and use Swift Pair.
    3. When a notification appears that a new Bluetooth device was found, select Connect.

    Before you start, make sure that your Windows 10 PC supports Bluetooth. For more info on how to check, see Fix Bluetooth problems in Windows 10. If you need help adding a device without Bluetooth capabilities, see Add a device to a Windows 10 PC.

    Turn on Bluetooth

    After you’ve checked that your Windows 10 PC supports Bluetooth, you’ll need to turn it on. Here’s how:

    In Settings: Select Start > Settings > Devices > Bluetooth & other devices, and turn on Bluetooth.

    In action center: Action center can be found next to time and date on your taskbar. On the taskbar, select action center ( or ), then select Bluetooth to turn it on. If it’s turned off, it might appear as Not connected.

    If you don’t see Bluetooth in your action center, here’s how you can change it:

    • Expand quick actions. On the taskbar, select action center ( or ) >Expand. Bluetooth should appear here. It will appear as Bluetooth or Not connected.
    • Add Bluetooth to action center. Select Start >Settings >System >Notifications & actions >Quick actions. Go to Add or remove quick actions and turn on Bluetooth.

    Note: For more info about how to change the apps and settings that appear in action center, see Change notification and action settings in Windows 10.

    To pair a Bluetooth headset, speaker, or other audio device

    1. Turn on your Bluetooth audio device and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufacturer’s website to learn how.
    2. On your PC, select Start >Settings >Devices >Bluetooth & other devices >Add Bluetooth or other device >Bluetooth. Choose the device and follow additional instructions if they appear, then select Done. Your Bluetooth device and PC will usually automatically connect anytime the two devices are in range of each other with Bluetooth turned on.

    To pair a Bluetooth keyboard, mouse, or other device

    1. Turn on your Bluetooth keyboard, mouse, or other device and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufactur’s website to learn how.
    2. On your PC, select Start >Settings >Devices >Bluetooth & other devices >Add Bluetooth or other device >Bluetooth. Choose the device and follow additional instructions if they appear, then select Done.

    To pair a Bluetooth printer or scanner

    1. Turn on your Bluetooth printer or scanner and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufacturer’s website to learn how.
    2. Select Start >Settings >Devices >Printers & scanners >Add a printer or scanner. Wait for it to find nearby printers, then choose the one you want to use and select Add device.

    If you’re having installation issues with your printer or scanner, see either Fix printer problems or Install and use a scanner in Windows 10.

    To pair a Bluetooth device using Swift Pair

    Swift Pair in Windows 10 lets you quickly pair a supported Bluetooth device with your PC. If the Bluetooth device supports Swift Pair, you’ll receive a notification when it’s nearby and you put it into pairing mode to make it discoverable.

    1. Turn on a Bluetooth device that supports Swift Pair and make it discoverable. The way you make it discoverable depends on the device. Check the device or visit the manufacturer’s website to learn more.
    2. If this is your first time using Swift Pair, select Yes when asked if you want to get notifications and use Swift Pair.
    3. When a notification appears that a new Bluetooth device was found, select Connect.
    4. After it’s connected, select Close.

    Tip: If you’re a small business owner looking for more information on how to get Microsoft 365 set up, visit Small business help & learning.

    Источник

    Оцените статью
    Adblock
    detector