/// <summary> /// Advanced feature. /// Send data to a bluetooth device. /// The device must be Initialised and Connected. /// The encoding should also be set. /// </summary> /// <param name="data">A string of characters to send.</param> /// <returns>A result status message "True" or "False".</returns> public static Primitive SendData(Primitive data) { BluetoothDeviceInfo info = GetBluetoothDeviceInfo(device); BluetoothServiceItem item = GetBluetoothServiceItem(service); if (null == info || null == item || null == bluetoothClient) { lastError = "Device, service or client not set"; return("False"); } if (null == bluetoothStreamWriter) { lastError = "Cannot write to device."; return("False"); } try { bluetoothStreamWriter.Write((string)data); bluetoothStreamWriter.Flush(); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); lastError = ex.Message; return("False"); } return("True"); }
private static void setServices() { bluetoothServiceItems.Clear(); FieldInfo[] fields = typeof(BluetoothService).GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (FieldInfo field in fields) { Object obj = field.GetValue(null); if (obj.GetType() == typeof(Guid)) { BluetoothServiceItem item = new BluetoothServiceItem((Guid)obj, field.Name); bluetoothServiceItems.Add(item); } } }
/// <summary> /// Advanced feature. /// Receive (fetch) data from a bluetooth device. /// The device must be Initialised and Connected. /// The encoding should also be set. /// </summary> /// <returns>String of received characters or "" on failure.</returns> public static Primitive ReceiveData() { string result = ""; BluetoothDeviceInfo info = GetBluetoothDeviceInfo(device); BluetoothServiceItem item = GetBluetoothServiceItem(service); if (null == info || null == item || null == bluetoothClient) { lastError = "Device, service or client not set"; return(result); } if (null == bluetoothStreamReader) { lastError = "Cannot read from device."; return(result); } try { Char[] buf = new Char[100]; int numRead = 1; while (numRead > 0) { numRead = bluetoothStreamReader.Read(buf, 0, buf.Length); if (numRead > 0) { result += new string(buf, 0, numRead); } } } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); lastError = ex.Message; return(result); } return(result); }
/// <summary> /// Advanced feature. /// Connect an external device and service to the bluetooth USB stick. /// You must first Initialise, then set the device, service and encoding. /// </summary> /// <returns>"True" or "False" on success or failure.</returns> public static Primitive Connect() { BluetoothDeviceInfo info = GetBluetoothDeviceInfo(device); BluetoothServiceItem item = GetBluetoothServiceItem(service); if (null == info || null == item || null == bluetoothClient) { lastError = "Device, service or client not set"; return("False"); } try { bluetoothClient.Connect(info.DeviceAddress, item.guid); bluetoothStreamWriter = new StreamWriter(bluetoothClient.GetStream(), encoding); bluetoothStreamReader = new StreamReader(bluetoothClient.GetStream(), encoding); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); lastError = ex.Message; return("False"); } return("True"); }