gpt4 book ai didi

android - 未检测到蓝牙 LE 设备

转载 作者:行者123 更新时间:2023-11-29 01:26:59 24 4
gpt4 key购买 nike

我的应用需要连接到低功耗蓝牙设备,显然第一步是检测它。我已经编写了下面的代码来做到这一点。当我运行该应用程序时,我的开发手机(华为 P8 Lite)旁边有另一个 BLE 设备(Xperia M2),蓝牙打开且可发现,但是,logcat 上没有打印任何内容。设备是否确实未被发现,或者回调函数是否未被调用/发生故障。我该怎么做才能确定发生的是哪种情况?

scanBluetoothMethod 在代码的其他地方被调用。

private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private static final long SCAN_PERIOD = 10000;
private BluetoothLeScanner scanner;
private Handler scanHandler = new Handler();
private List<ScanFilter> scanFilters = new ArrayList<ScanFilter>();
private ScanSettings scanSettings;
private boolean scanRunning=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_pandlet);

...

bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
}


private void scanBluetoothLE() {

if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
return;
}

progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getString(R.string.addpandlet_searchingforbledevices));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setIndeterminate(true);
progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, getString(R.string.addpandlet_cancelsearch), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scanner.stopScan(scanCallback);
scanHandler.removeCallbacks(scanRunnable);
scanRunning = false;
return;
}
});


ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
//scanSettingsBuilder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
scanSettings = scanSettingsBuilder.build();

scanHandler.post(scanRunnable);

}


private Runnable scanRunnable = new Runnable() {
@Override
public void run() {
scanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();

if (scanRunning) {
scanner.stopScan(scanCallback);
progressDialog.hide();
scanRunning = false;
} else {
scanRunning = true;
progressDialog.show();
scanHandler.postDelayed(this, SCAN_PERIOD);
scanner.startScan(scanFilters, scanSettings, scanCallback);
}
}
};


private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {

System.out.println(result.getRssi() + " " + result.getDevice().getName() );

super.onScanResult(callbackType, result);

}

@Override
public void onScanFailed(int errorCode) {
System.out.println("Error code " + errorCode );

super.onScanFailed(errorCode);

}
};



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK )
scanBluetoothLE();


}

最佳答案

从代码 - BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner(); 看来您正在尝试扫描蓝牙低功耗设备。我猜您使用的设备仅支持经典蓝牙。

尝试使用宣传蓝牙低功耗服务的设备。您可以构建一个应用,在支持蓝牙低功耗的设备上宣传蓝牙服务。

为了确定,您可以进入“设置”->“蓝牙”并扫描以检查设备是否被默认设备蓝牙模块发现。

您还可以在 playstore- https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en 上检查设备是否被其他一些标准应用程序检测到

关于android - 未检测到蓝牙 LE 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33482594/

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