Android BluetoothAdapter 使用(二)

2023-12-13 05:19:35

Android BluetoothAdapter 使用(二)

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

1: 蓝牙设备列表展示

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

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布局如下:

<?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,重新计算了高度.

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

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

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);
        }
    }
};
findAdapter.setDeviceConnect(new DeviceAdapter.DeviceConnect() {
    @Override
    public void doAction(BluetoothDevice bluetoothDevice) {
        bindDevice(bluetoothDevice.getAddress());
    }
});

配对设备的代码如下:

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

点击效果如下:

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_23025319/article/details/134958985
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。