• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

BLE Scan on start returning null

Bobcat1703

Lurker
Sep 24, 2019
1
0
I have used the BLE scanner code from the Android site, but everytime when I start the Scan it returns an Null. I check for all the relevant packages and ensure BT is enabled, but the BLE scan never starts. There is a log message to check the return status, oddly the handler does not seem to be working either to stop the scan after 10 seconds.

Is there something else that is required to enable the BLE scan function to work?

All the appropriate permissions have been added into the Manifest file and this is checked via the package manager:


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")
}

}


}

This is the 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>
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones