I am having problems with BLE scan functions, it constantly returns a null when I try to execute "
bluetoothLeScanner?.startScan(blescancallback)". I have added all the right permissions into the manifest file and tried different tablets, but it’s still returning null.
Within the code, I check for bluetooth and bluetooth_ble available and enable bluetooth.
I am using studio 3.5 which is fully upto date
package com.example.blesandpit
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.le.BluetoothLeScanner
import android.content.Intent
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import android.util.Log
import android.os.Handler
import android.content.pm.PackageManager
class MainActivity : AppCompatActivity() {
private var mbluetoothAdapter: BluetoothAdapter? = null
private lateinit var m_pairedDevice: Set<BluetoothDevice>
private val REQUEST_ENABLE_BLUETOOTH = 1
private val bluetoothLeScanner: BluetoothLeScanner? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Toast.makeText( this,"App started", Toast.LENGTH_LONG).show()
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this,
"BLUETOOTH_LE not supported in this device!",
Toast.LENGTH_SHORT).show();
}
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Toast.makeText(this,
"BLUETOOTH not supported in this device!",
Toast.LENGTH_SHORT).show();
}
mbluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mbluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText( this,"Device does not support Bluetooth", Toast.LENGTH_LONG).show()
} else {
if (!mbluetoothAdapter!!.isEnabled) {
// Bluetooth is not enable :)
Toast.makeText( this,"Bluetooth is not enable", Toast.LENGTH_LONG).show()
}
}
if(!mbluetoothAdapter!!.isEnabled)
{
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH)
Toast.makeText( this,"Bluetooth is now enabled", Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText( this,"bluetooth is enabled", Toast.LENGTH_LONG).show()
}
scanstart.setOnClickListener {
startbtscan()
val handler: Handler? = null
handler?.postDelayed({
stopbtscan()
}, 10000)
}
scanstop.setOnClickListener {
stopbtscan()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == REQUEST_ENABLE_BLUETOOTH){
if (requestCode == Activity.RESULT_OK){
if(mbluetoothAdapter!!.isEnabled){
Toast.makeText(this, "Bluetooth has been enabled(1)", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Bluetooth has been disabled(2)", Toast.LENGTH_SHORT).show()
}
} else if (resultCode == Activity.RESULT_CANCELED){
Toast.makeText( this, "Bluetooth enabling has been canceled(3)", Toast.LENGTH_SHORT).show()
}
}
}
fun stopbtscan(){
var result = bluetoothLeScanner?.stopScan(blescancallback)
Toast.makeText(this, "Stop scanning", Toast.LENGTH_LONG).show()
Log.i("Log", String.format("Stop BT Scan\" = %d", result))
}
fun startbtscan() {
var result = bluetoothLeScanner?.startScan(blescancallback)
Toast.makeText(this, "Start scanning", Toast.LENGTH_LONG).show()
Log.i("Log", String.format("Start BT Scan\" = %d", result))
}
private val blescancallback = object :ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
super.onScanResult(callbackType, result)
Log.i("Log", "onScanResult")
Log.i("Log","onScanResult: ${result?.device?.address} - ${result?.device?.name}")
}
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
super.onBatchScanResults(results)
Log.i("Log", "SonBatchScanResults")
Log.i("Log","onBatchScanResults:${results.toString()}")
}
override fun onScanFailed(errorCode: Int) {
super.onScanFailed(errorCode)
Log.i("Log", "onScanFailed")
Log.i("Log", "onScanFailed: $errorCode")
}
}
}
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blesandpit">
<!--
Reference
https://developer.android.com/guide/topics/connectivity/bluetooth-le
-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>