Android BluetoothAdapter 使用(二)

Android BluetoothAdapter 使用(二)

本篇文章主要讲下蓝牙设备的配对.

1: 蓝牙设备列表展示

下 面是蓝牙设备adapter的代码:

java 复制代码
package com.test.bluetooth;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * @Author: zh
 * @Time: 23-12-12.
 * @Email: 
 * @Describe:
 */
public class DeviceAdapter extends BaseAdapter {
    private Context context;
    private List<BluetoothDevice> list;

    public DeviceAdapter(Context context, List<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_device, parent, false);
        }
        BluetoothDevice bluetoothDevice = list.get(position);
        TextView name = convertView.findViewById(R.id.item_name);
        TextView address = convertView.findViewById(R.id.item_address);
        name.setText(bluetoothDevice.getName());
        address.setText(bluetoothDevice.getAddress());
        View viewById = convertView.findViewById(R.id.item_btn);
        viewById.setOnClickListener(v -> {
            if (deviceConnect != null)
                deviceConnect.doAction(bluetoothDevice);
        });
        return convertView;
    }

    DeviceConnect deviceConnect;

    public void setDeviceConnect(DeviceConnect deviceConnect) {
        this.deviceConnect = deviceConnect;
    }

    public interface DeviceConnect {
        void doAction(BluetoothDevice bluetoothDevice);
    }


}

item布局如下:

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:id="@+id/item_name"
        android:text="xxxxx"
        android:textSize="20sp"
        />
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/item_address"
         android:layout_marginTop="5dp"
         android:layout_marginLeft="15dp"
         android:textSize="16sp"
         />
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="16sp"
         android:text="配对"
         android:layout_marginTop="5dp"
         android:layout_marginLeft="15dp"
         android:id="@+id/item_btn"
         />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="15dp"
        android:background="#333"
        />
</LinearLayout>

另外由于嵌套使用listview. 这里简单自定义了listview,重新计算了高度.

java 复制代码
package com.test.bluetooth;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class MyListView extends ListView {

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, mExpandSpec);
    }
}

扫描蓝牙设备列表的代码,可以看下我的上篇文章,这里简单写下:

java 复制代码
private final BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Discovery has found a device. Get the BluetoothDevice
            // object and its info from the Intent.
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
            String deviceHardwareAddress = device.getAddress(); // MAC address
            if (!TextUtils.isEmpty(deviceName)){
                listFind.add(device);
                findAdapter.notifyDataSetChanged();
            }
            Log.d(TAG, "onReceive: deviceName:" + deviceName + "; deviceHardwareAddress:" + deviceHardwareAddress);
        }
    }
};
java 复制代码
findAdapter.setDeviceConnect(new DeviceAdapter.DeviceConnect() {
    @Override
    public void doAction(BluetoothDevice bluetoothDevice) {
        bindDevice(bluetoothDevice.getAddress());
    }
});

配对设备的代码如下:

java 复制代码
private void bindDevice(String address) {
    BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(address);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        remoteDevice.createBond();
    }
}

点击效果如下:

相关推荐
测试小工匠14 分钟前
移动端自动化环境搭建_Android
android·运维·自动化
ziix3 小时前
android-sdk 安装脚本
android·android-sdk安装脚本
柯南二号3 小时前
Android 实现悬浮球的功能
android·gitee·悬浮球
我想睡到自然醒₍˄·͈༝·͈˄*₎◞ ̑3 小时前
【Android】View的解析—滑动篇
android·java
chadm3 小时前
android bindService打开失败
android
诸神黄昏EX3 小时前
Android 常用命令和工具解析之内存相关
android·java·开发语言
zhangphil3 小时前
Android Glide load origin Bitmap, Kotlin
android·kotlin·glide
MuYe3 小时前
Android Hook - 隐藏API绕过实践
android
dreamsever3 小时前
Glide源码学习
android·java·学习·glide
weixin_449310843 小时前
聚水潭与MySQL数据集成案例分享
android·数据库·mysql