0

I can't find the solution for errors in the BluetoothService class in .NET MAUI. I get 2 errors:

bluetoothService does not contain a definition for SerialPort
bluetoothDeviceInfo does not contain a definition for Device

I googled it and saw suggestions that newer versions handle this differently. I downgraded to several older versions, but the errors remain. Two using directives that are supposed to be there are greyed out, which seems wrong.

Here is my code:

using Android.Bluetooth;
using InTheHand.Net.Sockets;
using System.Collections.ObjectModel;
using InTheHand.Net.Bluetooth;
using InTheHand.Net;

namespace ESP32_water_level_sensor.Platforms.Android
{
    public class BluetoothService : IBluetoothService
    {
        private BluetoothClient _client;
        private BluetoothSocket _socket;
        private Stream _stream;


        public async Task<ObservableCollection<BluetoothDeviceInfo>> GetPairedDevicesAsync()
        {
            _client = new BluetoothClient();
            var devices = _client.PairedDevices;
            return new ObservableCollection<BluetoothDeviceInfo>(devices);
        }

        public async Task<bool> ConnectToDeviceAsync(BluetoothDeviceInfo device)
        {
            try
            {
                // Connect to the device using the serial port UUID
                var serviceUuid = BluetoothService.SerialPort;
                _socket = device.Device.CreateRfcommSocketToServiceRecord(serviceUuid.Guid);
                await _socket.ConnectAsync();
                _stream = _socket.InputStream;
                return _socket.IsConnected;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Connection failed: {ex.Message}");
                return false;
            }
        }

        public async Task<string> ReadDataAsync()
        {
            if (_stream == null)
                return null;

            var reader = new StreamReader(_stream);
            // This will block until a line is received
            var line = await reader.ReadLineAsync();
            return line;
        }

        public void Disconnect()
        {
            _stream?.Dispose();
            _socket?.Dispose();
            _client?.Dispose();
        }
    }
}
1
  • 1
    BluetoothService.SerialPort isn't valid. BluetoothService is your service class implementation, so calling that expects a static method on your class when the "SerialPort" in question would be retrieved from the selected device you are connecting to. I didn't see any extension method or such to get a .Device from a BluetoothDeviceInfo. I recommend using ONLY documentation and examples from that specific llibary and get the basic example working and expand from that rather than trying to piece together something from multiple sources. The code you have doesn't look remotely valid. Commented Nov 10 at 1:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.