我正在尝试在我的 MAUI 应用程序中使用手机上的蓝牙。我关注了这篇文章。 我知道这可能不是最好的资源,但我找不到其他资源。 我是否在某个地方犯了错误,或者这不是正确的策略?
对于上下文,我正在尝试通过 HC-05 模块连接到我的 arduino。
但是,当我尝试使用 DependencyService.Get 时,它返回 null。
这是我的界面,位于 App.xaml.cs 中:
namespace RobotControl
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
}
public interface IBluetoothConnector
{
List<string> GetConnectedDevices();
void Connect(string deviceName);
public void Write(byte[] data);
}
}
这是我的实现,位于 Platforms/Android/BluetoothConnector.cs:
using Android.Bluetooth;
using Java.Util;
using RobotControl.Platforms.Android.Bluetooth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: Dependency(typeof(RobotControl.Platforms.Android.Bluetooth.BluetoothConnector))]
namespace RobotControl.Platforms.Android.Bluetooth
{
class BluetoothConnector : IBluetoothConnector
{
BluetoothAdapter adapter;
private const string SspUuid = "00001101-0000-1000-8000-00805f9b34fb";
private BluetoothSocket? socket;
public void Connect(string deviceName)
{
var device = adapter.BondedDevices.FirstOrDefault(d=> d.Name == deviceName);
socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString(SspUuid));
socket.Connect();
}
public void Write(byte[] data)
{
}
public List<string> GetConnectedDevices()
{
adapter = BluetoothAdapter.DefaultAdapter;
if (adapter == null)
{
throw new Exception("No Bluetooth adapter");
}
if (adapter.IsEnabled)
{
if(adapter.BondedDevices.Count >0)
{
return adapter.BondedDevices.Select(x => x.Name).ToList();
}
}
else
{
Console.WriteLine("BT is not enabled");
}
return new List<string>();
}
}
}
namespace RobotControl
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
}
public interface IBluetoothConnector
{
List<string> GetConnectedDevices();
void Connect(string deviceName);
public void Write(byte[] data);
}
}
我正在尝试访问该服务。 Lhand.xaml.cs
namespace RobotControl
{
public partial class LHand : ContentPage
{
public int L_PinkyAngle;
public int L_RingAngle;
public int L_MiddleAngle;
public int L_IndexAngle;
public int L_ThumbAngle;
public int L_RotaAngle;
public LHand()
{
InitializeComponent();
}
private async void SPinky_ValueChanged(object sender, ValueChangedEventArgs e)
{
var connector = DependencyService.Get<IBluetoothConnector>();
if(connector == null)
{
await DisplayAlert("er", "connector is null", "huh");
}
else
{
await DisplayAlert("er", "isnt null", "huh");
}
string x = "";
foreach(string s in connector.GetConnectedDevices())
{
x += s + "\n";
}
await DisplayAlert("er", x+ "", "huh");
}
private void SRing_ValueChanged(object sender, ValueChangedEventArgs e)
{
}
private void SMiddle_ValueChanged(object sender, ValueChangedEventArgs e)
{
}
private void SIndex_ValueChanged(object sender, ValueChangedEventArgs e)
{
}
private void SThumb_ValueChanged(object sender, ValueChangedEventArgs e)
{
}
}
}
感谢您的帮助。
您注册了该服务吗?
// App.xaml.cs
DependencyService.Register<IBluetoothConnector, BluetoothConnector>();
另外,请参阅这个问题MAUI:DependencyService.Get