Bluetooth Helper

Allows you to access the Bluetooth of your mobile device, manage turn-on - turn off, and discover bluetooth devices around you.

Bluetooth-Helper

Getting started

Setup

Step 1. Add the required permissions to the AndroidManifest.xml file
  • Add Bluetooth permissions to your AndroidManifest.xml:
<uses-permission android:name = "android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name = "android.permission.BLUETOOTH"/>
  • if you want to access the device list, you must add the following to the AndroidManifest.xml file in the location permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Step 2. Add the JitPack repository to your build file
  • Add it in your root build.gradle at the end of repositories:
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
Step 3. Add the dependency
  • Add it in app build.gradle
dependencies {
	        implementation 'com.github.tlgbltcn:BluetoothHelper:v1.0'
	}

Usage

  • If you want to get a list of the bluetooth devices around you, you should use setPermission (true). Automatically request location permission for you
   val bluetoothHelper = BluetoothHelper(this, this)
            .setPermissionRequired(true)
            .create()  
  • You have some commitment to handle the lifecycle BluetoothHelper to work properly
   override fun onResume() {
        super.onResume()
        bluetoothHelper.registerBluetoothStateChanged()
   }
   override fun onPause() {
        super.onPause()
        bluetoothHelper.unregisterBluetoothStateChanged()
   } 
  • Turn Bluetooth on and off
   enable_disable.setOnClickListener {
            if (bluetoothHelper.isBluetoothEnabled()) bluetoothHelper.disableBluetooth()
            else bluetoothHelper.enableBluetooth()
   }
  • Instructions for locating devices
   start_stop.setOnClickListener {
            if (bluetoothHelper.isBluetoothScanning()) bluetoothHelper.stopDiscovery()
            else bluetoothHelper.startDiscovery()
   }
  • And finally, you can update the UI in the following methods using the BluetoothHelperListener interface.
   override fun onStartDiscovery() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
   }

   override fun onFinishDiscovery() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
   }

   override fun onEnabledBluetooth() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
   }

   override fun onDisabledBluetooh() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
   }

   override fun getBluetoothDeviceList(device: BluetoothDevice) {
        itemList.add(BluetoothDeviceModel(device.name, device.address))
        viewAdapter.notifyDataSetChanged()
   }

GitHub