您好我正在尝试设置多个连接,但我遇到了两个问题:第一个是,我无法接收数据,我只接收到一些字节,但它应该是所有的声音,不是吗?
我的第二个问题是,在第二次连接后,我的应用程序崩溃了。
甚至可以连接多个耳机吗?至少能收到?
public void StartDiscover()
{
var devices = DiscoverClient.DiscoverDevices(10, true, true, true, true);
foreach (var device in devices)
{
if (KnowDevice(device))
continue;
Console.WriteLine("Found Device: " + device.DeviceName + "/" + device.DeviceAddress);
var paired = BluetoothSecurity.PairRequest(device.DeviceAddress, "1234");
Console.WriteLine("Paired: " + paired);
var ipend = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.Headset);
ipend.Service = BluetoothService.Headset;
var headset = new Headset();
headset.Device = device;
headset.Client = new BluetoothClient(LocalEndpoint);
headset.Client.SetPin("1234");
headset.Client.Connect(ipend);
Console.WriteLine("Connected!");
Task.Run(() => ReceiveData(headset.Client));
Headsets.Add(headset);
}
StartDiscover();
}
public Task ReceiveData(BluetoothClient client)
{
using (var stream = client.GetStream())
{
using (var reader = new StreamReader(stream))
{
while (true)
{
Console.WriteLine(reader.Read());
}
}
}
}
编辑:现在尝试这个,但它不工作。没有任何反应,没有控制台输出。
public BlueControl()
{
Headsets = new List<BluetoothDeviceInfo>();
LocalEndpoint = new BluetoothEndPoint(BluetoothAddress.Parse("00:00:00:00:00:00"), BluetoothService.SerialPort);
try
{
DiscoverClient = new BluetoothClient(LocalEndpoint);
LocalComponent = new BluetoothComponent(DiscoverClient);
LocalComponent.DiscoverDevicesProgress += LocalComponent_DiscoverDevicesProgress;
LocalComponent.DiscoverDevicesComplete += LocalComponent_DiscoverDevicesComplete;
}
catch (PlatformNotSupportedException)
{
throw new Exception("No bluetooth device found");
}
StartDiscover();
}
public bool KnowDevice(BluetoothDeviceInfo device)
{
foreach (var headset in Headsets)
if (headset.Equals(device))
return true;
return false;
}
public void StartDiscover()
{
LocalComponent.DiscoverDevicesAsync(10, true, true, true, true, null);
}
public void LocalComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
foreach (var device in e.Devices)
{
if (KnowDevice(device))
continue;
Console.WriteLine("Found Device: " + device.DeviceName + "/" + device.DeviceAddress);
device.SetServiceState(BluetoothService.Headset, true);
Headsets.Add(device);
}
}
public void LocalComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
StartDiscover();
}