S
Simon Liao
Guest
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!
Continue reading...
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!
Continue reading...