2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth;
15 import java.util.concurrent.CompletableFuture;
16 import java.util.concurrent.atomic.AtomicBoolean;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.bluetooth.BluetoothCharacteristic.GattCharacteristic;
21 import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
24 * Mock implementation of a {@link BluetoothDevice}.
26 * @author Connor Petty - Initial contribution
29 public class MockBluetoothDevice extends BaseBluetoothDevice {
31 private AtomicBoolean servicesDiscovered = new AtomicBoolean(false);
34 * This is the name that returned in the DEVICE_NAME characteristic
36 private @Nullable String deviceName = null;
38 public MockBluetoothDevice(BluetoothAdapter adapter, BluetoothAddress address) {
39 super(adapter, address);
43 public boolean connect() {
44 this.connectionState = ConnectionState.CONNECTED;
45 notifyListeners(BluetoothEventType.CONNECTION_STATE,
46 new BluetoothConnectionStatusNotification(ConnectionState.CONNECTED));
54 public boolean discoverServices() {
55 if (!servicesDiscovered.getAndSet(true)) {
57 notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
62 protected void populateServices() {
63 if (deviceName != null) {
64 BluetoothService service = new BluetoothService(BluetoothService.GattService.DEVICE_INFORMATION.getUUID());
65 service.addCharacteristic(new BluetoothCharacteristic(GattCharacteristic.DEVICE_NAME.getUUID(), 0));
71 public boolean disconnect() {
76 public boolean readCharacteristic(BluetoothCharacteristic characteristic) {
77 if (characteristic.getGattCharacteristic() == GattCharacteristic.DEVICE_NAME) {
78 characteristic.setValue(deviceName);
79 notifyListeners(BluetoothEventType.CHARACTERISTIC_READ_COMPLETE, characteristic,
80 BluetoothCompletionStatus.SUCCESS);
86 public void setDeviceName(String deviceName) {
87 this.deviceName = deviceName;
91 protected void notifyListeners(BluetoothEventType event, Object... args) {
92 CompletableFuture.runAsync(() -> super.notifyListeners(event, args));
96 public boolean writeCharacteristic(BluetoothCharacteristic characteristic) {
101 public boolean enableNotifications(BluetoothCharacteristic characteristic) {
106 public boolean disableNotifications(BluetoothCharacteristic characteristic) {
111 public boolean enableNotifications(BluetoothDescriptor descriptor) {
116 public boolean disableNotifications(BluetoothDescriptor descriptor) {