Пример #1
0
        private async Task ConnectDevice(string name)
        {
            BluetoothDevice  device  = null;
            BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;

            while (_ct.IsCancellationRequested == false)
            {
                try
                {
                    Thread.Sleep(250);

                    adapter = BluetoothAdapter.DefaultAdapter;

                    if (adapter == null)
                    {
                        System.Diagnostics.Debug.WriteLine("No Bluetooth adapter found.");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Adapter found!!");
                    }

                    if (!adapter.IsEnabled)
                    {
                        System.Diagnostics.Debug.WriteLine("Bluetooth adapter is not enabled.");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Adapter enabled!");
                    }

                    System.Diagnostics.Debug.WriteLine("Try to connect to " + name);

                    foreach (var bd in adapter.BondedDevices)
                    {
                        System.Diagnostics.Debug.WriteLine("Paired devices found: " + bd.Name.ToUpper());
                        if (bd.Name.ToUpper().IndexOf(name.ToUpper()) >= 0)
                        {
                            System.Diagnostics.Debug.WriteLine("Found " + bd.Name + ". Try to connect with it!");
                            device = bd;
                            break;
                        }
                    }

                    if (device == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Named device not found.");
                    }
                    else
                    {
                        UUID uuid = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");
                        bthSocket = device.CreateInsecureRfcommSocketToServiceRecord(uuid);
                        if (bthSocket != null)
                        {
                            await bthSocket.ConnectAsync();

                            var mReader = new InputStreamReader(bthSocket.InputStream);
                            var buffer  = new BufferedReader(mReader);

                            if (bthSocket.IsConnected)
                            {
                                System.Diagnostics.Debug.WriteLine("Connected!");
                                while (_ct.IsCancellationRequested == false)
                                {
                                    if (MessageToSend != null)
                                    {
                                        var chars = MessageToSend.ToCharArray();
                                        var bytes = new List <byte>();
                                        foreach (var character in chars)
                                        {
                                            bytes.Add((byte)character);
                                        }

                                        await bthSocket.OutputStream.WriteAsync(bytes.ToArray(), 0, bytes.Count);

                                        MessageToSend = null;
                                    }

                                    while (buffer.Ready())
                                    {
                                        // I read...
                                        char[] chr = new char[100];
                                        LastReceivedData = buffer.ReadLine();
                                        onReceive?.Invoke(this, LastReceivedData);
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
                finally
                {
                    if (bthSocket != null)
                    {
                        bthSocket.Close();
                    }
                    device  = null;
                    adapter = null;
                }
            }
        }