/// <summary> /// Attempt to connect to the BluetoothDevice, without trying to pair first. /// </summary> /// <param name="device"></param> /// <returns></returns> public static async Task<Boolean> connectToDeviceWithoutPairing(BluetoothDevice device) { BluetoothEndPoint endPoint = new BluetoothEndPoint(device.btDeviceInfo.DeviceAddress, BluetoothService.SerialPort); BluetoothClient client = new BluetoothClient(); if (!device.Authenticated) { return false; } else { try { client.Connect(endPoint); if (devicesStreams.Keys.Contains(device)) { devicesStreams.Remove(device); } devicesStreams.Add(device, client.GetStream()); if (devicesClients.Keys.Contains(device)) { devicesClients.Remove(device); } devicesClients.Add(device, client); } catch (Exception ex) { //System.Console.Write("Could not connect to device: " + device.DeviceName + " " + device.DeviceAddress); return false; } return true; } }
/// <summary> /// The worker will try to connect to the specified device and create a listener for /// the Bluetooth port messages. /// This must be canned async in a separate thread! /// </summary> public async void Work(BluetoothDevice btDevice) { try { Boolean success = await Bluetooth.connectToDeviceWithoutPairing(btDevice); if (success) { await BluetoothStreamListener(btDevice); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void disconnectFromDevice(BluetoothDevice device) { if (devicesClients.Keys.Contains(device)) { try { devicesClients[device].Close(); devicesClients.Remove(device); devicesStreams.Remove(device); } catch (Exception e) { } } }
public static Boolean pairWithDevice(BluetoothDevice device) { return pairWithDevice(device, null); }
public static Boolean pairWithDevice(BluetoothDevice device, String passCode) { return BluetoothSecurity.PairRequest(device.btDeviceInfo.DeviceAddress, passCode); }
public bool Equals(BluetoothDevice btDevice) { if ((object)btDevice == null) { return false; } return DeviceAddress.Equals(btDevice.DeviceAddress); }
/// <summary> /// Async Bluetooth Stream Listener waiting and reading messages on the specified Stream. /// On message received, the function will try to parse it based on the device type /// and store it to the Azure Database in its specific table. /// </summary> /// <param name="btDevice"></param> /// <returns></returns> private async Task BluetoothStreamListener(BluetoothDevice btDevice) { if (!Bluetooth.devicesStreams.Keys.Contains(btDevice)) { //failed to connect to device return; } Stream s = Bluetooth.devicesStreams[btDevice]; while (true) { byte[] buffer = new byte[1]; string result = ""; string bufferChar = ""; while (!bufferChar.Equals("}")) { try { s.Read(buffer, 0, 1); } catch (Exception e) { continue; } bufferChar = Encoding.UTF8.GetString(buffer); result += bufferChar; } Console.WriteLine(result); LoTDevice device; if (btDevice.DeviceType.Equals(BluetoothDevice.DEVICE_ENGDUINO)) { device = new Engduino(); } else if (btDevice.DeviceType.Equals(BluetoothDevice.DEVICE_ANDROIDPHONE)) { device = new AndroidPhone(); } else { //maybe print the message in the console continue; } device.MAC = btDevice.DeviceAddress; try { device.parseMessage(result); } catch (Exception e) { logger.Log("Failed to parse message: " + result + " for device: " + btDevice.DeviceName); continue; } try { await mobileService.WriteDevice(device); } catch (HttpRequestException ex) { logger.Log("No internet connection on writing to Azure MS.", ex.ToString()); } catch (Exception e) { logger.Log("Failed to write to Azure MS.", e.ToString()); } } }