Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here the code server code in c#
C#
            BluetoothClient bc = new BluetoothClient();
            BluetoothDeviceInfo[] devices;
            //BluetoothDeviceInfo info = null;
            Guid id = new Guid("{00112233-4455-6677-8899-aabbccddeeff}");
            Console.WriteLine(id.ToString());
            Console.WriteLine("Waiting.......");
            Console.ReadLine();
            devices = bc.DiscoverDevices();
try
            {
               BluetoothListener bl = new BluetoothListener(id);
                bl.Start();
                if (bl.AcceptSocket() != null)
                    Console.WriteLine("Successfully Connected");
                   Console.Read();
             }
            catch (Exception e)
            {
                Console.WriteLine("Exception : " + e.Message);
                Console.Read();
            }

-----------------------------------------------------------------------------
Here the Andriod Client Code
public class MainActivity extends Activity {


BluetoothAdapter adapter;
BluetoothDevice bd;
BluetoothSocket sock;
OutputStream ostr;
int REQUEST_ENABLE_BT;
String str="6C:AC:6C:DD:6C:0D"; // BlueTooth Device Id

private static final UUID id=UUID.fromString("00112233-4455-6677-8899- aabbccddeeff");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
adapter=BluetoothAdapter.getDefaultAdapter();

if (!adapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}


Button b = (Button) findViewById(
le="http://R.id">R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
 Toast.makeText(getApplicationContext(), "clicked button",            Toast.LENGTH_LONG).show();
    try
    { 
        bd=adapter.getRemoteDevice(str);                      Toast.makeText(getApplicationContext(),"Server is    running at   "+bd.getName().toString()+"...", Toast.LENGTH_LONG).show();
                   sock=bd.createInsecureRfcommSocketToServiceRecord(id);                    sock.connect();
                ostr=sock.getOutputStream();
                ostr.write(0);
            }
            catch(Exception e)
            {
                 Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });

}
Posted
Updated 20-Apr-15 20:36pm
v2
Comments
Richard MacCutchan 9-Apr-15 2:36am    
Where?
Srikanth59 9-Apr-15 6:56am    
am getting error in the android (Client) while connecting to the server ( c# code )
Richard MacCutchan 9-Apr-15 7:06am    
Well I am afraid that we cannot guess: where the error occurs, or what type of error it is. I suggest you use your debugger to identify exactly what is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900