未检测到蓝牙 LE 设备

Bluetooth LE devices not detected

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

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