Android蓝牙低功耗设备列表视图

时间:2016-01-10 12:13:34

标签: android listview bluetooth bluetooth-lowenergy

我需要在列表视图中显示所有蓝牙低功耗设备,这样当您点击其中一个设备时,手机应该连接到那个设备。 首先,我试图展示设备,因为我有这个活动:

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

和xml布局

<?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>

我对清单也有必要的权限,我在android 6.0上测试应用程序,但这没有显示任何内容。 我也尝试过this示例,但我不知道要使用它。

1 个答案:

答案 0 :(得分:0)

假设您要求API&gt; 21(早期的适配器apis startLeScan(),不推荐使用stopLeScan(),请尝试以下操作:

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.