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_915918415 小时前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者85 小时前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview
雨白10 小时前
实现双向滑动的 ScalableImageView(上)
android
Y40900111 小时前
数据库基础知识——聚合函数、分组查询
android·数据库
没有了遇见15 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_9160088916 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214316 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO17 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO17 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
g_i_a_o_giao19 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析