2016-01-10 67 views
0

我需要在列表视图中显示所有蓝牙低功耗设备,以便当您单击其中一个设备时,手机应该连接到该设备。所有I'm的 首先试图展示设备,我有这个活动:Android蓝牙低功耗设备列表视图

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的例子,但我不知道用它。

+0

添加mBluetoothAdapter.enable()后BluetoothAdapter mBluetoothAdapter = BM .getAdapter(); –

+0

它仍然doesn't显示任何东西 – Javierd98

+0

尝试adapter.startLeScan(新BluetoothAdapter.LeScanCallback(){ @Override 公共无效onLeScan(BluetoothDevice类设备,INT RSSI,字节[] scanRecord){ } }) –

回答

0

假设你问API> 21(前面的适配器的API 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.