Beispiel #1
0
            static void ProcessReceiveResults(IAsyncResult ar)
            {
                BluetoothStreamHandler BSH = (BluetoothStreamHandler)ar.AsyncState;

                try
                {
                    int bytesToRead = 0;
                    bytesToRead = BSH.BluetoothStream.EndRead(ar);
                    if (bytesToRead == 0)
                    {
                        BSH.BluetoothStream.Close();
                        return;
                    }
                    try
                    {
                        if (BSH.buffer[0] == 0xCA)
                        {
                            string text = System.Text.Encoding.UTF8.GetString(BSH.buffer, 2, BSH.buffer.Length - 2) + " id:" + BSH.buffer[1] + " connected";
                            text = text.Replace("\a", "");
                            BSH.bcFm.Invoke(BSH.bcFm.setLabelStatus, new object[] { text, Color.Green });
                        }
                        BSH.bcFm.Invoke(BSH.bcFm.setRcvText, (BitConverter.ToString(BSH.buffer)));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                    BSH.BluetoothStream.BeginRead(BSH.buffer, 0, BSH.buffer.Length, BSH.AsyncReceiveCallback, BSH);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to read from a bluetooth stream with error: " + e.Message);
                    BSH.BluetoothStream.Close();
                }
            }
Beispiel #2
0
        private void bgWorkerListen_DoWork(object sender, DoWorkEventArgs e)
        {
            running = true;
            bl.Start();
            try
            {
                bc = bl.AcceptBluetoothClient();
                ns = bc.GetStream();
                Console.WriteLine("connected");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            BluetoothStreamHandler BSH = new BluetoothStreamHandler(bc, this);

            while (running)
            {
            }
        }
 private void bgWorkerListen_DoWork(object sender, DoWorkEventArgs e)
 {
     running = true;
     bl.Start();
     try
     {
         bc = bl.AcceptBluetoothClient();
         ns = bc.GetStream();
         Console.WriteLine("connected");
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     BluetoothStreamHandler BSH = new BluetoothStreamHandler(bc, this);
     while (running) { }
 }