Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startLeScan: cannot get BluetoothLeScanner

I am trying to implement a application that starts the BLE scan when the person with the phone moves, and automatically switches off if there is no movement for over 10 seconds ,while the movement detection works fine something is going wrong with the BLEscanner here is the code

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends Activity implements SensorEventListener {
    private TextView textView,textView1;
    private SensorManager mSensorManager;
    private Sensor mStepDetectorSensor;
    private BluetoothAdapter BA;
    private BluetoothManager manager;
    private BluetoothDevice device;
    long initialseconds;
    public static String Address;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);
         mStepDetectorSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        BA = manager.getAdapter();
        // BA = BluetoothAdapter.getDefaultAdapter();
        timeup t = new timeup();
        t.start();
}


public void onSensorChanged(SensorEvent event) {
    long initialtime=System.currentTimeMillis();
    initialseconds=initialtime/1000;
    if(!BA.isEnabled()){
        BA.enable();
        discoverBLEDevices();
    }
}

@SuppressLint("NewApi")
private void discoverBLEDevices() {
    Log.e("HIT", "HIT");
    BA.startLeScan(mLeScanCallback);
}

@SuppressLint("NewApi")
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @SuppressLint("NewApi")
    public void onLeScan(final BluetoothDevice device, int rssi,
                         byte[] scanRecord) {
        Address = device.getAddress();
        String Name = device.getName();

    }
};


@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{

}
@Override
public void onResume()
{
    super.onResume();

    mSensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
public void onPause()
{
    super.onPause();
    mSensorManager.unregisterListener(this);
}


public class timeup extends Thread{
    public void run(){
        boolean always=true;
        while(always) {
            long currenttime=System.currentTimeMillis();
            long currentseconds=currenttime/1000;
            if(currentseconds-initialseconds>10){
                BA.disable();
            }
        }
    }
}

The error shows in Log as:

timeStamp/com.domain.www D/BluetoothAdapter: startLeScan: cannot get BluetoothLeScanner
like image 463
aditya ramesh Avatar asked Mar 23 '26 07:03

aditya ramesh


1 Answers

Just ran into this same exact issue while working on a Cordova problem. I looked all over to find the answer (including this question), before I realized the problem. I had forgotten to turn on the bluetooth on my phone.

So, either turn on your bluetooth mode on you phone, or have some sort of pop-up to alert the user and programmatically turn it on for them.

like image 154
EvSunWoodard Avatar answered Mar 24 '26 19:03

EvSunWoodard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!