startLeScan:无法获取BluetoothLeScanner

时间:2015-07-21 10:12:37

标签: android bluetooth-lowenergy

我正在尝试实现一个应用程序,当有电话的人移动时启动BLE扫描,并且如果没有移动超过10秒则自动关闭,而移动检测工作正常BLEscanner出错了 这是代码

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();
            }
        }
    }
}

错误在Log中显示为:

timeStamp/com.domain.www D/BluetoothAdapter: startLeScan: cannot get BluetoothLeScanner

3 个答案:

答案 0 :(得分:1)

在处理Cordova问题时遇到了同样的问题。在我意识到问题之前,我全神贯注地找到答案(包括这个问题)。我忘记打开手机上的蓝牙了。

因此,要么打开手机上的蓝牙模式,要么使用某种弹出窗口提醒用户并以编程方式为他们打开它。

答案 1 :(得分:0)

public void onSensorChanged(SensorEvent event) {....}

你应该等到BA启用,然后尝试再次获得适配器:

BA = manager.getAdapter()
然后开始扫描;

答案 2 :(得分:-2)

在开始扫描之前,请按照此页面进行所有验证:https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

请确保您的设备支持蓝牙低功耗。