104 lines
3.4 KiB
Java
104 lines
3.4 KiB
Java
package com.yaxon.bluetooth.adapter;
|
|
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.widget.TextView;
|
|
|
|
import com.yaxon.bluetooth.R;
|
|
import com.yaxon.bluetoothsdk.constant.Constant;
|
|
import com.yaxon.bluetoothsdk.model.LinkedInfo;
|
|
import com.yaxon.common.base.adapter.CommonAdapter;
|
|
import com.yaxon.common.base.adapter.CommonViewHolder;
|
|
import com.yaxon.support.utils.YXLog;
|
|
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* ClassName: BtDeviceAdapter
|
|
* @author fanjinmin
|
|
* Date: 2026/01/15 14:04
|
|
* Description: 可用设备适配器
|
|
*/
|
|
public class BtDevScanedAdapter extends CommonAdapter<LinkedInfo, CommonViewHolder> {
|
|
|
|
private static final String TAG = BtDevScanedAdapter.class.getSimpleName();
|
|
|
|
public BtDevScanedAdapter(Context context, List<LinkedInfo> list) {
|
|
super(context, R.layout.item_btdev_scaned, list);
|
|
}
|
|
|
|
@Override
|
|
protected void onBindItemViewHolder(CommonViewHolder holder, int position) {
|
|
LinkedInfo linkedInfo = mList.get(position);
|
|
TextView tvScanDev = holder.getViewById(R.id.tv_scan_dev);
|
|
TextView tvScanState = holder.getViewById(R.id.tv_scan_state);
|
|
tvScanDev.setText(linkedInfo.getLinkedName());
|
|
if (linkedInfo.getLinkedStatus() == Constant.BluetoothConnectStatus.BLUETOOTH_PAIRING_BONDING
|
|
|| linkedInfo.getLinkedStatus() == Constant.BluetoothConnectStatus.BLUETOOTH_CONNECTING) {
|
|
tvScanState.setVisibility(View.VISIBLE);
|
|
} else {
|
|
tvScanState.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 刷新正在配对
|
|
*/
|
|
public void refreshConnectingStatus(String mac) {
|
|
if (TextUtils.isEmpty(mac)) {
|
|
return;
|
|
}
|
|
if(mList == null){
|
|
return;
|
|
}
|
|
for (LinkedInfo linkedInfoBean : mList) {
|
|
if (mac.equals(linkedInfoBean.getMacAddressStr())) {
|
|
linkedInfoBean.linkedStatus = Constant.BluetoothConnectStatus.BLUETOOTH_CONNECTING;
|
|
notifyDataSetChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新连接成功的设备
|
|
*/
|
|
public void refreshDeviceConnectData(String mac, boolean isConnected) {
|
|
if (mList == null) {
|
|
notifyDataSetChanged();
|
|
return;
|
|
}
|
|
for (LinkedInfo linkedInfoBean : mList) {
|
|
if (linkedInfoBean.getMacAddressStr().equals(mac)) {
|
|
YXLog.d(TAG,"更新连接状态 Name = "+ linkedInfoBean.getLinkedName() + " Mac = "+ linkedInfoBean.getMacAddressStr() + " isConnected = "+isConnected);
|
|
int connectStatus = isConnected ? Constant.BluetoothConnectStatus.BLUETOOTH_CONNECTED : Constant.BluetoothConnectStatus.BLUETOOTH_DISCONNECT;
|
|
linkedInfoBean.setLinkedStatus(connectStatus);
|
|
}
|
|
}
|
|
|
|
Collections.sort(mList, (o1, o2) -> o2.getLinkedStatus()-o1.getLinkedStatus());
|
|
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
/**
|
|
* 更新连接成功的设备
|
|
*/
|
|
public void removeDeviceByMacAddress(String mac) {
|
|
if (mList == null) {
|
|
return;
|
|
}
|
|
LinkedInfo temp = null;
|
|
for (LinkedInfo linkedInfoBean : mList) {
|
|
if (linkedInfoBean.getMacAddressStr().equals(mac)) {
|
|
temp = linkedInfoBean;
|
|
}
|
|
}
|
|
if(temp != null){
|
|
mList.remove(temp);
|
|
}
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
} |