如何使用c#连接多个设备和蓝牙技术?

时间:2017-07-06 07:29:36

标签: c# bluetooth

我尝试使用蓝牙将两个以上的设备连接到一个设备,如果我使用 c#编程,最好的方法是什么?

public BluetoothDeviceInfo[] DiscoverDevices();

1 个答案:

答案 0 :(得分:2)

您可以获取可用设备的数量,然后使用List类循环遍历所有设备的BluetoothClient

int maxDevices = 10;
List<Device> devices = new List<Device>();
BluetoothClient bc = new BluetoothClient();
BluetoothDeviceInfo[] array = bc.DiscoverDevices(maxDevices);
int count = array.Length;
for (int i = 0; i < count; i++)
{
    Device device = new Device(array[i]);
    devices.Add(device);

    // the variable device will now hold a detected BT device.
    // Now you can connect to the device:
    bc.Connect(new BluetoothEndPoint((BluetoothAddress)adres,service));

    // Send a message to the device
    System.Net.Sockets.NetworkStream stream = bc.GetStream();
    StreamWriter streamWriter = new StreamWriter(stream);
    streamWriter.WriteLine("! 0 200 200 210 1");
}
// the variable devices will now hold an array of all detected BT devices.

注意: DiscoverDevices大约需要20-30秒才能完成。