zhuguifei
2025-12-30 61eee1173c00a7ba9d9c748d28fe3acdb33b9441
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
package com.blakequ.bluetooth_manager_lib.connect;
 
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
 
 
import com.blankj.utilcode.util.LogUtils;
 
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
 
/**
 * bluetooth write and read queue oprator
 * Created by PLUSUB on 2015/11/16.
 */
public class BluetoothOperatorQueue {
 
    private String uuid;
    private final Queue<SimpleEntity> sWriteQueue = new ConcurrentLinkedQueue<SimpleEntity>();
    private boolean sIsWriting = false;
    private BluetoothGatt mBluetoothGatt;
 
    public BluetoothOperatorQueue(){
    }
 
    public synchronized void clean(){
        sIsWriting = false;
        uuid = null;
        sWriteQueue.clear();
    }
 
    /**
     * start operator to write or read BluetoothGatt Service
     * @param mBluetoothGatt
     * @exception IllegalArgumentException if mBluetoothGatt is null
     */
    public void start(BluetoothGatt mBluetoothGatt){
        if (mBluetoothGatt == null){
            throw new IllegalArgumentException("BluetoothGatt is null, can not write or read BluetoothGatt Service");
        }
        this.mBluetoothGatt = mBluetoothGatt;
        nextOperator();
    }
 
    /**
     * add write or read characteristic operator by order,In order execution
     * @param gattCharacteristic
     * @param isWrite true is write operator, false is read
     */
    public synchronized void addOperator(BluetoothGattCharacteristic gattCharacteristic, boolean isWrite) {
        SimpleEntity entity = new SimpleEntity(isWrite, gattCharacteristic);
        sWriteQueue.add(entity);
    }
 
    /**
     * add write or read descriptor operator by order,In order execution
     * @param gattDescriptor
     * @param isWrite true is write operator, false is read
     */
    public synchronized void addOperator(BluetoothGattDescriptor gattDescriptor, boolean isWrite) {
        SimpleEntity entity = new SimpleEntity(isWrite, gattDescriptor);
        sWriteQueue.add(entity);
    }
 
    /**
     * next operator, should invoke by hand
     */
    public synchronized void nextOperator() {
        sIsWriting = false;
        if (!sWriteQueue.isEmpty() && !sIsWriting) {
            doOperator(sWriteQueue.poll());
        }
    }
 
    /**
     * do operator of read or write
     * @param entity
     */
    private synchronized boolean doOperator(SimpleEntity entity) {
        if (mBluetoothGatt == null){
            LogUtils.e("do operator fail, bluetoothgatt is null");
            return false;
        }
        boolean result = true;
        if (entity.obj instanceof BluetoothGattCharacteristic) {
            sIsWriting = true;
            BluetoothGattCharacteristic character = (BluetoothGattCharacteristic) entity.obj;
            uuid = character.getUuid().toString();
            if (entity.isWrite){
                result = mBluetoothGatt.writeCharacteristic(character);
            }else{
//                test(character);
                result = mBluetoothGatt.readCharacteristic(character);
            }
        } else if (entity.obj instanceof BluetoothGattDescriptor) {
            sIsWriting = true;
            BluetoothGattDescriptor desc = (BluetoothGattDescriptor) entity.obj;
            uuid = desc.getUuid().toString();
            if (entity.isWrite){
                result = mBluetoothGatt.writeDescriptor(desc);
            }else {
                result = mBluetoothGatt.readDescriptor(desc);
            }
        } else {
            LogUtils.d("do operator next");
            nextOperator();
        }
        LogUtils.d("do operator result:"+result+" "+uuid);
        return result;
    }
 
//    private void test(BluetoothGattCharacteristic characteristic){
//        boolean result = (characteristic.getProperties() &
//                BluetoothGattCharacteristic.PROPERTY_READ) == 0;
//        System.out.println("result:"+result+ " "+characteristic.getProperties()+" "+BluetoothGattCharacteristic.PROPERTY_READ);
//    }
 
    private static class SimpleEntity {
        public boolean isWrite;
        public Object obj;
        public String info;
 
        public SimpleEntity(boolean isWrite, Object obj) {
            this.isWrite = isWrite;
            this.obj = obj;
        }
 
        public SimpleEntity(boolean isWrite, Object obj, String info) {
            this.isWrite = isWrite;
            this.obj = obj;
            this.info = info;
        }
    }
}