locked
How to make the music connection of the Bluetooth speakers faster? RRS feed

  • Question

  • I have tried three libraries to connect Bluetooth on WPF.

    using Windows.Devices.Bluetooth;

    async void ConnectBTUWP() { btdevice = await BluetoothDevice.FromIdAsync(BTId); rfcommResult = await btdevice.GetRfcommServicesAsync(); rfcomm = rfcommResult.Services[1]; if (btdevice.ConnectionStatus != BluetoothConnectionStatus.Connected) { // Create a socket and connect to the target var _socket = new StreamSocket(); await _socket.ConnectAsync( rfcomm.ConnectionHostName, rfcomm.ConnectionServiceName, SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication); } }

    using InTheHand.Net;
    using InTheHand.Net.Sockets;
    using InTheHand.Net.Bluetooth;
    using System.Net.Sockets;
    
    public void ConnectBT(string MACAddr)
    {
      var device = new BluetoothDeviceInfo(BluetoothAddress.Parse(MACAddr));
      var remoteEP = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.GenericAudio);
      var socket = new Socket((AddressFamily)32, SocketType.Stream, ProtocolType.Ggp);
      try
      {
         if (device != null)
         {
            socket.BeginConnect(remoteEP, null, null);
         }
      }
      catch (Exception x)
      {
         Console.WriteLine("!ERROR! " + x);
      }
    }

    using InTheHand.Net;
    using InTheHand.Net.Sockets;
    using InTheHand.Net.Bluetooth;
    
    public void ConnectBT(string MACAddr)
    {
      bl = new BluetoothClient();
      var device = new BluetoothDeviceInfo(BluetoothAddress.Parse(MACAddr));
      var remoteEP = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.GenericAudio);
      
      try
      {
         if (device != null)
         {
          device.SetServiceState(BluetoothService.AudioSink, true);
          device.SetServiceState(BluetoothService.AVRemoteControl, true);
          device.SetServiceState(BluetoothService.Handsfree, true);
          device.SetServiceState(BluetoothService.GenericAudio, true);
          bl.BeginConnect(device.DeviceAddress, BluetoothService.GenericAudio, new AsyncCallback(BluetoothConnectedAsyncHandler), device);
         }
      }
      catch (Exception x)
      {
         Console.WriteLine("!ERROR! " + x);
      }
    }
    These are immediately connected to the Bluetooth speaker, but take 5 seconds to connect the music when my computer plays music.

    How to connect the music faster? Thanks!

    • Moved by CoolDadTx Thursday, May 28, 2020 1:40 PM WPF related
    Thursday, May 28, 2020 3:44 AM

All replies

  • Hi Simon,

    Thank you for posting here.

    Since this thread is related to WPF, so I suggest that you can ask this question in  Microsoft Q&A.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 28, 2020 8:52 AM