public async void ReceiveFile() { if (BluetoothRadio.IsSupported) { if (numReceiving < 6) { statusDisplay.Text = (++numReceiving).ToString(); BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable; await Task.Run(() => { ObexListener listener = new ObexListener(ObexTransport.Bluetooth); listener.Start(); ObexListenerContext context = listener.GetContext(); ObexListenerRequest request = context.Request; string[] pathSplits = request.RawUrl.Split('/'); string fileName = pathSplits[pathSplits.Length - 1]; request.WriteFile(filePath + fileName); listener.Stop(); listener.Close(); }); statusDisplay.Text = (--numReceiving).ToString(); } else { MessageBox.Show("Number of pending transfers is limited to 6", "FYI", MessageBoxButton.OK, MessageBoxImage.Information); } } else { MessageBox.Show("Bluetooth must be enabled on your device for this function to work", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void DealWithRequest() { while (mObexListener.IsListening) { try { ObexListenerContext olc = mObexListener.GetContext(); ObexListenerRequest olr = olc.Request; string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' })); // olr.WriteFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename); olr.WriteFile(Environment.SpecialFolder.MyDocuments + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename); // olr.WriteFile("\\My Documents\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename); // 匿名方法 (Anonymous Method) // 匿名方法要求參數的是一個委託(delegate)類型, 編譯器在處理匿名方法的時候, 需要指定這個匿名方法將會返回什麼類型的委託, MethodInvoke和Action都是方法返回類型為空的委託 BeginInvoke(new MethodInvoker(delegate() { UpdateBTDeviceDetails("Received file : " + filename + "\r\nFolder Path :" + System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); })); } catch (Exception ex) { BeginInvoke(new MethodInvoker(delegate() { UpdateBTDeviceDetails(ex.Message); })); continue; } } }
void Foo(ObexListener lsnr) { ObexListenerContext ctx = lsnr.GetContext(); if (ctx != null) { ObexListenerRequest req = ctx.Request; } }
public static async Task <ObexListenerContext> GetContextAsync(this ObexListener obexListener, int timeout) { var task = Task.Run(() => obexListener.GetContext()); if (await Task.WhenAny(task, Task.Delay(timeout)) == task) { return(task.Result); } else { throw new TimeoutException(); } }
static private void listen() { while (true) { var listener = new ObexListener(ObexTransport.Bluetooth); listener.Start(); ObexListenerContext con = listener.GetContext(); ObexListenerRequest req = con.Request; String[] pathSplits = req.RawUrl.Split('/'); String filename = pathSplits[pathSplits.Length - 1]; req.WriteFile(filename); listener.Stop(); } }
private void buttonReceive_Click(object sender, EventArgs e) { ObexListener listener = new ObexListener(); listener.Start(); ObexListenerContext context = listener.GetContext(); ObexListenerRequest request = context.Request; String[] pathSplits = request.RawUrl.Split('/'); String filename = pathSplits[pathSplits.Length - 1]; request.WriteFile(filename); listener.Stop(); }
// Funkcja pozwalająca na odbieranie plików private void ReceiveFiles() { while (isPaired) { var listener = new ObexListener(ObexTransport.Bluetooth); listener.Start(); ObexListenerContext ctx = listener.GetContext(); ObexListenerRequest req = ctx.Request; String[] pathSplits = req.RawUrl.Split('/'); String file = pathSplits[pathSplits.Length - 1]; req.WriteFile(file); textBoxConsole.Text += "Odebrano plik.\r\n"; listener.Stop(); } }
void ObexSvr1() { var lsnr = new ObexListener(ObexTransport.Bluetooth); lsnr.Start(); // For each connection ObexListenerContext ctx = lsnr.GetContext(); ObexListenerRequest req = ctx.Request; String[] pathSplits = req.RawUrl.Split('/'); String filename = pathSplits[pathSplits.Length - 1]; req.WriteFile(filename); // lsnr.Stop(); }
private void ReceiveFile() { textBox1.Text += ("Rozpoczynam nasluchiwanie na nowe wiadomosci...") + Environment.NewLine; // wykonuje do momenu gdy nie zostaniemy rozlaczeni z urzadzeniem while (czySparowano) { var listener = new ObexListener(ObexTransport.Bluetooth); listener.Start(); ObexListenerContext ctx = listener.GetContext(); ObexListenerRequest req = ctx.Request; String[] pathSplits = req.RawUrl.Split('/'); String filename = pathSplits[pathSplits.Length - 1]; req.WriteFile(filename); textBox1.Text += (filename); listener.Stop(); } }
private void receiveFile()//收文件方法 { ObexListenerContext context = null; ObexListenerRequest request = null; while (listener.IsListening) { context = listener.GetContext();//获取监听上下文 if (context == null) { break; } request = context.Request; //获取请求 string uriString = Uri.UnescapeDataString(request.RawUrl); //将uri转换成字符串 string recFileName = recDir + uriString; request.WriteFile(recFileName); //接收文件 Console.WriteLine("收到文件" + uriString.TrimStart(new char[] { '/' })); } }
public void DealWithRequest() { while (ol.IsListening) { try { ObexListenerContext olc = ol.GetContext(); ObexListenerRequest olr = olc.Request; string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' })); olr.WriteFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename); this.Invoke(new AddToListDelegate(AddToList), new object[] { filename }); } catch (Exception) { break; } } }
//static string BytesToHexAlpha(byte[] bytes) //{ // StringBuilder sb = new StringBuilder(); // foreach(byte b in bytes) { // char c = Convert.ToChar(b); // if (!Char.IsControl(c)) { // sb.Append(c); // } else { // sb.Append("."); // } // } // return sb.ToString(); //} static void ReceiveFiles() { BluetoothRadio br = BluetoothRadio.PrimaryRadio; if (br == null) { Console.WriteLine("No BlueTooth radio found"); return; } if (br.Mode != RadioMode.Discoverable) { br.Mode = RadioMode.Discoverable; } var listener = new ObexListener(ObexTransport.Bluetooth); listener.Start(); Console.WriteLine("Listening for files... press any key to stop"); //need to use another thread since we have two blocking calls var stopit = Task.Factory.StartNew(() => { Console.ReadKey(true); //blocks listener.Stop(); }); while (listener.IsListening) { try { var olc = listener.GetContext(); //blocks var olr = olc.Request; string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' })); string final = DateTime.Now.ToString("yyMMddHHmmss") + "-" + filename; olr.WriteFile(final); Console.WriteLine("Wrote " + final); } catch (Exception e) { e.ToString(); break; } } }
public void DealWithRequest() { while (listener.IsListening) { try { ObexListenerContext olc = listener.GetContext(); ObexListenerRequest olr = olc.Request; string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' })); olr.WriteFile("\\My Documents\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename); items.Add(filename); } catch (Exception) { break; } this.Invoke(new EventHandler(RedrawList)); } }
public MemoryStream IniciarRecepcionImagenBT() { MemoryStream archivo = new MemoryStream(); BluetoothRadio radio = BluetoothRadio.PrimaryRadio; if (radio == null) { throw new Exception("No bluetooth device connected"); } else { radio.Mode = RadioMode.Discoverable; } ObexListener receptorImagenes = new ObexListener(ObexTransport.Bluetooth); receptorImagenes.Start(); //MessageBox.Show("Esperando imagen", "Receptor Imagenes", MessageBoxButtons.OK); ObexListenerContext contexto = receptorImagenes.GetContext(); //MessageBox.Show("Imagen Recibida", "Receptor Imagenes", MessageBoxButtons.OK); ObexListenerRequest req = contexto.Request; string archivoDireccion = req.RawUrl; string extension = req.RawUrl.Split('.').ToList().LastOrDefault(); archivo = new MemoryStream(); req.InputStream.CopyTo(archivo); string b64 = Convert.ToBase64String(archivo.ToArray()); //req.WriteFile("."+ archivoDireccion); //File.WriteAllBytes("./b64." + extension, archivo.ToArray()); receptorImagenes.Stop(); return(archivo); }
/// <summary> /// Receive (fetch) a file from an attached external bluetooth device. /// You must first Initialise, then set the device. /// </summary> /// <param name="fileName">The location to save the received file.</param> /// <returns>A result status message "True" or "False".</returns> public static Primitive ReceiveFile(Primitive fileName) { BluetoothDeviceInfo info = GetBluetoothDeviceInfo(device); if (null == info || null == bluetoothClient) { lastError = "Device or client not set"; return("False"); } try { ObexListener ol = new ObexListener(ObexTransport.Bluetooth); ol.Start(); while (ol.IsListening) { try { ObexListenerContext olc = ol.GetContext(); ObexListenerRequest olr = olc.Request; olr.WriteFile(fileName); ol.Stop(); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); lastError = ex.Message; return("False"); } } return("True"); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); lastError = ex.Message; return("False"); } }
public void DealWithRequest() { while (ol.IsListening) { //receive files via bluetooth try { ObexListenerContext olc = ol.GetContext(); ObexListenerRequest olr = olc.Request; string filename = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\" + DateTime.Now.ToString("dd-MM-yy HHmm") + " " + Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' })); //remove newline filename = filename.Substring(0, filename.Length - 1); Console.Write("filetype: " + filename.Substring(filename.Length - 3, 3)); olr.WriteFile(filename); //add filename to fileList DoFileListUpdate(filename); if (filename.Substring(filename.Length - 3, 3).Equals("jpg") || filename.Substring(filename.Length - 3, 3).Equals("JPG") || filename.Substring(filename.Length - 3, 3).Equals("png") || filename.Substring(filename.Length - 3, 3).Equals("PNG") || filename.Substring(filename.Length - 3, 3).Equals("GIF") || filename.Substring(filename.Length - 3, 3).Equals("gif")) { //image upload to picasa Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumid)); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename); System.IO.FileStream fileStream = fileInfo.OpenRead(); PicasaEntry entry = (PicasaEntry)picasaService.Insert(postUri, fileStream, "image/jpeg", filename); fileStream.Close(); //image properties entry.Title.Text = "Gogbot09: " + olr.RawUrl.TrimStart(new char[] { '/' }); entry.Summary.Text = "User content for GOGBOT 2009 festival, provided by bluetooth device: " + olr.UserHostAddress + " on: " + DateTime.Now.ToString("dd-MM-yy HHmm"); entry.Media.Keywords.Value = "gogbot, GOGBOT09, gogbot2009, bluetube"; //entry.Location = new GeoRssWhere(); //entry.Location.Latitude = 37; //entry.Location.Longitude = -122; PicasaEntry updatedEntry = (PicasaEntry)entry.Update(); Console.WriteLine("Photo: " + olr.RawUrl.TrimStart(new char[] { '/' }) + " uploaded."); } else { //upload video to youtube Video newVideo = new Video(); newVideo.Title = "Gogbot09: " + olr.RawUrl.TrimStart(new char[] { '/' }); newVideo.Tags.Add(new MediaCategory("People", YouTubeNameTable.CategorySchema)); newVideo.Keywords = "gogbot, GOGBOT09, gogbot2009, bluetube"; newVideo.Description = "User content for GOGBOT 2009 festival, provided by bluetooth device: " + olr.UserHostAddress + " on: " + DateTime.Now.ToString("dd-MM-yy HHmm"); newVideo.YouTubeEntry.Private = false; newVideo.Tags.Add(new MediaCategory("uploaded_through_bluetube", YouTubeNameTable.DeveloperTagSchema)); //newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122); // alternatively, you could just specify a descriptive string newVideo.YouTubeEntry.setYouTubeExtension("location", "Vrije Universiteit, Amsterdam"); newVideo.YouTubeEntry.MediaSource = new MediaFileSource(filename, "video/3gpp"); Video video = request.Upload(newVideo); //Check status if (video.IsDraft) { Console.WriteLine("Video is not live."); string stateName = video.Status.Name; if (stateName == "processing") { Console.WriteLine("Video is still being processed."); } else if (stateName == "rejected") { Console.Write("Video has been rejected because: "); Console.WriteLine(video.Status.Value); Console.Write("For help visit: "); Console.WriteLine(video.Status.Help); } else if (stateName == "failed") { Console.Write("Video failed uploading because:"); Console.WriteLine(video.Status.Value); Console.Write("For help visit: "); Console.WriteLine(video.Status.Help); } } } } catch (Exception e) { Console.WriteLine(e); } } }