package com.google.heartrate.wearos.app.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.Context;
import android.content.pm.PackageManager;

import com.google.heartrate.wearos.app.gatt.GattException;

import static android.content.Context.BLUETOOTH_SERVICE;

/**
 * Provides bluetooth related objects and checks.
 */
public class BluetoothUtils {
    private static final String TAG = BluetoothUtils.class.getSimpleName();

    /**
     * Assert that bluetooth is supported in current device.
     *
     * @param context application context
     * @throws GattException if bluetooth is not supported
     */
    public static void assertBluetoothIsSupported(Context context) throws GattException {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);

        if (bluetoothManager == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothManager found.");
        }

        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

        if (bluetoothAdapter == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothAdapter found.");
        }

        if (!bluetoothAdapter.isEnabled()) {
            throw new GattException("Bluetooth adapter is currently disabled.");
        }

        if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            throw new GattException("Bluetooth LE is not supported. No FEATURE_BLUETOOTH_LE found.");
        }
    }

    /**
     * Get {@link BluetoothManager}.
     *
     * @param context application context
     * @return bluetooth manager
     * @throws GattException if bluetooth is not supported
     */
    public static BluetoothManager getBluetoothManager(Context context) throws GattException {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

        if (bluetoothManager == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothManager found.");
        }

        return bluetoothManager;
    }

    /**
     * Get {@link BluetoothAdapter}.
     *
     * @param context application context
     * @return bluetooth adapter
     * @throws GattException if bluetooth is not supported
     */
    public static BluetoothAdapter getBluetoothAdapter(Context context) throws GattException {
        BluetoothManager bluetoothManager = getBluetoothManager(context);
        BluetoothAdapter adapter = bluetoothManager.getAdapter();

        if (adapter == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothAdapter found.");
        }

        return adapter;
    }

    /**
     * Get {@link BluetoothLeAdvertiser}.
     *
     * @param context application context
     * @return bluetooth advertiser
     * @throws GattException if bluetooth advertising is not supported
     */
    public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(Context context) throws GattException {
        BluetoothAdapter adapter = getBluetoothAdapter(context);
        BluetoothLeAdvertiser advertiser = adapter.getBluetoothLeAdvertiser();

        if (advertiser == null) {
            throw new GattException("Bluetooth advertising is not supported. Cannot get BluetoothLeAdvertiser.");
        }

        return advertiser;
    }

    /**
     * Get {@link BluetoothGattServer}.
     *
     * @param context application context
     * @param callback server callback
     * @return bluetooth gatt server
     * @throws GattException if unable to create gatt server
     */
    public static BluetoothGattServer getBluetoothGattServer(Context context, BluetoothGattServerCallback callback) throws GattException {
        BluetoothManager bluetoothManager = getBluetoothManager(context);
        BluetoothGattServer server = bluetoothManager.openGattServer(context, callback);

        if (server == null) {
            throw new GattException("Unable to create GATT server.");
        }

        return server;
    }
}

@@ -1855,25 +1855,32 @@
 @return 
-b
+%7B@link B
 luetooth
  manager
@@ -1875,16 +1875,16 @@
 ooth
- m
+M
 anager
+%7D
 %0A   
@@ -2454,25 +2454,32 @@
 @return 
-b
+%7B@link B
 luetooth
  adapter
@@ -2470,24 +2470,24 @@
 luetooth
- a
+A
 dapter
+%7D
 %0A     * 
@@ -3064,25 +3064,32 @@
 @return 
-b
+%7B@link B
 luetooth
  adverti
@@ -3076,26 +3076,27 @@
 nk Bluetooth
- a
+LeA
 dvertiser%0A  
@@ -3092,16 +3092,17 @@
 vertiser
+%7D
 %0A     * 
@@ -3740,60 +3740,190 @@
 ack 
-s
+%7B@link BluetoothGattS
 erver
- c
+C
 allback
-%0A     * @return bluetooth gatt s
+%7D to configure %7B@link BluetoothGattServer%7D with%0A     * @return %7B@link BluetoothGattServer%7D configured with given %7B@link BluetoothGattS
 erver
+Callback%7D
 %0A   
@@ -3972,19 +3972,35 @@
 ate 
-gatt s
+%7B@link BluetoothGattS
 erver
+%7D
 %0A   

Fix comment. Add links to used classed