未检测到蓝牙LE设备

时间:2015-11-02 16:41:40

标签: android bluetooth bluetooth-lowenergy

我的应用需要连接蓝牙低功耗设备,显然第一步是检测它。我已经编写了下面的代码来做到这一点。当我运行应用程序时,我的Dev手机(华为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();


}

1 个答案:

答案 0 :(得分:1)

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

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

为了确保您可以访问设置 - >蓝牙并扫描以检查设备是否被默认设备蓝牙模块发现。

此外,您还可以检查Playstore上的其他一些标准应用是否检测到该设备 - https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en