I am testing connecting to my phone, via Bluetooth. I already scan, and found my phne
namespace SnapLabel.Platforms.Windows {
public class WindowsBluetoothScanner : IBluetoothService {
public event Action<BluetoothDeviceModel>? DeviceFound;
private BluetoothDevice? _connectedDevice;
private StreamSocket? _socket;
private DeviceWatcher? _watcher;
private bool _keepScanning;
private readonly HashSet<string> _seenDevices = [];
public void StartScan() {
_keepScanning = true;
_seenDevices.Clear();
StartWatcher();
}
public void StopScan() {
_keepScanning = false;
if(_watcher != null &&
(_watcher.Status == DeviceWatcherStatus.Started ||
_watcher.Status == DeviceWatcherStatus.EnumerationCompleted)) {
_watcher.Stop();
_watcher = null;
}
}
private void StartWatcher() {
string selector = "System.Devices.Aep.ProtocolId:=\"{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\"";
_watcher = DeviceInformation.CreateWatcher(
selector,
[
"System.Devices.Aep.DeviceAddress",
"System.ItemNameDisplay"
],
DeviceInformationKind.AssociationEndpoint
);
_watcher.Added += async (s, deviceInfo) => {
string name = !string.IsNullOrWhiteSpace(deviceInfo.Name)
? deviceInfo.Name
: deviceInfo.Properties.TryGetValue("System.Devices.Aep.DeviceAddress", out var addr)
? addr?.ToString() ?? "Unknown"
: "Unknown";
if(name.EndsWith("(Bluetooth)", StringComparison.OrdinalIgnoreCase))
name = name.Replace("(Bluetooth)", "").Trim();
if(!_seenDevices.Add(name))
return;
string fontIcon = FontsConstants.Bluetooth;
try {
var btDevice = await BluetoothDevice.FromIdAsync(deviceInfo.Id);
if(btDevice != null) {
uint major = (uint)btDevice.ClassOfDevice.MajorClass;
fontIcon = major switch {
1 => FontsConstants.Computer,
2 => FontsConstants.Smartphone,
4 => FontsConstants.Headphones,
5 => FontsConstants.Mouse,
6 => FontsConstants.Print,
7 => FontsConstants.Keyboard,
_ => FontsConstants.Bluetooth_audio
};
}
} catch(Exception ex) {
Debug.WriteLine($"[WARN] Could not get ClassOfDevice for {name}: {ex.Message}");
}
DeviceFound?.Invoke(new BluetoothDeviceModel {
Name = name,
FontIcon = fontIcon,
Address = Convert.ToUInt64(
deviceInfo.Properties["System.Devices.Aep.DeviceAddress"].ToString()?.Replace(":", ""),
16
)
});
};
_watcher.EnumerationCompleted += (s, e) => { };
_watcher.Updated += (s, update) => { };
_watcher.Removed += (s, update) => { };
_watcher.Start();
}
public async Task<bool> ConnectAsync(ulong deviceId) {
try {
_connectedDevice = await BluetoothDevice.FromBluetoothAddressAsync(deviceId);
if(_connectedDevice == null) {
return false;
}
var services = await _connectedDevice.GetRfcommServicesAsync();
var service = (services.Services.Count > 0)
? services.Services[0]
: null;
if(service == null) { // here is the problem
return false;
}
_socket = new StreamSocket();
await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
return true;
} catch(Exception ex) {
Debug.WriteLine($"[ERROR] Could not connect to device {deviceId}: {ex.Message}");
throw;
}
}
public Task<bool> SendDataAsync(byte[] data) {
throw new NotImplementedException();
}
}
}
my service is null when connecting