HI,
I can use this code to send a file from Pocket pc to pocket pc
#region Using directives
using System;
using System.IO;
using System.Net.Sockets;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using InTheHand;
using InTheHand.IO;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Forms;
using InTheHand.Net.Sockets;
#endregion
namespace SendXmlFile
{
public class Form1 : System.Windows.Forms.Form
{
private Button SelectFile;
private Button ConnectDevices;
private Button SendFile;
private ComboBox deviceComboBox;
private Button DiscoverDevices;
private MenuItem menuItem1;
private OpenFileDialog openFileDialog1;
private System.Windows.Forms.MainMenu mainMenu1;
public BluetoothClient btClient = new BluetoothClient();
public string fileName;
public string data;
public bool fileSent = false;
public NetworkStream stream;
private TextBox textBox1;
InTheHand.Net.BluetoothAddress[] address_array = new BluetoothAddress[1000];
#region Form1() Constructor,Dispose Method,Form Designer Generated Code,Main Method
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.SelectFile = new System.Windows.Forms.Button();
this.ConnectDevices = new System.Windows.Forms.Button();
this.SendFile = new System.Windows.Forms.Button();
this.deviceComboBox = new System.Windows.Forms.ComboBox();
this.DiscoverDevices = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// menuItem1
//
this.menuItem1.Text = "CLOSE";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// SelectFile
//
this.SelectFile.Location = new System.Drawing.Point(157, 87);
this.SelectFile.Size = new System.Drawing.Size(83, 26);
this.SelectFile.Text = "Select File";
this.SelectFile.Visible = false;
this.SelectFile.Click += new System.EventHandler(this.SelectFile_Click);
//
// ConnectDevices
//
this.ConnectDevices.Location = new System.Drawing.Point(0, 87);
this.ConnectDevices.Size = new System.Drawing.Size(72, 26);
this.ConnectDevices.Text = "Connect";
this.ConnectDevices.Click += new System.EventHandler(this.ConnectDevices_Click);
//
// SendFile
//
this.SendFile.Location = new System.Drawing.Point(78, 87);
this.SendFile.Size = new System.Drawing.Size(76, 26);
this.SendFile.Text = "Send File";
this.SendFile.Click += new System.EventHandler(this.SendFile_Click);
//
// deviceComboBox
//
this.deviceComboBox.Location = new System.Drawing.Point(124, 36);
this.deviceComboBox.Size = new System.Drawing.Size(113, 22);
//
// DiscoverDevices
//
this.DiscoverDevices.Location = new System.Drawing.Point(3, 36);
this.DiscoverDevices.Size = new System.Drawing.Size(72, 20);
this.DiscoverDevices.Text = "Discover";
this.DiscoverDevices.Click += new System.EventHandler(this.DiscoverDevices_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(3, 135);
this.textBox1.Multiline = true;
this.textBox1.Size = new System.Drawing.Size(234, 130);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(240, 268);
this.ControlBox = false;
this.Controls.Add(this.textBox1);
this.Controls.Add(this.SelectFile);
this.Controls.Add(this.ConnectDevices);
this.Controls.Add(this.SendFile);
this.Controls.Add(this.deviceComboBox);
this.Controls.Add(this.DiscoverDevices);
this.Menu = this.mainMenu1;
this.Text = "Send File Version 1.0";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
#endregion
#region Form load
private void Form1_Load(object sender, EventArgs e)
{
this.ConnectDevices.Enabled = false;
this.SendFile.Enabled = false;
this.SelectFile.Enabled = false;
}
#endregion
#region Discover Devices
private void DiscoverDevices_Click(object sender, EventArgs e)
{
this.DiscoverDevices.Enabled = false;
BluetoothClient bluetoothClient = new BluetoothClient();
BluetoothDeviceInfo[] bluetoothDeviceInfo = bluetoothClient.DiscoverDevices();
for (int i = 0; i < bluetoothDeviceInfo.Length; i++)
{
this.address_array
= bluetoothDeviceInfo
.DeviceAddress;
}
MessageBox.Show(bluetoothDeviceInfo.Length + " Devices found");
deviceComboBox.DataSource = bluetoothDeviceInfo;
deviceComboBox.DisplayMember = "DeviceName";
deviceComboBox.ValueMember = "DeviceAddress";
deviceComboBox.Focus();
this.ConnectDevices.Enabled = true;
}
#endregion
#region Connect Device
private void ConnectDevices_Click(object sender, EventArgs e)
{
try
{
btClient = new BluetoothClient();
btClient.Connect(new BluetoothEndPoint((BluetoothAddress)deviceComboBox.SelectedValue, BluetoothService.ObexObjectPush));
if (btClient.Connected)
{
MessageBox.Show("connected");
this.SelectFile.Enabled = true;
this.SendFile.Enabled = true;
}
else
{
MessageBox.Show("Not Connected");
this.DiscoverDevices.Enabled = true;
}
this.ConnectDevices.Enabled = false;
}
catch (SocketException se)
{
MessageBox.Show(se.ToString());
}
}
#endregion
#region Select File
private void SelectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
}
this.SelectFile.Enabled = false;
this.SendFile.Enabled = true;
}
#endregion
#region Send File
private void SendFile_Click(object sender, EventArgs e)
{
if (btClient.Connected)
{
stream = (NetworkStream)btClient.GetStream();
string fName = Path.GetFileName(fileName);
string fType = "";
StreamReader sr = File.OpenText(fileName);
string fFileContent = sr.ReadToEnd();
sr.Close();
//send client request, start put
int result = OBEXRequest("PUT",fName,fType, fFileContent);
switch (result)
{
case 160: // 0xa0
MessageBox.Show("File Send");
break;
case 197: // 0xc5
MessageBox.Show("Method not allowed");
break;
case 192: // 0xc0
MessageBox.Show("Bad Request");
break;
default:
MessageBox.Show("Other Error");
break;
}
}
else
{
MessageBox.Show("Failed to connect to OBEX Server");
}
//this.SendFile.Enabled = false;
this.SelectFile.Enabled = true;
this.DiscoverDevices.Enabled = true;
}
private int OBEXRequest(string tReqType, string tName,string tType ,string tFileContent)
{
//send client request
int i;
int offset;
int packetsize;
byte reqtype = 0x82;
int tTypeLen = 0x03;
int typeheadsize;
int typesizeHi = 0x00;
int typesizeLo = 0x03;
if (tReqType == "GET")
{
reqtype = 0x83; // 131 GET-Final
}
if (tReqType == "PUT")
{
reqtype = 0x82; // 130 PUT-Final
}
packetsize = 3;
//Name Header
int tNameLength = tName.Length;
int nameheadsize = (3 + (tNameLength * 2) + 2);
int namesizeHi = (nameheadsize & 0xff00) / 0xff;
int namesizeLo = nameheadsize & 0x00ff;
packetsize = packetsize + nameheadsize;
if (tType != "")
{
//Type Header
tTypeLen = tType.Length;
typeheadsize = 3 + tTypeLen + 1;
typesizeHi = (typeheadsize & 0xff00) / 0xff;
typesizeLo = typeheadsize & 0x00ff;
packetsize = packetsize + typeheadsize;
}
//Body
int fileLen = tFileContent.Length;
int fileheadsize = 3 + fileLen;
int filesizeHi = (fileheadsize & 0xff00) / 0xff; ;
int filesizeLo = fileheadsize & 0x00ff; ;
packetsize = packetsize + fileheadsize;
int packetsizeHi = (packetsize & 0xff00) / 0xff;
int packetsizeLo = packetsize & 0x00ff;
byte[] tSendByte = new byte[packetsize];
//PUT-final Header
tSendByte[0] = reqtype; // Request type e.g. PUT-final 130
tSendByte[1] = Convert.ToByte(packetsizeHi); // Packetlength Hi
tSendByte[2] = Convert.ToByte(packetsizeLo); // Packetlength Lo
offset = 2;
//Name Header
tSendByte[offset + 1] = 0x01; // HI for Name header
tSendByte[offset + 2] = Convert.ToByte(namesizeHi); // Length of Name header (2 bytes per char)
tSendByte[offset + 3] = Convert.ToByte(namesizeLo); // Length of Name header (2 bytes per char)
// Name+\n\n in unicode
byte[] tNameU = System.Text.Encoding.BigEndianUnicode.GetBytes(tName);
tNameU.CopyTo(tSendByte, offset + 4);
offset = offset + 3 + (tNameLength * 2);
tSendByte[offset + 1] = 0x00; // null term
tSendByte[offset + 2] = 0x00; // null term
offset = offset + 2;
if (tType != "")
{
//Type Header
tSendByte[offset + 1] = 0x42; // HI for Type Header 66
tSendByte[offset + 2] = Convert.ToByte(typesizeHi); // Length of Type Header
tSendByte[offset + 3] = Convert.ToByte(typesizeLo); // Length of Type Header
for (i = 0; i <= (tTypeLen - 1); i++)
{
tSendByte[offset + 4 + i] = Convert.ToByte(Convert.ToChar(tType.Substring(i, 1)));
}
tSendByte[offset + 3 + tTypeLen + 1] = 0x00; // null terminator
offset = offset + 3 + tTypeLen + 1;
}
//Body
tSendByte[offset + 1] = 0x49; //HI End of Body 73
tSendByte[offset + 2] = Convert.ToByte(filesizeHi); //
tSendByte[offset + 3] = Convert.ToByte(filesizeLo); //1k payload + 3 for HI header
for (i = 0; i <= (fileLen - 1); i++)
{
tSendByte[offset + 4 + i] = Convert.ToByte(Convert.ToChar(tFileContent.Substring(i, 1)));
}
//tSendByte[offset+4+fileLen] = 0x00; // null terminator
offset = offset + 3 + fileLen;
stream.Write(tSendByte, 0, tSendByte.Length);
//listen for server response
//TODO: can hang here forever waiting response...
bool x = stream.DataAvailable; // changed bluetoothclient - public NetworkStream GetStream()
byte[] tArray4 = new byte[3];
stream.Read(tArray4, 0, 3);
x = stream.DataAvailable;
if (tArray4[0] == 160) // 0xa0
{
int plength = (tArray4[1] * 256) + tArray4[2] - 3;
byte[] tArray5 = new byte[plength];
if (plength > 0)
{
stream.Read(tArray5, 0, plength);
data = Convert.ToString(tArray5);
textBox1.Text = data.ToString();
//TODO: data in returned packet to deal with
}
return 160;
}
if (tArray4[0] == 197) // 0xc5 Method not allowed
{
return 197;
}
if (tArray4[0] == 192) // 0xc0 Bad Request
{
return 192;
}
return 0;
}
#endregion
#region Close
private void menuItem1_Click(object sender, EventArgs e)
{
Application.DoEvents();
Application.Exit();
}
#endregion
}
}
Regards,
Krishna