Example #1
0
 private bool ConnectBtDevice(BluetoothDeviceInfo device, string pin)
 {
     try
     {
         BluetoothSecurity.SetPin(device.DeviceAddress, pin);
         BluetoothEndPoint ep = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.SerialPort);
         _btClient = new BluetoothClient();
         _btClient.SetPin(pin);
         try
         {
             _btClient.Connect(ep);
         }
         catch (Exception)
         {
             BluetoothSecurity.RemoveDevice(device.DeviceAddress);
             Thread.Sleep(1000);
             _btClient.Connect(ep);
         }
         _dataStream = _btClient.GetStream();
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
Example #2
0
        public bool SetAddress(BluetoothAddress address)
        {
            try
            {
                //Set BT Device Address
                blt_address = address;

                //Set BT Device Pin
                BluetoothSecurity.SetPin((BluetoothAddress)blt_address, "1234");

                // Create a connection channel specifying the Bluetooth-Serial end-points 
                blt_endPoint = new BluetoothEndPoint((BluetoothAddress)blt_address, BluetoothService.SerialPort);

                
                return true;
            }
            catch 
            {
                return false;
            }
        }
Example #3
0
        private void button3_Click(object sender, EventArgs e)
        {
            using (BluetoothClient bc = new BluetoothClient())
            {
                Console.WriteLine("enter you pin \n ");
                string pinString = textBox2.Text;
                if (pinString.Length != 6) // did not succe
                {
                    tries++;

                    if (tries < 3)
                    {
                        textBox2.Clear();
                    }
                    else
                    {
                        Application.Exit();
                    }
                    return;
                }
                Console.WriteLine("you entered pin\n " + pinString);
                bool succed = false;
                try
                {
                    Console.WriteLine("lets set pin and see if succeded\n ");
                    succed = BluetoothSecurity.SetPin(myArray[phoneChoosed].DeviceAddress, pinString);
                }
                catch
                {
                    Console.WriteLine("xxx\n");
                }

                if (succed)
                {
                    myArray[phoneChoosed].Refresh();
                    string deviceNameOfPhoneChoosed = myArray[phoneChoosed].DeviceName;

                    textBox2.Visible = false;
                    label2.Visible   = false;
                    button3.Visible  = false;
                    textBox1.Visible = false;
                    label1.Visible   = false;
                    button2.Visible  = false;
                    Devices.Visible  = false;
                    listBox1.Visible = false;
                    button1.Visible  = false;

                    while (true)
                    {
                        bool inRange = false;
                        myArray = bc.DiscoverDevices();
                        for (int i = 0; i < myArray.Length; i++)
                        {
                            if (string.Compare(myArray[i].DeviceName, deviceNameOfPhoneChoosed) == 0)
                            {
                                inRange = true;
                            }
                        }

                        if (inRange)
                        {
                            Console.WriteLine("In range");
                        }
                        else
                        {
                            Console.WriteLine("Out of range");
                            LockScreen.LockWorkStation();
                        }
                    }
                }
            }
        }
Example #4
0
        private void SendMessage(string pAddress, string pMessage)
        {
            try
            {
                BluetoothAddress objDeviceAddress = BluetoothAddress.Parse(pAddress);

                BluetoothDeviceInfo objDevice = new BluetoothDeviceInfo(objDeviceAddress);

                if (objDevice.Remembered && !objDevice.Connected && objDevice.Authenticated)
                {
                    BluetoothSecurity.PairRequest(objDevice.DeviceAddress, "0000");
                }
                else
                {
                    BluetoothSecurity.SetPin(objDevice.DeviceAddress, "0000");
                }

                Guid serviceClass    = BluetoothService.SerialPort;
                BluetoothEndPoint ep = new BluetoothEndPoint(objDeviceAddress, serviceClass);

                BluetoothClient cli = new BluetoothClient();
                cli.Connect(ep);
                Stream peerStream = cli.GetStream();

                byte[] bMessage = System.Text.Encoding.ASCII.GetBytes(pMessage);
                peerStream.Write(bMessage, 0, bMessage.Length);

                peerStream.Close();
                cli.Close();
            }
            catch (SocketException ex)
            {
                string reason;
                switch (ex.ErrorCode)
                {
                case 10048:     // SocketError.AddressAlreadyInUse
                    // RFCOMM only allow _one_ connection to a remote service from each device.
                    reason = "There is an existing connection to the remote Chat2 Service";
                    break;

                case 10049:     // SocketError.AddressNotAvailable
                    reason = "Chat2 Service not running on remote device";
                    break;

                case 10064:     // SocketError.HostDown
                    reason = "Chat2 Service not using RFCOMM (huh!!!)";
                    break;

                case 10013:     // SocketError.AccessDenied:
                    reason = "Authentication required";
                    break;

                case 10060:     // SocketError.TimedOut:
                    reason = "Timed-out";
                    break;

                default:
                    reason = null;
                    break;
                }

                lblError.Text = reason;
            }
            catch (Exception ex)
            {
                lblError.Text = "Error unexpected! Ex[" + ex.Message + "]";
            }
        }