蓝牙低功耗设备avaible列表视图 [英] Bluetooth Low Energy avaible devices listview

查看:322
本文介绍了蓝牙低功耗设备avaible列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm工作为Android 5.0和上层的应用程序,我需要显示所有可用的蓝牙低功耗在一个ListView设备和,在这之后,当单击设备,当我需要它的MAC地址保存和连接到它。的问题是,它doesn't甚至显示了可用的设备。经过大量的尝试,我终于得到这个code:

I´m working on an app for android 5.0 and upper, and I need to show all the available bluetooth low energy devices on a listview and, after that, when a device is clicked I need its mac adress to be saved and to connect to it. The problem is that it doesn´t even show the available devices. After lots of tries i finally get this code:

package com.sma.javier.sma_q.BluetoothLE;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.sma.javier.sma_q.CustomArrayAdapter;
import com.sma.javier.sma_q.R;

import java.util.ArrayList;
import java.util.List;

public class Connect extends AppCompatActivity {

    private BluetoothAdapter mBluetoothAdapter;
    private int REQUEST_ENABLE_BT   =   1;
    private Handler mHandler;
    private static  final   long    SCAN_PERIOD =   10000;
    private BluetoothLeScanner mLEScanner;
    private ScanSettings settings;
    private List<ScanFilter> filters;
    private ArrayList<String> DevicesList;
    private BluetoothGatt mGatt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bt_connect);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        CollapsingToolbarLayout collapser =
                (CollapsingToolbarLayout) findViewById(R.id.collapser);

        ArrayList<String> DevicesList = new ArrayList<String>();
        ArrayAdapter<String> itemsAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DevicesList);

        View rowView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.content_bt_connect, null);
        ListView listView = (ListView) rowView.findViewById(R.id.btDevicesListView);
        listView.setAdapter(itemsAdapter);

        mHandler    =   new Handler();

        /*  Check   to  make    sure    BLE is  supported   */
        if  (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
        {
            Toast.makeText(this,    "BLE    Not Supported",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
        /*  Get a   Bluetooth   Adapter Object  */
        final   BluetoothManager    bluetoothManager    =
                (BluetoothManager)  getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter   =   bluetoothManager.getAdapter();
    }

    @Override
    protected   void    onActivityResult(int    requestCode,    int resultCode, Intent data) {
        if  (requestCode    ==  REQUEST_ENABLE_BT) {
            if  (resultCode ==  Activity.RESULT_CANCELED) {
                //Bluetooth not enabled.
                finish();
                return;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void scanLeDevice(final boolean enable) {
        if (enable) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mLEScanner.stopScan(mScanCallback);
                }
            },  SCAN_PERIOD);
            mLEScanner.startScan(filters,   settings,   mScanCallback);
        }
        else {
            mLEScanner.stopScan(mScanCallback);
        }
    }


    private ScanCallback mScanCallback   =   new ScanCallback() {
        @Override
        public void onScanResult(int callbackType,ScanResult result) {
            /*  Connect to  device  found   */
            Log.i("callbackType", String.valueOf(callbackType));
            BluetoothDevice btDevice    =   result.getDevice();
            DevicesList.add(String.valueOf(btDevice));
            connectToDevice(btDevice);
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            /*  Process a   batch   scan    results */
            for (ScanResult sr  :   results) {
                Log.i("Scan Item:   ",  sr.toString());
            }
        }
    };


    public void connectToDevice(BluetoothDevice device) {
        if  (mGatt == null){
            mGatt = device.connectGatt(this, false, mainGattCallback);
            scanLeDevice(false);//  will    stop    after   first   device  detection
        }
    }

    private final BluetoothGattCallback mainGattCallback = new BluetoothGattCallback() {
        @Override
        public  void onConnectionStateChange(BluetoothGatt   gatt,   int status, int newState) {
            switch  (newState) {
                case    BluetoothProfile.STATE_CONNECTED:
                    Log.i("mainGattCallback",   "CONNECTED");
                    gatt.discoverServices();
                    break;
                case    BluetoothProfile.STATE_DISCONNECTED:
                    Log.e("mainGattCallback",   "DISCONNECTED");
                    break;
                default:
                    Log.e("mainGattCallback",   "STATE_OTHER");
            }

        }

        @Override
        public  void onServicesDiscovered(BluetoothGatt gatt,int status) {
            List<BluetoothGattService> services = gatt.getServices();
            Log.i("onServicesDiscovered",   services.toString());
            gatt.readCharacteristic(services.get(1).getCharacteristics().get
                    (0));
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt  gatt,
                                             BluetoothGattCharacteristic
                                                     characteristic, int status)
        {
            Log.i("onCharacteristicRead",   characteristic.toString());
        }
    };

}

这其实只是应该显示的第一个发现的设备,连接到它,得到它的特点。展望和日志与logcat中,我可以看到它连接并读取的特性,但没有显示在列表视图。我的布局是:
bt_content.xml为工具栏

That actually just should show the first discovered device, connect to it and get it characteristics. Looking and the log with logcat, i can see that it connects and reads the characteristics, but nothing is displayed on the listview. My layouts are: bt_content.xml for the toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- App Bar -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <!-- Collapser -->
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/image_paralax"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="fitEnd"
                app:layout_collapseMode="parallax"
                android:src="@drawable/icon"/>

            <!-- Toolbar -->
            <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/CustomActionBar" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

和content_bt_connect.xml为ListView:

And content_bt_connect.xml for the listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.sma.javier.sma_q.BluetoothLE.Connect"
    tools:showIn="@layout/bt_connect">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btDevicesListView" />
</RelativeLayout>

不过,正如我刚才所说,这是行不通的,能有人帮助我吗?
非常感谢!

But as I have said, it does not work, can somebody help me?? Thanks a lot!!

推荐答案

有与你的方法有问题。在你的连接活动,您膨胀 content_bt_connect.xml 但你永远不将其添加到活动的布局。所以你从来没有真正看到你的ListView和它有任何项目。而不是的onCreate充气 content_bt_connect.xml 单独(),为什么不使用它在您的活动的布局。您的活动的XML应该如下所示:

There is a problem with your approach. In your Connect activity, you inflate content_bt_connect.xml but you never add it to the activity's layout. So you never actually see your listview and any items it has. Rather than inflating the content_bt_connect.xml separately in onCreate(), why not just use it in your activity's layout. Your activity xml should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- App Bar -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <!-- Collapser -->
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/image_paralax"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="fitEnd"
                app:layout_collapseMode="parallax"
                android:src="@mipmap/ic_launcher"/>

            <!-- Toolbar -->
            <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btDevicesListView" />

</android.support.design.widget.CoordinatorLayout>

的onCreate()

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    CollapsingToolbarLayout collapser = (CollapsingToolbarLayout) findViewById(R.id.collapser);

    ArrayList<String> DevicesList = new ArrayList<String>();
    ArrayAdapter<String> itemsAdapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DevicesList);

    ListView listView = (ListView) findViewById(R.id.btDevicesListView);
    listView.setAdapter(itemsAdapter);

    mHandler    =   new Handler();

    /*  Check   to  make    sure    BLE is  supported   */
    if  (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
    {
        Toast.makeText(this, "BLE    Not Supported",
                Toast.LENGTH_SHORT).show();
        finish();
    }
    /*  Get a   Bluetooth   Adapter Object  */
    final BluetoothManager bluetoothManager    =
            (BluetoothManager)  getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter   =   bluetoothManager.getAdapter();
}

这篇关于蓝牙低功耗设备avaible列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆