2 * Copyright (c) 2010-2022 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.nio.charset.StandardCharsets;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.atomic.AtomicBoolean;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.bluetooth.BluetoothCharacteristic.GattCharacteristic;
22 import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
25 * Mock implementation of a {@link BluetoothDevice}.
27 * @author Connor Petty - Initial contribution
30 public class MockBluetoothDevice extends BaseBluetoothDevice {
32 private AtomicBoolean servicesDiscovered = new AtomicBoolean(false);
35 * This is the name that returned in the DEVICE_NAME characteristic
37 private @Nullable String deviceName = null;
39 public MockBluetoothDevice(BluetoothAdapter adapter, BluetoothAddress address) {
40 super(adapter, address);
44 public boolean connect() {
45 this.connectionState = ConnectionState.CONNECTED;
46 notifyListeners(BluetoothEventType.CONNECTION_STATE,
47 new BluetoothConnectionStatusNotification(ConnectionState.CONNECTED));
55 public boolean discoverServices() {
56 if (!servicesDiscovered.getAndSet(true)) {
58 notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
63 protected void populateServices() {
64 if (deviceName != null) {
65 BluetoothService service = new BluetoothService(BluetoothService.GattService.DEVICE_INFORMATION.getUUID());
66 service.addCharacteristic(new BluetoothCharacteristic(GattCharacteristic.DEVICE_NAME.getUUID(), 0));
72 public boolean disconnect() {
77 public CompletableFuture<byte[]> readCharacteristic(BluetoothCharacteristic characteristic) {
78 if (characteristic.getGattCharacteristic() == GattCharacteristic.DEVICE_NAME) {
79 String name = deviceName;
81 return CompletableFuture.completedFuture(name.getBytes(StandardCharsets.UTF_8));
83 return CompletableFuture.completedFuture(new byte[0]);
86 return CompletableFuture.failedFuture(new UnsupportedOperationException());
89 public void setDeviceName(String deviceName) {
90 this.deviceName = deviceName;
94 protected void notifyListeners(BluetoothEventType event, Object... args) {
95 CompletableFuture.runAsync(() -> super.notifyListeners(event, args));
99 public CompletableFuture<@Nullable Void> writeCharacteristic(BluetoothCharacteristic characteristic, byte[] value) {
100 return CompletableFuture.failedFuture(new UnsupportedOperationException());
104 public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
105 return CompletableFuture.failedFuture(new UnsupportedOperationException());
109 public boolean isNotifying(BluetoothCharacteristic characteristic) {
114 public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
115 return CompletableFuture.failedFuture(new UnsupportedOperationException());
119 public boolean enableNotifications(BluetoothDescriptor descriptor) {
124 public boolean disableNotifications(BluetoothDescriptor descriptor) {