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();
    }
}

点击效果如下:

相关推荐
2501_940094027 分钟前
PS1模拟器 DuckStation更新最新版整合 下载即玩 附PS1Bios/游戏/金手指 安卓版+电脑版
android·游戏·电脑
橙武低代码2 小时前
业务流低代码平台:从理念到实战
android·低代码·ai编程
空白格973 小时前
三方框架必学系列#Retrofit
android
安卓程序猿3 小时前
kotlin build.gradle.kts下修改APK的输出名称
android·kotlin·gradle
wuwu_q3 小时前
通俗易懂 + Android 开发实战的方式,详细讲讲 Kotlin 中的 StateFlow
android·开发语言·kotlin
峰哥的Android进阶之路3 小时前
Kotlin面试题总结
android·开发语言·kotlin
美摄科技3 小时前
android短视频sdk,灵活集成,快速上线!
android·音视频
佳哥的技术分享3 小时前
图形化android可视化开机观测工具bootchart
android
杨筱毅3 小时前
【底层机制】 Android ION内存分配器深度解析
android·底层机制
abner.Li3 小时前
基于AOSP11创建一个能用emulator启动的android tv产品
android