2016-01-10 86 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.