2013-05-15 83 views
1

我想自動檢測周圍的我的電腦的所有設備和文件發送給他們通過藍牙發送文件在C#應用程序

我使用brecham和inthehand dll的,

這裏是我的代碼:

 BluetoothClient bc = new BluetoothClient(); 
     BluetoothDeviceInfo[] info = null; 
     info = bc.DiscoverDevices(999); 
     foreach (BluetoothDeviceInfo device in info) 
     { 
      lstDevices.Items.Add(device.DeviceName + device.DeviceAddress); 
      device.Update(); 
      device.Refresh(); 
      device.SetServiceState(BluetoothService.ObexObjectPush, true); 

      if (!device.Authenticated) 
      { 
       // Use pin "0000" for authentication 
       if (!BluetoothSecurity.PairRequest(device.DeviceAddress, "0000")){ 
        MessageBox.Show("Request failed"); 
       } 

      } 

      var file = @"C:\1.jpg"; 
      var uri = new Uri("obex://" + info[1].DeviceAddress + "/" + file); 
      var request = new ObexWebRequest(uri); 
      request.ReadFile(file); 
      var response = (ObexWebResponse)request.GetResponse(); 
      MessageBox.Show(response.StatusCode.ToString()); 
      //check response.StatusCode 
      response.Close(); 
     } 

但我收到消息「請求失敗!」 請誰能糾正我?

任何人都有想法?

回答

2

解決的問題與代碼中的一個小變化:

 if (!BluetoothRadio.IsSupported) 
      MessageBox.Show("No Bluetooth device detected."); 
     if (BluetoothRadio.PrimaryRadio.Mode == RadioMode.PowerOff) 
      BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable; 
     MessageBox.Show(BluetoothRadio.PrimaryRadio.Name.ToString()); 
     MessageBox.Show(BluetoothRadio.PrimaryRadio.Mode.ToString()); 
     BluetoothClient bc = new BluetoothClient(); 
     BluetoothDeviceInfo[] info = null; 
     info = bc.DiscoverDevices(999); 
     foreach (BluetoothDeviceInfo device in info) 
     { 
      lstDevices.Items.Add(device.DeviceName + " - " + device.DeviceAddress); 
      device.Update(); 
      device.Refresh(); 
      device.SetServiceState(BluetoothService.ObexObjectPush, true); 
      if (!device.Authenticated){ 
       // Use pin "0000" for authentication 
       if (!BluetoothSecurity.PairRequest(device.DeviceAddress, "0000")){ 
        MessageBox.Show("Request failed"); 
       } 
      } 
      var file = @"d:\1.jpg"; 
      var uri = new Uri("obex://" + device.DeviceAddress + "/" + file); 
      var request = new ObexWebRequest(uri); 
      request.ReadFile(file); 
      var response = (ObexWebResponse)request.GetResponse(); 
      MessageBox.Show(response.StatusCode.ToString()); 
      // check response.StatusCode 
      response.Close(); 
     } 

希望它是有用的你,每個人都需要它:)

+0

感謝您的代碼,這樣的好大塊!我只是想補充一點,你需要從Nuget安裝32feet.NET庫 - 它會將必要的InTheHand.Net.Personal.dll添加到項目中。 – CatCap

+0

謝謝......它爲我工作.... tooooo –