gpt4 book ai didi

java - 为什么 BluetoothLEScanner 不调用它的 ScanCallback

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:24 27 4
gpt4 key购买 nike

我正在尝试在一个简单的安卓应用程序中实现一个 BLE 扫描仪。我一直在关注: startLeScan replacement to current apihttps://developer.android.com/guide/topics/connectivity/bluetooth-le

不幸的是,我的 BLEScanCallback 类的回调函数似乎没有被调用。我一直在尝试使用 Log.e 来查看是否有任何一个被调用。

我调试了应用程序,应用程序进入 bluetoothLeScanner.stopScan(scanCallback);在 run() 函数中。

我已将 BLUETOOTH、BLUETOOTH_ADMIN 和 ACCESS_COARSE_LOCATION 添加到 list 中。

我不太确定我在这里做错了什么。对此的任何帮助将不胜感激。

谢谢

相关代码如下:

public class DiscoverActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;
private boolean scanning;
private Handler handler;
static int REQUEST_ENABLE_BT = 1001;
private static final long SCAN_PERIOD = 10000;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discover);
handler = new Handler();
scanning = false;

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}

public boolean onOptionsItemSelected(MenuItem item) {
boolean ret;
int id = item.getItemId();

switch (id){
case R.id.scan:
Log.e("test", "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
scanBLEDevices(true);
ret = true;
break;
default:
ret = super.onOptionsItemSelected(item);
}

return ret;
}

private void scanBLEDevices(final boolean enable){
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();

final BLEScanCallback scanCallback = new BLEScanCallback();
if (enable){
// Stops scanning after a pre-defined scan period.
handler.postDelayed(new Runnable() {
@Override
public void run() {
scanning = false;
bluetoothLeScanner.stopScan(scanCallback);
}
}, SCAN_PERIOD);

scanning = true;
bluetoothLeScanner.startScan(scanCallback);
}else{
scanning = false;
bluetoothLeScanner.stopScan(scanCallback);
}
}

public class BLEScanCallback extends ScanCallback{
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
Log.e("Scan Success", "Scan Success");
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
Log.e("Scan Success", "Scan Success Batch");
}

@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Log.e("Scan Failed", "Error Code: " + errorCode);
}
}

最佳答案

事实证明,即使我从 list 中添加了 ACCESS_COURSE_LOCATION 权限,但这对于 API 23+ 来说还是不够的。我认为在较低的 api 版本中, list 中的请求就足够了。

我必须补充:

if (Build.VERSION.SDK_INT >= 23) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect peripherals.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}

关于java - 为什么 BluetoothLEScanner 不调用它的 ScanCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50242755/

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