hc
2023-08-30 862c27fc9920c83318c784bfdadf43a65df1ec8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.rockchip.smart.rockhome.blue;
 
import android.annotation.TargetApi;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.rockchip.smart.rockhome.R;
 
import java.util.ArrayList;
 
/**
 * Created by GJK on 2018/11/9.
 */
 
public class BluetoothDeviceAdapter extends BaseAdapter {
 
    private Context mContext;
    private ArrayList<Device> mDevices;
    private BluetoothDeviceAdapterCallback mCallback;
 
    public BluetoothDeviceAdapter(Context context) {
        this(context, null);
    }
 
    public BluetoothDeviceAdapter(Context context, BluetoothDeviceAdapterCallback callback) {
        mContext = context;
        mDevices = new ArrayList<Device>();
        mCallback = callback;
    }
 
    public ArrayList<Device> getDevices() {
        return mDevices;
    }
 
    public void setDevices(ArrayList<Device> devices) {
        this.mDevices = devices;
    }
 
    public BluetoothDeviceAdapterCallback getCallback() {
        return mCallback;
    }
 
    public void setCallback(BluetoothDeviceAdapterCallback callback) {
        this.mCallback = callback;
    }
 
    @Override
    public int getCount() {
        return mDevices.size();
    }
 
    @Override
    public Object getItem(int position) {
        return mDevices.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        for (Device blue : mDevices) {
            if (blue.getDevice().getAddress().equals(device.getAddress())) {
                blue.setDevice(device);
                blue.setRssi(rssi);
 
                return;
            }
        }
 
        if (device != null && device.getName() != null && (device.getName().startsWith("RockChip") ||
                device.getName().startsWith("Rockchip") || device.getName().startsWith("ROCKCHIP"))) {
            Device blueDevice = new Device(device, rssi);
            mDevices.add(blueDevice);
            if (mCallback != null)
                mCallback.onLeScan(blueDevice, mDevices.size() - 1);
 
            notifyDataSetChanged();
        }
    }
 
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_blue, null);
            viewHolder = new ViewHolder();
            viewHolder.rootView = (LinearLayout) convertView.findViewById(R.id.item_blue_root);
            viewHolder.deviceName = (TextView) convertView.findViewById(R.id.tv_blue);
            viewHolder.deviceAddr = (TextView) convertView.findViewById(R.id.tv_blue_addr);
 
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
 
        Device device = mDevices.get(position);
        viewHolder.deviceName.setText(device.getDevice().getName());
        viewHolder.deviceAddr.setText(device.getDevice().getAddress());
 
        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback != null) {
                    mCallback.onBluetoothDeviceClick(mDevices.get(position), position);
                }
            }
        });
 
        return convertView;
    }
 
    static class ViewHolder {
        LinearLayout rootView;
        TextView deviceName;
        TextView deviceAddr;
    }
 
    public interface BluetoothDeviceAdapterCallback {
        void onLeScanStart();
        void onLeScan(Device device, int position);
        void onLeScanStop(int size);
 
        void onBluetoothDeviceClick(Device device, int position);
    }
 
    private abstract class OnConvertViewClickListener implements View.OnClickListener{
        private View convertView;
        private int[] positionIds;
        public OnConvertViewClickListener(View convertView, int... positionIds) {
            this.convertView = convertView;
            this.positionIds = positionIds;
        }
 
        @TargetApi(Build.VERSION_CODES.DONUT)
        @Override
        public void onClick(View v) {
            int len = positionIds.length;
            int[] positions = new int[len];
            for(int i = 0; i < len; i++){
                positions[i] = (int) convertView.getTag(positionIds[i]);
            }
            onClickCallBack(v, positions);
        }
 
        public abstract void onClickCallBack(View registedView, int... positionIds);
    };
}