Hello Viwer,
My application is currently woking fine by using Obex(OPP) but i want to do same using SPP profile.
so what are the nessary changes i need to do in coding in windows xp PC using c#.
Code C#
public List<BluetoothDeviceInfo> DiscoverAllDevices()
{
List<BluetoothDeviceInfo> _devices = new List<BluetoothDeviceInfo>();
if (BluetoothRadio.PrimaryRadio != null)
{
BluetoothClient bc = new BluetoothClient();
try
{
Status = "Discovering the Devices.....";
BluetoothDeviceInfo[ dev = bc.DiscoverDevices();
_devices.AddRange(dev);
Status = "Discovering the Devices Completed.....";
}
catch (Exception ex)
{
Status = ex.Message;
}
}
else
{
Status = "The file could not be sent because the connection could not be established. Check to make sure the Bluetooth radio is plugged in.";
}
return _devices;
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (OBEXOpenStream())
{
BlueToothState = ConnectionState.Connecting;
BlueToothState = ConnectionState.Streaming;
ol = new ObexListener ( ObexTransport.Bluetooth );
ol.Start();
while (ol.IsListening)
{
try
{
ObexListenerContext obexListenerContext = ol.GetContext ( );
if(obexListenerContext != null)
{
ObexListenerRequest obexListenerRequest = obexListenerContext.Request;
InTheHand.Net.BluetoothEndPoint btEndPoint = (InTheHand.Net.BluetoothEndPoint) obexListenerRequest.RemoteEndPoint;
BluetoothDeviceInfo l = new BluetoothDeviceInfo ( btEndPoint.Address );
InTheHand.Net.BluetoothAddress theSenderAddress = btEndPoint.Address;
string filename = System.Environment.GetFolderPath ( System.Environment.SpecialFolder.Personal ) + "\\" + obexListenerRequest.RawUrl.Substring ( 1, 12 );//Uri.UnescapeDataString(obexListenerRequest.RawUrl.TrimStart(new char[ { '/' }));
obexListenerRequest.WriteFile ( filename );
}
}
catch (Exception ex)
{
MessageBox.Show ( ex.Message );
}
}
// }
// else
// {
//addtolog("Fail to get the File");
/// }
//OBEXCloseStream();
}
}
private bool OBEXOpenStream()
{
string BTMAC = _deviceInfo.DeviceAddress.ToString();
string BTName = _deviceInfo.DeviceName.ToString();
string DeviceParingPin = "0000";
BlueToothState = ConnectionState.Connecting;
Status = "Paring with Selected Device.... ";
Guid spguid = BluetoothService.ObexObjectPush ;
btaddress = BluetoothAddress.Parse(BTMAC);
client = new BluetoothClient();
//client.Authenticate = true;
//BluetoothSecurity.RemoveDevice(btaddress);
BlueToothState = ConnectionState.Pairing;
if (BluetoothSecurity.PairRequest(btaddress, DeviceParingPin))
{
if(OnDevicePaired != null)
{
OnDevicePaired ( );
}
Status = "Device Paired and reday to Receive the data ";
endpoint = new BluetoothEndPoint(btaddress, spguid,1);
try
{
client.Connect(endpoint);
}
catch (System.Exception e)
{
//unable to connect (server not listening on spguid)
Status = e.Message;
return false;
}
//connect socket
stream = client.GetStream();
return true;
}
else
{
/// addtolog("Invalid Pin");
Status = "Pairing failed/Client device is not enabled BT";
BlueToothState = ConnectionState.Disconnected;
return false;
}
}
private void OBEXCloseStream()
{
if(stream != null)
stream.Close();
if(client != null)
client.Close();
}
Please Replay Me ASAP
Thanks In advance