gpt4 book ai didi

android - 开始LeScan : cannot get BluetoothLeScanner

转载 作者:行者123 更新时间:2023-11-29 14:14:12 27 4
gpt4 key购买 nike

我正在尝试实现一个应用程序,当拿着手机的人移动时启动 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();
}
}
}
}

错误在日志中显示为:

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

最佳答案

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

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

关于android - 开始LeScan : cannot get BluetoothLeScanner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31536191/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com