| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 | using InABox.Avalonia.Platform;using InABox.Core;using Microsoft.Maui.ApplicationModel;using Plugin.BLE;using Plugin.BLE.Abstractions;using Plugin.BLE.Abstractions.Contracts;namespace InABox.Avalonia.Platform.Android;public class Android_Bluetooth : IBluetooth{    public Logger? Logger { get; set; }        public async Task<bool> IsAvailable()    {        try        {            PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Bluetooth>();            if (status != PermissionStatus.Granted)                status = await Permissions.RequestAsync<Permissions.Bluetooth>();            return status == PermissionStatus.Granted;        }        catch (Exception e)        {            Console.WriteLine(e);        }        return false;    }    public async Task<bool> WriteAsync(string macaddress, Guid serviceid, Guid characteristicid, byte[] data)    {        if (await IsAvailable() != true)            return false;                IDevice? _device = null;        var adapter = CrossBluetoothLE.Current.Adapter;        adapter.DeviceDiscovered += (s, e) => _device = e.Device;        try        {            var options = new ScanFilterOptions();            options.DeviceAddresses = [macaddress];            options.ServiceUuids = [serviceid];            await adapter.StartScanningForDevicesAsync(options, x=> true, false);        }        catch (Exception e)        {            Console.WriteLine(e);            Logger?.Error(e.Message);        }                if (_device != null)        {            try            {                //await adapter.StopScanningForDevicesAsync();                //ConnectParameters connectParameters = new ConnectParameters(true, true);                await adapter.ConnectToDeviceAsync(_device);                try                {                    var service = await _device.GetServiceAsync(serviceid);                    var characteristic = await service.GetCharacteristicAsync(characteristicid);                    var bytes = await characteristic.ReadAsync();                    await characteristic.WriteAsync(data);                }                catch (Exception e)                {                    Logger?.Error(e.Message);                    return false;                }                await adapter.DisconnectDeviceAsync(_device);                return true;            }            catch (Exception e)            {                Logger?.Error(e.Message);                return false;            }                               }        return false;    }}
 |