0

I need to show all the bluetooth low energy devices on a listview so that when you click in one of those devices the phone should connect to that one. First of all I´m trying to show the devices, for that I have this activity:

package com.sma.javier.sma_q;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class BTConnectActivity extends AppCompatActivity {

    private int REQUEST_ENABLE_BT = 1;
    ArrayList<String> listItems=new ArrayList<String>();
    ArrayAdapter<String> adapter;


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

        final ListView devicesListView = (ListView)this.findViewById(R.id.devicesListView);
        final ArrayAdapter<BluetoothDevice> arrayAdapter = new ArrayAdapter<>(BTConnectActivity.this, android.R.layout.simple_list_item_1);
        devicesListView.setAdapter(arrayAdapter);

        BluetoothManager bm = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter mBluetoothAdapter = bm.getAdapter();

        // Ensures Bluetooth is available on the device and it is enabled. If not,
        // displays a dialog requesting user permission to enable Bluetooth.
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Int

ent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
    scanner.startScan(new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            // get the discovered device as you wish
            // this will trigger each time a new device is found

            BluetoothDevice device = result.getDevice();
            arrayAdapter.add(device);
        }
    });
}
}

And the xml layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sma.javier.sma_q.BTConnectActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/devicesListView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

I also have the necessary permissions on the manifest, and I´m testing the app on android 6.0, but this doesn´t show anything. I have also tried with this example, but I don´t know to use it.

Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42
Javierd98
  • 708
  • 9
  • 27

1 Answers1

0

Assuming you're asking for API>21 (the earlier adapter apis startLeScan(), stopLeScan() are deprecated, please try the following:

if (bluetoothAdapter != null) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        leScanner = bluetoothAdapter.getBluetoothLeScanner();
    }
}

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        scanCallback = new ScanCallback() {

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            Log.d(TAG, "onScanFailed: errorCode: " + errorCode);
        }

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);

        Log.d(TAG, "onScanResult: Scan Result Callback Type = " + getCallbackType(callbackType));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Log.d(TAG, "onScanResult: Device = " + result.getDevice().getName() ;  --> Here, instead of the log message, you could add to an array and put them into a list on UI thread.

        }
    }

    @Override
    public void onBatchScanResults(final List<ScanResult> results) {
        super.onBatchScanResults(results);

        Log.d(TAG, "onBatchScanResults: ");

    }

If you're using API <21, you would use startLeScan() and get the scan results under onLeScan(). Pls. refer to http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html on the APIs.