zhuguifei
2025-12-31 a5a64b0bdc4e90e265cbff767c09351ea82f113a
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.shlb.comb.activity;
 
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothProfile;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import com.blakequ.bluetooth_manager_lib.connect.BluetoothConnectManager;
import com.blakequ.bluetooth_manager_lib.connect.ConnectState;
import com.blakequ.bluetooth_manager_lib.connect.ConnectStateListener;
import com.blakequ.bluetooth_manager_lib.device.BluetoothLeDevice;
import com.blakequ.bluetooth_manager_lib.util.ByteUtils;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.shlb.comb.R;
import com.shlb.comb.base.BaseActivity;
import com.shlb.comb.event.UpdateEvent;
import com.shlb.comb.util.Singletion;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
 
public class TestActivity extends BaseActivity {
 
    private TextView tvStatus;
    private TextView tvSelectedService;
    private QMUIGroupListView mGroupListView;
    private EditText etCommand;
    private Button btnSend;
    private TextView tvLog;
 
    private BluetoothConnectManager connectManager;
    private BluetoothLeDevice mDevice;
    private BluetoothGatt mGatt;
    private BluetoothGattCharacteristic selectedCharacteristic;
    
    // Store pairs of (Characteristic, UI Item) if needed, or just list
    private List<BluetoothGattCharacteristic> writeableCharacteristics = new ArrayList<>();
 
    @Override
    protected void contentView() {
        setContentView(R.layout.activity_test);
    }
 
    @Override
    protected void initView() {
        super.initHead();
        tv_center.setText("蓝牙测试");
        if (iv_left != null) {
            iv_left.setImageDrawable(getResources().getDrawable(R.mipmap.icon_back));
            iv_left.setOnClickListener(v -> finish());
        }
        
        tvStatus = findViewById(R.id.tv_status);
        tvSelectedService = findViewById(R.id.tv_selected_service);
        mGroupListView = findViewById(R.id.groupListView);
        etCommand = findViewById(R.id.et_command);
        btnSend = findViewById(R.id.btn_send);
        tvLog = findViewById(R.id.tv_log);
    }
 
    @Override
    protected void initData() {
        mDevice = Singletion.getInstance().mDevice;
        if (mDevice == null) {
            appendLog("未选择设备!");
            return;
        }
        initConn(mDevice);
    }
 
    @Override
    protected void initEvent() {
        btnSend.setOnClickListener(v -> {
            String cmd = etCommand.getText().toString().trim();
            if (TextUtils.isEmpty(cmd)) {
                Toast("请输入指令");
                return;
            }
            if (selectedCharacteristic == null) {
                Toast("请选择一个服务/特征值");
                return;
            }
            sendCmd(cmd);
        });
    }
 
    private void initConn(BluetoothLeDevice device) {
        appendLog("正在连接 " + device.getAddress() + "...");
        connectManager = BluetoothConnectManager.getInstance(this);
        connectManager.addConnectStateListener(stateListener);
        connectManager.setBluetoothGattCallback(new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                EventBus.getDefault().post(new UpdateEvent(UpdateEvent.Type.CONN_STATU, newState, "conn_statu"));
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    mGatt = gatt;
                }
            }
 
            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    runOnUiThread(() -> processServices(gatt.getServices()));
                }
            }
 
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
                String msg = (status == BluetoothGatt.GATT_SUCCESS) ? "写入成功" : "写入失败";
                String uuid = characteristic.getUuid().toString().substring(0, 8);
                runOnUiThread(() -> appendLog(msg + " (" + uuid + ")"));
            }
 
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                byte[] value = characteristic.getValue();
                String hex = bytesToHexString(value);
                String uuid = characteristic.getUuid().toString().substring(0, 8);
                runOnUiThread(() -> appendLog("收到(" + uuid + "): " + hex));
 
            }
        });
        connectManager.connect(device.getAddress());
    }
 
    private ConnectStateListener stateListener = new ConnectStateListener() {
        @Override
        public void onConnectStateChanged(String address, ConnectState state) {
            runOnUiThread(() -> {
                String stateStr = "";
                switch (state) {
                    case CONNECTED: stateStr = "已连接"; break;
                    case CONNECTING: stateStr = "连接中"; break;
                    case NORMAL: stateStr = "已断开"; break;
                    default: stateStr = state.toString();
                }
                tvStatus.setText("状态: " + stateStr);
                appendLog("状态变更: " + stateStr);
            });
        }
    };
 
    private void processServices(List<BluetoothGattService> services) {
        writeableCharacteristics.clear();
        int size = QMUIDisplayHelper.dp2px(this, 20);
        QMUIGroupListView.Section section = QMUIGroupListView.newSection(this)
                .setTitle("可写服务列表")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT);
 
        for (BluetoothGattService service : services) {
            for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                int props = characteristic.getProperties();
                // Filter for Write properties
                if ((props & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 ||
                    (props & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
                    
                    writeableCharacteristics.add(characteristic);
                    
                    String propertyStr = getPropertyString(props);
                    String uuidFull = characteristic.getUuid().toString();
                    
                    QMUICommonListItemView item = mGroupListView.createItemView(null,
                            "UUID: " + uuidFull,
                            propertyStr,
                            QMUICommonListItemView.VERTICAL,
                            QMUICommonListItemView.ACCESSORY_TYPE_NONE);
                            
                    item.setTag(characteristic);
                    
                    section.addItemView(item, v -> {
                        if (v.getTag() instanceof BluetoothGattCharacteristic) {
                            handleCharacteristicSelection((BluetoothGattCharacteristic) v.getTag());
                        }
                    });
                }
            }
        }
        
        section.addTo(mGroupListView);
        
        if (!writeableCharacteristics.isEmpty()) {
            // Default select first
            handleCharacteristicSelection(writeableCharacteristics.get(0));
        } else {
            appendLog("未找到可写特征值.");
        }
    }
 
    private void handleCharacteristicSelection(BluetoothGattCharacteristic characteristic) {
        selectedCharacteristic = characteristic;
        tvSelectedService.setText("当前选择服务: " + selectedCharacteristic.getUuid().toString());
        appendLog("已选择特征值: " + selectedCharacteristic.getUuid());
        Toast("已选择该服务");
 
        // Attempt to find and enable notification on a characteristic in the same service
        BluetoothGattService service = characteristic.getService();
        boolean foundNotify = false;
        for (BluetoothGattCharacteristic c : service.getCharacteristics()) {
            int props = c.getProperties();
            if ((props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0 ||
                    (props & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
                appendLog("尝试启用通知: " + c.getUuid());
                enableNotification(c);
                foundNotify = true;
                // Found one, stop searching to avoid concurrent descriptor writes
                break; 
            }
        }
        
        if (!foundNotify) {
             appendLog("当前服务未找到可通知的特征值");
        }
    }
    
    private String getPropertyString(int property) {
        StringBuilder sb = new StringBuilder();
        if ((property & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            sb.append("读 ");
        }
        if ((property & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0
                || (property & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
            sb.append("写 ");
        }
        if ((property & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            sb.append("通知 ");
        }
        if ((property & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
            sb.append("指示 ");
        }
        return sb.toString();
    }
 
    private void enableNotification(BluetoothGattCharacteristic characteristic) {
        if (mGatt == null || characteristic == null) return;
 
        boolean success = mGatt.setCharacteristicNotification(characteristic, true);
        if (success) {
            appendLog("启用通知监听成功");
 
            // Write Descriptor for Notify/Indicate
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
 
            if (descriptor != null) {
                int props = characteristic.getProperties();
                byte[] value = null;
                if ((props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                    value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
                } else if ((props & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
                    value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
                }
 
                if (value != null) {
                    descriptor.setValue(value);
                    boolean writeDesc = mGatt.writeDescriptor(descriptor);
                    appendLog("写入Descriptor: " + writeDesc);
                }
            }
        } else {
            appendLog("启用通知监听失败!");
        }
    }
 
    private void sendCmd(String hexCmd) {
        if (selectedCharacteristic == null || mGatt == null) return;
        
        byte[] data = hexStringToBytes(hexCmd);
        if (data == null) {
            Toast("Hex指令格式错误");
            return;
        }
 
        selectedCharacteristic.setValue(data);
        boolean status = mGatt.writeCharacteristic(selectedCharacteristic);
        appendLog("发送: " + hexCmd + ", 结果: " + status);
    }
 
    private void appendLog(String msg) {
        tvLog.append(msg + "\n");
        ((View)tvLog.getParent()).post(() -> ((View)tvLog.getParent()).scrollTo(0, tvLog.getBottom()));
    }
 
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        hexString = hexString.replace(" ", "");
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
 
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toUpperCase();
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventRefresh(UpdateEvent event) {
        // Handle global events if necessary
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connectManager != null && mDevice != null) {
            connectManager.removeConnectStateListener(stateListener);
            connectManager.disconnect(mDevice.getAddress());
        }
    }
}