void bg_DoWork(object sender, DoWorkEventArgs e) { /* * Loop over the list of all detected bluetooth devices and display them for selecion * by the user . */ ObservableCollection<Device> devices = new ObservableCollection<Device>(); // Check if a Bluetooth radio is available on the system that is compatible with the 32Feet library. // If not then exit. log.dispatchLogMessage("Bluetooth_Devices: Attempting to find a Bluetooth radio "); bool d = BluetoothRadio.IsSupported; if (!d) { var msg = "No compatible Bluetooth radio found on system . Aboring application"; log.dispatchLogMessage(msg); System.Windows.Forms.MessageBox.Show(msg); Environment.Exit(1); } log.dispatchLogMessage("Success! Compatibe Bluetooth radio found on system "); BluetoothRadio br = BluetoothRadio.PrimaryRadio; var messg = "Details of the Bluetooth radio : "; log.dispatchLogMessage(messg); // Manfucaturer messg = "Manufacturer : " + br.Manufacturer.ToString(); log.dispatchLogMessage(messg); // System Name messg = "Name : " + br.Name.ToString(); log.dispatchLogMessage(messg); //Software Manufacturer messg = "Software Manufacturer :" + br.SoftwareManufacturer.ToString(); log.dispatchLogMessage(messg); log.dispatchLogMessage("Bluetooth Radio initiated"); log.dispatchLogMessage("***"); // This must be put in a try block InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient(); InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices(); log.dispatchLogMessage("Bluetooth_Devices: Bluetooth Devices found in vicinity"); int count = array.Length; Device device; for (int i = 0; i < count; i++) { device = new Device(array[i]); devices.Add(device); //UnsecuredDevices.Add(device); } _unsecuredDevices = devices; e.Result = _unsecuredDevices; }
void doBluetoothWork() { int attempts = 0; int maxTries = 1; while (attempts < maxTries) { attempts++; List<Device> devices = new List<Device>(); // Check if a Bluetooth radio is available on the system that is compatible with the 32Feet library. // If not then exit. log.dispatchLogMessage("Bluetooth Devices: Attempting to find a Bluetooth radio "); bool d = BluetoothRadio.IsSupported; if (!d) { var msg = "No compatible Bluetooth radio found on system . Aboring application"; log.dispatchLogMessage(msg); System.Windows.Forms.MessageBox.Show(msg); Environment.Exit(1); } log.dispatchLogMessage("Success! Compatibe Bluetooth radio found on system "); BluetoothRadio br = BluetoothRadio.PrimaryRadio; var messg = "Details of the Bluetooth radio : "; log.dispatchLogMessage(messg); // Manfucaturer messg = "Manufacturer : " + br.Manufacturer.ToString(); log.dispatchLogMessage(messg); // System Name messg = "Name : " + br.Name.ToString(); log.dispatchLogMessage(messg); //Software Manufacturer messg = "Software Manufacturer :" + br.SoftwareManufacturer.ToString(); log.dispatchLogMessage(messg); log.dispatchLogMessage("Bluetooth Radio initiated"); log.dispatchLogMessage("***"); // This must be put in a try block InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient(); InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices(); log.dispatchLogMessage("Bluetooth Devices found in vicinity"); int count = array.Length; BluetoothDeviceInfo dev; for (int i = 0; i < count; i++) { Device device = new Device(array[i]); if (device.DeviceName == "SAMSUNG GT-I8350") { log.dispatchLogMessage("Found a PROTAG device to connect to "); dev = array[i]; var addr = device.DeviceAddress; if (addr == null) { return; } Guid serviceClass = BluetoothService.SerialPort; // Make a connection to the bluetooth device and continously check for pairing // Lets make 3 consecutive checks to ensure that the connection still exists . // Make a connection to the specified Bluetooth device try { connectBluetoothDevice(bc, addr, serviceClass); try { int sigStrgt = dev.Rssi; var mssg = "Signal strength of the connection is" + sigStrgt; log.dispatchLogMessage(mssg); } catch (Exception e) { /* Rssi query may fail on certain platforms . Check http://inthehand.com/library/html/P_InTheHand_Net_Sockets_BluetoothDeviceInfo_Rssi.htm for platfrom details . Handle failure gracefully. */ var mssg = e.Message; mssg += "Signal strength cannot be determined"; log.dispatchLogMessage(mssg); } checkconnection(dev, serviceClass, bc); } catch (Exception ex) { // handle exception var msg = "Bluetooth connection failed: " + ex.Message; log.dispatchLogMessage(msg); log.dispatchLogMessage("Re-initiating connection"); msg = "Attempt " + attempts; log.dispatchLogMessage(msg); Thread.Sleep(10); } } devices.Add(device); } if (attempts >= maxTries) { } //e.Result = devices; } var mesg = "Bluetooth pairing is broken . Please check ! "; log.dispatchLogMessage(mesg); LockWorkStation(); }
void doBluetoothWork(String deviceName) { // Check if a Bluetooth radio is available on the system that is compatible with the 32Feet library. // If not then exit. log.dispatchLogMessage("Mainservices : Attempting to find a Bluetooth radio "); bool d = BluetoothRadio.IsSupported; if (!d) { var msg = "No compatible Bluetooth radio found on system . Aboring application"; log.dispatchLogMessage(msg); System.Windows.Forms.MessageBox.Show(msg); Environment.Exit(1); } log.dispatchLogMessage("Mainservices : Success! Compatibe Bluetooth radio found on system "); BluetoothRadio br = BluetoothRadio.PrimaryRadio; var messg = "Mainservices : Details of the Bluetooth radio : "; log.dispatchLogMessage(messg); // Manfucaturer messg = "Mainservices : Manufacturer : " + br.Manufacturer.ToString(); log.dispatchLogMessage(messg); // System Name messg = "Mainservices : Name : " + br.Name.ToString(); log.dispatchLogMessage(messg); //Software Manufacturer messg = "Mainservices : Software Manufacturer :" + br.SoftwareManufacturer.ToString(); log.dispatchLogMessage(messg); log.dispatchLogMessage("Mainservices : Bluetooth Radio initiated"); log.dispatchLogMessage("***"); // This must be put in a try block InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient(); InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices(); log.dispatchLogMessage("Mainservices : Bluetooth Devices found in vicinity"); int count = array.Length; BluetoothDeviceInfo dev; for (int i = 0; i < count; i++) { Device device = new Device(array[i]); if (device.DeviceName == deviceName) { /* * Here the maxTries refers to the number of times we will make an attempt to "connect" to the device . Later * the maxTries refer to the number of times we will make an attemp to "checkconnection" that was established earlier. */ int attempts = 0; int maxTries = 3; while (attempts < maxTries) { log.dispatchLogMessage("Mainservices : Found a Bluetooth device to connect to "); log.dispatchLogMessage("Mainservices : Attempting connection to : " + deviceName); dev = array[i]; var addr = device.DeviceAddress; if (addr == null) { return; } Guid serviceClass = BluetoothService.SerialPort; // Make a connection to the bluetooth device and continously check for pairing // // Make a connection to the specified Bluetooth device try { connectBluetoothDevice(bc, addr, serviceClass); /* * Here we fetch the RSSI values of the strength of the signal established betweem the system * and bluetooth device . Currently we just log it . Maybe later we make more better use of it. */ try { int sigStrgt = dev.Rssi; var mssg = "Mainservices : Signal strength of the connection is" + sigStrgt; log.dispatchLogMessage(mssg); } catch (Exception e) { /* Rssi query may fail on certain platforms . Check http://inthehand.com/library/html/P_InTheHand_Net_Sockets_BluetoothDeviceInfo_Rssi.htm for platfrom details . Handle failure gracefully. */ var mssg = e.Message; mssg += "Mainservices : Signal strength cannot be determined"; log.dispatchLogMessage(mssg); } checkconnection(dev, serviceClass, bc); /* * If you fall out from checkconnection you probably mean that the connection no longer exits . * So increment attempts. */ attempts++; } catch (Exception ex) { // handle exception attempts++; var msg = "Mainservices : Bluetooth connection failed: " + ex.Message; log.dispatchLogMessage(msg); log.dispatchLogMessage("Mainservices : Re-initiating connection"); msg = "Mainservices : Attempt " + attempts; log.dispatchLogMessage(msg); } } } } }