AmiguelS
AmiguelS

Reputation: 855

Bluetooth LE devices not detected

My app needs to connect to a Bluetooth Low Energy device, and obviously the first step is to detect it. I have written the code bellow to do just that. When I run the app I have another BLE device (Xperia M2) beside my Dev phone (Huawei P8 Lite) with Bluetooth ON and discoverable, however, nothing get printed on the logcat. Is the device truly not being discovered or is the callback function not being called/malfunctioning. What can I do to identify which one of this situations is happening?

The scanBluetoothMethod is called elsewhere in the code.

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();


}

Upvotes: 2

Views: 3548

Answers (1)

rupesh jain
rupesh jain

Reputation: 3430

From the code - BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner(); it seems you are trying to scan bluetooth low energy devices..The device you are using I guess supports only classic bluetooth.

Try using devices which advertise bluetooth low energy services.You can build a app which advertise ble services on a device which supports bluetooth low energy.

Just to be sure, you can go to Settings->bluetooth and scan to check whether the device is being discovered by the default device bluetooth module.

Also you can check whether the device is detected by some other standard apps on playstore-https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en

Upvotes: 1

Related Questions