The Android platform support Bluetooth wireless technology, which allows exchanging the data between nearby devices. An application can discover or scan available devices by using Bluetooth API.
If we want to use Bluetooth features, we must declare permissions. The BLUETOOTH
permission enables Bluetooth communication and BLUETOOTH_ADMIN
permission allows discovering and pair Bluetooth devices.
Also, we must declare ACCESS_FINE_LOCATION
permission because a Bluetooth scan can be used to get information about user location.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application>
...
</application>
</manifest>
In the layout XML file, we added the Button
element which will used to start scanning for Bluetooth devices.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Start" />
</RelativeLayout>
Using the method ContextCompat.checkSelfPermission()
we check if required permission was granted. If not, then we request permission by calling the method ActivityCompat.requestPermissions()
.
We need to create and register a BroadcastReceiver
. The onReceive()
method is invoked when a Bluetooth device is discovered.
The BluetoothAdapter.getDefaultAdapter()
method is used to get the Bluetooth adapter. By using the startDiscovery()
method, we start discovering devices. The discovery process usually takes about 12 seconds. After that, we can start discovery again if needed.
package com.example.app
import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
private val permission: String = Manifest.permission.ACCESS_FINE_LOCATION
private val requestCode: Int = 1
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
registerReceiver(receiver, filter)
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
myButton.setOnClickListener{
if (!bluetoothAdapter.isDiscovering) {
bluetoothAdapter.startDiscovery()
}
}
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
}
}
override fun onDestroy()
{
unregisterReceiver(receiver)
super.onDestroy()
}
private val receiver = object : BroadcastReceiver()
{
override fun onReceive(context: Context, intent: Intent)
{
if (intent.action == BluetoothDevice.ACTION_FOUND) {
val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
as BluetoothDevice
val deviceName = device.name
val deviceMacAddress = device.address
Log.d("MY_APP", "$deviceName $deviceMacAddress")
}
}
}
}
Leave a Comment
Cancel reply