//-----------------------------------------------------------------------
//
// Copyright (c) 2018-22 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License - see License.txt
//
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace InTheHand.Bluetooth
{
///
/// Entry point to all Bluetooth LE functionality.
///
///
/// Several platforms require permissions for this functionality to work:-
/// iOS 13 (and above) - Set NSBluetoothAlwaysUsageDescription in your Info.plist to a descriptive message to be displayed to the user
/// iOS (older versions) - Set NSBluetoothPeripheralUsageDescription in your Info.plist to a descriptive message to be displayed to the user
/// Android - Add <uses-permission android:name="android.permission.BLUETOOTH" /> to your android manifest. To enable scanning also add <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> and <uses-permission android:name="android.permission.COARSE_LOCATION" />
/// Windows - Add the Bluetooth permission
///
public static partial class Bluetooth
{
///
/// Returns true if Bluetooth is physically available and user has given the app permission.
///
///
public static Task GetAvailabilityAsync()
{
return PlatformGetAvailability();
}
///
/// Performs a device lookup and prompts the user for permission if required.
///
///
/// A BluetoothDevice or null if unsuccessful.
public static Task RequestDeviceAsync(RequestDeviceOptions options = null)
{
ThrowOnInvalidOptions(options);
return PlatformRequestDevice(options);
}
private static void ThrowOnInvalidOptions(RequestDeviceOptions options)
{
if (options != null)
{
if ((options.Filters != null && options.Filters.Count > 0) == options.AcceptAllDevices)
throw new ArgumentException("Cannot set both Filters and AcceptAllDevices");
}
}
private static event EventHandler availabilityChanged;
///
/// Bluetooth availability has changed, for example by disabling the radio or revoking user permission.
///
public static event EventHandler AvailabilityChanged
{
add
{
if (availabilityChanged == null)
{
AddAvailabilityChanged();
}
availabilityChanged += value;
}
remove
{
availabilityChanged -= value;
if (availabilityChanged == null)
{
RemoveAvailabilityChanged();
}
}
}
private static void OnAvailabilityChanged()
{
availabilityChanged?.Invoke(null, EventArgs.Empty); ;
}
public static Task> GetPairedDevicesAsync()
{
return PlatformGetPairedDevices();
}
public static Task> ScanForDevicesAsync(RequestDeviceOptions options = null)
{
ThrowOnInvalidOptions(options);
return PlatformScanForDevices(options);
}
public static Task RequestLEScanAsync(BluetoothLEScanOptions options = null)
{
return PlatformRequestLEScan(options);
}
public static event EventHandler AdvertisementReceived;
internal static void OnAdvertisementReceived(BluetoothAdvertisingEvent advertisingEvent)
{
AdvertisementReceived?.Invoke(null, advertisingEvent);
}
}
}