private void Transfer(ObexTransferObject obj) { ObexWebResponse rsp = null; ObexWebRequest req; try { Uri uri = new Uri("obex://" + obj.Target.address.ToString() + "/" + obj.Filename); req = new ObexWebRequest(uri); req.Timeout = 15000; using (Stream content = req.GetRequestStream()) { content.Write(obj.Data, 0, obj.Data.Length); content.Flush(); req.ContentLength = obj.Data.Length; req.ContentType = obj.MimeType; } rsp = (req.GetResponse() as ObexWebResponse); } catch (Exception e) { General.WriteLogLine(e.GetType().Name + " while transferring '" + obj.Filename + "' data to " + obj.Target.address.ToString() + ": " + e.Message); } if ((rsp != null) && (rsp.StatusCode != ObexStatusCode.OK)) { General.WriteLogLine("Received response code: " + rsp.StatusCode + " while transferring '" + obj.Filename + "' data to " + obj.Target.address.ToString()); } if (rsp != null) { rsp.Close(); } }
// This sets the object to be transferred public void TransferSingleObject(ObexTransferObject obj) { List <ObexTransferObject> objs = new List <ObexTransferObject>(1); objs.Add(obj); TransferObjects(objs); }
// Transfer over bluetooth private void transferbutton_Click(object sender, EventArgs e) { string html = Properties.Resources.grocerieshtml; int itembeginpos = html.IndexOf("$ITEMBEGIN$"); int itemendpos = html.IndexOf("$ITEMEND$"); string itemtemplate = html.Substring(itembeginpos + 11, itemendpos - (itembeginpos + 11)); html = html.Replace(itemtemplate, ""); html = html.Replace("$ITEMEND$", ""); string itemlist = ""; for (int i = 0; i < items.Length; i++) { string itemname = items[i].name; if (items[i].count > 1) { itemname += " " + items[i].count + "x"; } string itemstr = itemtemplate; itemstr = itemstr.Replace("$ITEMNAME$", itemname); itemlist += itemstr; } html = html.Replace("$ITEMBEGIN$", itemlist); byte[] data = Encoding.ASCII.GetBytes(html); ObexTransferObject obj = new ObexTransferObject(data); obj.Filename = "Groceries-" + DateTime.Now.Day.ToString("00") + "-" + DateTime.Now.Month.ToString("00") + ".html"; obj.MimeType = "text/plain"; General.MainWindow.ObexTransferPanel.TransferSingleObject(obj); General.MainWindow.ObexTransferPanel.ReturnPanel = "groceries"; General.MainWindow.ShowTaggedPanel("transferobex"); }
// This sends an object to a device public bool SendObject(ObexTransferObject obj) { lock (transferobjects) { transferobjects.Enqueue(obj); } return(true); }
// This sends an object to a device public bool SendObject(ObexTransferObject obj) { lock(transferobjects) { transferobjects.Enqueue(obj); } return true; }
// Transfer selected file private void transferbutton_Click(object sender, EventArgs e) { if (!transferbutton.Enabled) { return; } for (int i = 0; i < allitems.TotalCount; i++) { if (itemselected[i]) { string filepathname = allitems[i].filepathname; FileInfo fi = new FileInfo(filepathname); if (fi.Length < 100000000) { // Determine mime type from file extension string mimetype = "application/unknown"; string ext = allitems[i].extension.ToLower(); RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(ext); if (regkey != null && regkey.GetValue("Content Type") != null) { mimetype = regkey.GetValue("Content Type").ToString(); } // Load the file in memory and queue for sending byte[] data = File.ReadAllBytes(filepathname); ObexTransferObject obj = new ObexTransferObject(data); obj.Filename = allitems[i].filename; obj.MimeType = mimetype; General.MainWindow.ObexTransferPanel.TransferSingleObject(obj); General.MainWindow.ObexTransferPanel.ReturnPanel = "librarybrowser"; General.MainWindow.ShowTaggedPanel("transferobex"); } else { General.Sounds.Play("error"); } } } }
// Transfer over bluetooth private void transferbutton_Click(object sender, EventArgs e) { string html = Properties.Resources.grocerieshtml; int itembeginpos = html.IndexOf("$ITEMBEGIN$"); int itemendpos = html.IndexOf("$ITEMEND$"); string itemtemplate = html.Substring(itembeginpos + 11, itemendpos - (itembeginpos + 11)); html = html.Replace(itemtemplate, ""); html = html.Replace("$ITEMEND$", ""); string itemlist = ""; for(int i = 0; i < items.Length; i++) { string itemname = items[i].name; if(items[i].count > 1) itemname += " " + items[i].count + "x"; string itemstr = itemtemplate; itemstr = itemstr.Replace("$ITEMNAME$", itemname); itemlist += itemstr; } html = html.Replace("$ITEMBEGIN$", itemlist); byte[] data = Encoding.ASCII.GetBytes(html); ObexTransferObject obj = new ObexTransferObject(data); obj.Filename = "Groceries-" + DateTime.Now.Day.ToString("00") + "-" + DateTime.Now.Month.ToString("00") + ".html"; obj.MimeType = "text/plain"; General.MainWindow.ObexTransferPanel.TransferSingleObject(obj); General.MainWindow.ObexTransferPanel.ReturnPanel = "groceries"; General.MainWindow.ShowTaggedPanel("transferobex"); }
// Transfer selected file private void transferbutton_Click(object sender, EventArgs e) { if(!transferbutton.Enabled) return; for(int i = 0; i < allitems.TotalCount; i++) { if(itemselected[i]) { string filepathname = allitems[i].filepathname; FileInfo fi = new FileInfo(filepathname); if(fi.Length < 100000000) { // Determine mime type from file extension string mimetype = "application/unknown"; string ext = allitems[i].extension.ToLower(); RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(ext); if(regkey != null && regkey.GetValue("Content Type") != null) mimetype = regkey.GetValue("Content Type").ToString(); // Load the file in memory and queue for sending byte[] data = File.ReadAllBytes(filepathname); ObexTransferObject obj = new ObexTransferObject(data); obj.Filename = allitems[i].filename; obj.MimeType = mimetype; General.MainWindow.ObexTransferPanel.TransferSingleObject(obj); General.MainWindow.ObexTransferPanel.ReturnPanel = "librarybrowser"; General.MainWindow.ShowTaggedPanel("transferobex"); } else { General.Sounds.Play("error"); } } } }
// This finds and updates the available bluetooth devices private void UpdateThread() { BluetoothClient cli; while (true) { // This is allowed to fail when there are no bluetooth devices connected try { cli = new BluetoothClient(); } catch (ThreadInterruptedException) { return; } catch (Exception e) { General.WriteLogLine(e.GetType().Name + " while updating bluetooth manager: " + e.Message + "\r\n" + e.StackTrace); return; } List <ObexBluetoothDevice> newdevices = null; try { cli.Client.ReceiveTimeout = 1000; cli.Client.SendTimeout = 1000; BluetoothDeviceInfo[] peers = cli.DiscoverDevices(ObexTransferDisplayPanel.NUM_DEVICE_BUTTONS, true, true, true); string p = "Found " + peers.Length + " peers: "; int index = 0; newdevices = new List <ObexBluetoothDevice>(); foreach (BluetoothDeviceInfo bdi in peers) { // Make sure the information on this device is accurate! string addrname = bdi.DeviceName.Replace(":", ""); if (addrname == bdi.DeviceAddress.ToString()) { bdi.Refresh(); bdi.Update(); } p += bdi.DeviceName + " "; // Make info struct newdevices.Add(new ObexBluetoothDevice(bdi)); } General.WriteLogLine(p); } catch (ThreadInterruptedException) { return; } #if !DEBUG catch (NotImplementedException e) { // Ignore this, it's probably due to outdated code in the Bluetooth classes made by 'In The Hand' //General.WriteLogLine(e.GetType().Name + " while updating bluetooth devices: " + e.Message); } #endif catch (Exception e) { General.Fail(e, "while updating bluetooth devices"); } // Make the new devices array on success if (newdevices != null) { lock (this) { devicesinfo = newdevices.ToArray(); } } try { Queue <ObexTransferObject> dotransfers = new Queue <ObexTransferObject>(); lock (transferobjects) { while (transferobjects.Count > 0) { dotransfers.Enqueue(transferobjects.Dequeue()); } } while (dotransfers.Count > 0) { ObexTransferObject obj = dotransfers.Peek(); Transfer(obj); dotransfers.Dequeue(); } } catch (ThreadInterruptedException) { return; } #if !DEBUG catch (NotImplementedException e) { // Ignore this, it's probably due to outdated code in the Bluetooth classes made by 'In The Hand' //General.WriteLogLine(e.GetType().Name + " while updating bluetooth devices: " + e.Message); } #endif catch (Exception e) { General.Fail(e, "while transferring bluetooth data"); } try { Thread.Sleep(300); } catch (ThreadInterruptedException) { return; } } }
private void Transfer(ObexTransferObject obj) { ObexWebResponse rsp = null; ObexWebRequest req; try { Uri uri = new Uri("obex://" + obj.Target.address.ToString() + "/" + obj.Filename); req = new ObexWebRequest(uri); req.Timeout = 15000; using(Stream content = req.GetRequestStream()) { content.Write(obj.Data, 0, obj.Data.Length); content.Flush(); req.ContentLength = obj.Data.Length; req.ContentType = obj.MimeType; } rsp = (req.GetResponse() as ObexWebResponse); } catch(Exception e) { General.WriteLogLine(e.GetType().Name + " while transferring '" + obj.Filename + "' data to " + obj.Target.address.ToString() + ": " + e.Message); } if((rsp != null) && (rsp.StatusCode != ObexStatusCode.OK)) General.WriteLogLine("Received response code: " + rsp.StatusCode + " while transferring '" + obj.Filename + "' data to " + obj.Target.address.ToString()); if(rsp != null) rsp.Close(); }
// This sets the object to be transferred public void TransferSingleObject(ObexTransferObject obj) { List<ObexTransferObject> objs = new List<ObexTransferObject>(1); objs.Add(obj); TransferObjects(objs); }