蓝牙连接
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package com.ckkj.griddle.device.adapter;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.ckkj.griddle.R;
|
||||
import com.ckkj.griddle.databinding.BlDeviceListItemBinding;
|
||||
import com.ckkj.griddle.device.entity.BleScanDevice;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** 按扫描地址去重,保留发现顺序并实时刷新广播名称与 RSSI。 */
|
||||
public class BltDeviceListAdapter extends RecyclerView.Adapter<BltDeviceListAdapter.Holder> {
|
||||
private final List<BleScanDevice> devices = new ArrayList<>();
|
||||
// 保存扫描地址对应的位置,同一设备重复广播时更新原条目,不追加新行。
|
||||
private final Map<String, Integer> positions = new HashMap<>();
|
||||
private OnDeviceClickListener clickListener;
|
||||
|
||||
public interface OnDeviceClickListener { void onDeviceClick(BleScanDevice device); }
|
||||
|
||||
public void setOnDeviceClickListener(OnDeviceClickListener listener) { clickListener = listener; }
|
||||
|
||||
/** 新一轮扫描开始时同时清空数据和地址索引,避免残留上一轮已离线设备。 */
|
||||
public void clear() {
|
||||
int count = devices.size();
|
||||
devices.clear();
|
||||
positions.clear();
|
||||
if (count > 0) notifyItemRangeRemoved(0, count);
|
||||
}
|
||||
|
||||
/** 新设备按发现顺序追加;已存在的设备只更新名称和信号,不改变列表顺序。 */
|
||||
public void updateDevice(BleScanDevice device) {
|
||||
Integer position = positions.get(device.getAddress());
|
||||
if (position == null) {
|
||||
positions.put(device.getAddress(), devices.size());
|
||||
devices.add(device);
|
||||
notifyItemInserted(devices.size() - 1);
|
||||
} else {
|
||||
BleScanDevice previous = devices.get(position);
|
||||
// 部分广播包没有携带名称,保留之前已识别的名称,防止显示反复变成未命名设备。
|
||||
String name = TextUtils.isEmpty(device.getName()) ? previous.getName() : device.getName();
|
||||
// 展示内容未变化时不触发列表刷新,减少持续广播造成的无效更新。
|
||||
if (TextUtils.equals(name, previous.getName()) && device.getRssi() == previous.getRssi()) {
|
||||
return;
|
||||
}
|
||||
devices.set(position, new BleScanDevice(name, device.getAddress(), device.getRssi()));
|
||||
notifyItemChanged(position);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new Holder(BlDeviceListItemBinding.inflate(
|
||||
LayoutInflater.from(parent.getContext()), parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull Holder holder, int position) {
|
||||
BleScanDevice device = devices.get(position);
|
||||
BlDeviceListItemBinding binding = holder.binding;
|
||||
binding.tvDeviceName.setText(TextUtils.isEmpty(device.getName())
|
||||
? holder.itemView.getContext().getString(R.string.bl_device_unknown_name) : device.getName());
|
||||
// 扫描阶段尚未读取业务设备信息,因此这里显示 MAC,不将其冒充设备序列号。
|
||||
binding.tvDeviceSn.setText(holder.itemView.getContext().getString(
|
||||
R.string.bl_device_address, device.getAddress()));
|
||||
int label;
|
||||
int color;
|
||||
int background;
|
||||
// RSSI 越接近 0 信号越强;这两个阈值仅用于页面分档,不代表实际距离。
|
||||
if (device.getRssi() >= -60) {
|
||||
label = R.string.bl_device_signal_strong;
|
||||
color = R.color.bl_device_signal_strong;
|
||||
background = R.drawable.bl_device_signal_strong_bg;
|
||||
} else if (device.getRssi() >= -80) {
|
||||
label = R.string.bl_device_signal_medium;
|
||||
color = R.color.device_home_orange;
|
||||
background = R.drawable.mine_soft_pill_bg;
|
||||
} else {
|
||||
label = R.string.bl_device_signal_weak;
|
||||
color = R.color.bl_device_signal_weak;
|
||||
background = R.drawable.bl_device_signal_weak_bg;
|
||||
}
|
||||
// 每次绑定均同步设置文字、颜色和背景,避免列表复用时残留上一设备的样式。
|
||||
binding.tvDeviceSignal.setText(label);
|
||||
binding.tvDeviceSignal.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), color));
|
||||
binding.tvDeviceSignal.setBackgroundResource(background);
|
||||
// 无障碍朗读额外提供原始 dBm 数值,页面仍使用信号强弱标签。
|
||||
binding.tvDeviceSignal.setContentDescription(holder.itemView.getContext().getString(
|
||||
R.string.bl_device_signal_description,
|
||||
holder.itemView.getContext().getString(label), device.getRssi()));
|
||||
// 点击时重新读取位置,避免扫描更新或列表复用后连接到旧条目。
|
||||
binding.getRoot().setOnClickListener(v -> {
|
||||
int current = holder.getBindingAdapterPosition();
|
||||
if (current != RecyclerView.NO_POSITION && clickListener != null) {
|
||||
clickListener.onDeviceClick(devices.get(current));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() { return devices.size(); }
|
||||
|
||||
static final class Holder extends RecyclerView.ViewHolder {
|
||||
final BlDeviceListItemBinding binding;
|
||||
|
||||
Holder(BlDeviceListItemBinding binding) {
|
||||
super(binding.getRoot());
|
||||
this.binding = binding;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.ckkj.griddle.device.adapter;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.ckkj.griddle.R;
|
||||
import com.ckkj.griddle.device.entity.WifiNetwork;
|
||||
import com.ckkj.griddle.device.entity.WifiNetworkSelection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 按设计稿切换普通行和浅橙色选中行;始终只允许选中一个网络。 */
|
||||
public final class WifiNetworkAdapter extends RecyclerView.Adapter<WifiNetworkAdapter.Holder> {
|
||||
public interface OnSelectionChanged { void onSelected(WifiNetwork network); }
|
||||
private final WifiNetworkSelection selection = new WifiNetworkSelection();
|
||||
private final OnSelectionChanged listener;
|
||||
private boolean selectionEnabled = true;
|
||||
|
||||
public WifiNetworkAdapter(OnSelectionChanged listener) { this.listener = listener; }
|
||||
|
||||
public void submit(List<WifiNetwork> result) {
|
||||
selection.submit(result);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void setSelectionEnabled(boolean enabled) { selectionEnabled = enabled; }
|
||||
public WifiNetwork getSelected() { return selection.getSelected(); }
|
||||
public String getSelectedKey() { return selection.getSelectedKey(); }
|
||||
public void restoreSelection(String key) { selection.restoreSelection(key); }
|
||||
@Override public int getItemCount() { return selection.size(); }
|
||||
@Override public int getItemViewType(int position) { return selection.isSelected(position) ? 1 : 0; }
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new Holder(LayoutInflater.from(parent.getContext()).inflate(viewType == 1
|
||||
? R.layout.wifi_list_selected_item : R.layout.wifi_list_item, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull Holder holder, int position) {
|
||||
WifiNetwork network = selection.get(position);
|
||||
holder.name.setText(network.getSsid());
|
||||
holder.itemView.setSelected(selection.isSelected(position));
|
||||
if (holder.lock != null) holder.lock.setVisibility(network.isSecured() ? View.VISIBLE : View.GONE);
|
||||
holder.signal.setContentDescription(holder.itemView.getContext().getString(
|
||||
R.string.wifi_setup_signal_description, network.getRssi()));
|
||||
holder.itemView.setOnClickListener(v -> {
|
||||
int current = holder.getBindingAdapterPosition();
|
||||
if (!selectionEnabled || current == RecyclerView.NO_POSITION || selection.isSelected(current)) return;
|
||||
int previous = selection.getSelectedPosition();
|
||||
selection.select(current);
|
||||
// 同时刷新旧行和新行,恢复旧行背景、文字及图标,防止复用后出现多个选中项。
|
||||
if (previous >= 0) notifyItemChanged(previous);
|
||||
notifyItemChanged(current);
|
||||
listener.onSelected(selection.getSelected());
|
||||
});
|
||||
}
|
||||
|
||||
static final class Holder extends RecyclerView.ViewHolder {
|
||||
final TextView name;
|
||||
final ImageView signal;
|
||||
final ImageView lock;
|
||||
Holder(View itemView) {
|
||||
super(itemView);
|
||||
name = itemView.findViewById(R.id.tv_wifi_name);
|
||||
signal = itemView.findViewById(R.id.iv_wifi_signal);
|
||||
lock = itemView.findViewById(R.id.iv_wifi_lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user