我正在为Android 5.0及更高版本开发应用程序,我需要在列表视图中显示所有可用的蓝牙低能耗设备,之后,当点击设备时,我需要保存其mac地址并且连接到它。问题是它甚至没有显示可用的设备。经过多次尝试,我终于得到了这段代码:
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查看日志,我可以看到它连接并读取特征,但listview上没有显示任何内容。我的布局是: 工具栏的bt_content.xml
<?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>
listview的content_bt_connect.xml:
<?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>
但正如我所说,它不起作用,有人可以帮助我吗? 非常感谢!!
答案 0 :(得分:0)
您的方法存在问题。在Connect
活动中,您会夸大content_bt_connect.xml
,但您永远不会将其添加到活动的布局中。所以你永远不会真正看到你的listview及其拥有的任何物品。不要在content_bt_connect.xml
中单独夸大onCreate()
,为什么不在活动的布局中使用它。您的活动xml应如下所示:
<?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();
}