wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
/*  Copyright (C) 2015-2024 Alicia Hormann, Carsten Pfeiffer, Daniele Gobbetti
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle.actions;
 
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothStatusCodes;
import android.os.Build;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
 
import static nodomain.freeyourgadget.gadgetbridge.service.btle.GattDescriptor.UUID_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION;
 
import androidx.annotation.RequiresPermission;
 
/**
 * Enables or disables notifications for a given GATT characteristic.
 * The result will be made available asynchronously through the
 * {@link BluetoothGattCallback}.
 */
public class NotifyAction extends BtLEAction {
 
    private static final Logger LOG = LoggerFactory.getLogger(NotifyAction.class);
    protected final boolean enableFlag;
    private boolean hasWrittenDescriptor = false;
 
    public NotifyAction(BluetoothGattCharacteristic characteristic, boolean enable) {
        super(characteristic);
        enableFlag = enable;
    }
 
    @RequiresPermission("android.permission.BLUETOOTH_CONNECT")
    private boolean writeDescriptor(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final byte[] value) {
        if (gatt == null) {
            LOG.error("gatt == null");
            return false;
        }
 
        if (descriptor == null) {
            LOG.error("descriptor == null");
            return false;
        }
 
        final String charUuid = descriptor.getCharacteristic().getUuid().toString();
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            // use API introduced in SDK level 33 to catch exceptions and more specific errors
            try {
                final int result = gatt.writeDescriptor(descriptor, value);
 
                if (result != BluetoothStatusCodes.SUCCESS) {
                    LOG.error("Writing characteristic {} descriptor failed: {}", charUuid, result);
                    return false;
                }
            } catch (final SecurityException ex) {
                LOG.error("SecurityException while writing to characteristic {} descriptor: {}", charUuid, ex.getMessage(), ex);
                return false;
            }
        } else {
            if (!descriptor.setValue(value)) {
                LOG.error("Updating descriptor value on characteristic {} failed", charUuid);
                return false;
            }
 
            if (!gatt.writeDescriptor(descriptor)) {
                LOG.error("Writing descriptor on characteristic {} failed", charUuid);
                return false;
            }
        }
 
        LOG.debug("Successfully written characteristic {} descriptor", charUuid);
        return true;
    }
 
    @Override
    @RequiresPermission("android.permission.BLUETOOTH_CONNECT")
    public boolean run(BluetoothGatt gatt) {
        // register gatt's callback to receive notifications
        boolean result = gatt.setCharacteristicNotification(getCharacteristic(), enableFlag);
 
        if (result) {
            BluetoothGattDescriptor clientCharConfigDescriptor = getCharacteristic().getDescriptor(UUID_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION);
 
            if (clientCharConfigDescriptor != null) {
                int properties = getCharacteristic().getProperties();
 
                if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                    LOG.debug("use NOTIFICATION for Characteristic " + getCharacteristic().getUuid());
                    final byte[] value = enableFlag ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
                    result = writeDescriptor(gatt, clientCharConfigDescriptor, value);
                    hasWrittenDescriptor = true;
                } else if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
                    LOG.debug("use INDICATION for Characteristic " + getCharacteristic().getUuid());
                    final byte[] value = enableFlag ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
                    result =  writeDescriptor(gatt, clientCharConfigDescriptor, value);
                    hasWrittenDescriptor = true;
                } else {
                    LOG.debug("use neither NOTIFICATION nor INDICATION for Characteristic " + getCharacteristic().getUuid());
                    hasWrittenDescriptor = false;
                }
            } else {
                LOG.warn("Descriptor CLIENT_CHARACTERISTIC_CONFIGURATION for characteristic " + getCharacteristic().getUuid() + " is null");
                hasWrittenDescriptor = false;
            }
        } else {
            LOG.error("Unable to enable notifications for " + getCharacteristic().getUuid());
            hasWrittenDescriptor = false;
        }
 
        return result;
    }
 
    @Override
    public boolean expectsResult() {
        return hasWrittenDescriptor;
    }
}