Android蓝牙低能设备列表视图

Android蓝牙低能设备列表视图,android,listview,bluetooth,bluetooth-lowenergy,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.bluetoot

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

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);
        }
    });
}
}
package com.sma.javier.smaq;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothManager;
导入android.bluetooth.le.BluetoothLeScanner;
导入android.bluetooth.le.ScanCallback;
导入android.bluetooth.le.ScanResult;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入java.util.ArrayList;
公共类BTConnectActivity扩展了AppCompatActivity{
私有整数请求\启用\ BT=1;
ArrayList listItems=新的ArrayList();
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_btconnect);
最终ListView设备ListView=(ListView)this.findViewById(R.id.DeviceListView);
final ArrayAdapter ArrayAdapter=新的ArrayAdapter(BTConnectActivity.this,android.R.layout.simple\u list\u item\u 1);
setAdapter(arrayAdapter);
BluetoothManager bm=(BluetoothManager)getSystemService(Context.BLUETOOTH\u服务);
BluetoothAdapter mBluetoothAdapter=bm.getAdapter();
//确保设备上的蓝牙可用且已启用。如果未启用,
//显示一个对话框,请求用户允许启用蓝牙。
if(mBluetoothAdapter==null | |!mBluetoothAdapter.isEnabled()){
Intent enableBtIntent=新整数
ent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
BluetoothLeScanner scanner=mBluetoothAdapter.getBluetoothLeScanner();
scanner.startScan(新ScanCallback(){
@凌驾
公共void onScanResult(int callbackType、ScanResult){
//按照您的意愿获取发现的设备
//这将在每次找到新设备时触发
BluetoothDevice=result.getDevice();
arrayAdapter.add(设备);
}
});
}
}
以及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>

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

假设您要求的是API>21(早期的适配器API StartedScan()、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.
if(bluetoothAdapter!=null){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
leScanner=bluetoothAdapter.getBluetoothLeScanner();
}
}
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
scanCallback=新的scanCallback(){
@凌驾
公共无效OnScan失败(内部错误代码){
super.onscan失败(错误代码);
Log.d(标签“onScanFailed:errorCode:”+errorCode);
}
@凌驾
公共void onScanResult(int callbackType、ScanResult){
super.onScanResult(回调类型、结果);
Log.d(标记“onScanResult:Scan结果回调类型=“+getCallbackType(callbackType));
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
Log.d(标记“onScanResult:Device=“+result.getDevice().getName();”-->在这里,您可以添加到数组中,而不是日志消息,并将它们放入UI线程上的列表中。
}
}
@凌驾
public void onBatchScanResults(最终列表结果){
super.onBatchScanResults(结果);
Log.d(标记为“onBatchScanResults:”);
}

如果您使用的是API,则在BluetoothAdapter mBluetoothAdapter=bm.getAdapter()之后添加mBluetoothAdapter.enable();它仍然不会显示任何内容try adapter.starteScan(新的BluetoothAdapter.LeScanCallback(){@Override public void onLeScan(BluetoothDevice设备,int rssi,byte[]scanRecord){})但是,我应该如何使用它呢?当我把它放在Android Studio上时,StarteScan是红色的,你能发布一个屏幕截图吗?然后在上面移动鼠标以获得错误。