• 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

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>

OxygenOS update 9.0.17 for 6T & 9.0.9 for 6

OnePlus has begun the OTA, (Over The Air), rollout of their new stable OxygenOS firmware, 9.0.17, for OnePlus 6T and McLaren and 9.0.9 for OnePlus 6 users.

As always, there will be a limited roll out initially, followed by the global roll out in a few days.

Changelog
  • System
  • Fixed the crash issue with Screen Recorder
  • Re-activated the calling function after the network has been reset.
  • This update will improve the overall system performance to prepare for later versions
Android Security patch = August 2019

Camera version is still 3.0.16

This is the 12th official, stable, update for the 6T in 322 days
(The T-Mobile (USA) carrier minority variant has only received 8 updates in 331 days)

(N.B. OxygenOS 9.0.17 is not available for the T-Mobile (USA) carrier minority variant)

I believe I'm hacked

Make sure you use an uncompromised device to change your Google password. I'd suggest removing your Google account from the phone before resetting, to avoid problems with factory reset protection (a password change and reset close in time is a pattern often seen when a phone is stolen, so could result in you being locked out for a couple of days). Don't enter your new password on the phone until you know it's clean, and enable two factor authentication on your account.

The tricky bit is restoring data and apps after a reset. If the problem is an infected app you don't want to just restore it. So some care with what you restore after a backup and reset is needed.

Root? Hotspot despite service provider

Mikedt I applaud you.

Foxfi won't work but only because it's old software for older times. It seems to have had a hay day of Android 4.0, 5.0, 6.0 etc vintage.

But the same company makes PDANet+. I may have some of that capitalization wrong. Regardless, it works as a wireless hotspot to my Win10 laptop. BTW my phone is still on Android 8.0.

It has the ability to connect via USB debugging or completely wireless. I have successfully installed it on my phone and laptop and have cruised the internet on my laptop wirelessly. I didn't try the corded USB debugging.

It does cost $10 for the phone app. But it's worth that to be wireless.

The only downside is I can't get the Roku into this loop. But that's okay because I can get almost all that it can do with the laptop.

Thanks again Mike.

Help When I deactivate an app instead of deinstalling it can I then witch of the ads of that app?

If this is incorrect, someone please enlighten me!
Well, as I never have registered with my phone or logged in to download from Google, I would say there is no need to register.

The fact that you're not seeing anyone else complaining about it, and its rating is high, suggest to me that it may not actually be the source of your problem. If others were experiencing what you were, they'd most likely speak up!
Yes, very strange. Since I removed that app the spam ads are not shown anymore at least.

Android 10 security updates via play store

whether Google are mofing to more of a Windows 10 style of support with ongoing security updates and yearly or half yearly 'feature updates'.
Considering everything Google does, from its own servers, to its cloud, to its laptops, to its mobile OS, is Linux, if they move toward anyone's style of support it won't be window$!

Most Linux distributions, such as the most famous one, Ubuntu, have long been on twice-yearly version upgrades plus continual security and other updates. That's who I'd expect Google to follow.

Lost contacts

Yeah, we need more information to offer any advice.

Though frankly if it was not synced or backed-up anywhere and you deleted it from the phone I really don't think there's anything you can do to restore it. Though for most people these days if you know who you are looking for there is some way of finding out how to contact them.

Root access

I'd like to point out that Motorola is one of the root friendly brands still left. Just like a Pixel you can go to the Motorola website to get instructions on how to unlock the bootloader for your model. Rooting is easy from there.

It's been a long time since I've used a one-click app to root a phone. Those things are risky anyways. Some come with weird chinese root exploits and I just don't trust them.

Surely you must have a friend that will let you use their PC for a few minutes. By the way, you can go "big boy galaxy" and still be in the same predicament.
Yep my note 10+ is locked down pretty tight..... there's no rooting this puppy unfortunately.

Android 10 update for Samsung

Two years of version updates (which these days means 2 major updates) was for a very long time the norm for android manufacturers. Samsung are nothing exceptional in that, good or bad.

Of course they can change their mind as they like. Google did: they only promised 2 years for the first Pixel when it was released, and 3 years for the Pixel 2 a year later, but they clearly extended that policy retrospectively to the older model. But I've not heard Samsung make such a promise about any of their phones.

Wakelocks?

I've been especially pleased with the battery life on my V40, untill recently.
Battery stats indicate that "Held Awake" percentages are increasing.
No new apps recently to explain the increase in wakelocks either.

What do I look for to find out what's happening. I had thought I had my settings right.

Did I miss something?

If you suspect you have a wake lock, connect your device to a pc and fire up a terminal and run these commands...
Bash:
adb devices
adb shell dumpsys power | grep -i wake_lock
Post the output here wrapped in code tags.

The first command is just to determine if your phone is recognized by ADB. The second command does all the magic.

If there's any active wake locks, this will catch them. If the wake locks are not active at the time of running the command, then it will not catch them.

Filter

Back
Top Bottom