Communication between Bluetooth devices

C

I had used this communication to get the surrounding temperature, but we can also use this to develop chat room which will be offline.

BluetoothSPP bt; //Bluetooth Socket

bt = new BluetoothSPP(this); //initilisation

// check weather Bluetooth is on/off

 

if (!bt.isBluetoothEnabled()) {

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);

}

else {

if(!bt.isServiceAvailable())

{

bt.setupService();

bt.startService(BluetoothState.DEVICE_ANDROID);

setup();

}

 

BluetoothAdapter.getDefaultAdapter();

if(!bt.isBluetoothAvailable())

{

Toast.makeText(getApplicationContext() , “Bluetooth is not available” , Toast.LENGTH_SHORT).show();

finish();

}

bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {

public void onDeviceDisconnected() {

//do something if device get disconnected

}

public void onDeviceConnectionFailed() {

//do something if device connection get failed

}

public void onDeviceConnected(String name, String address) {

//  do something if device get connected

// name is the name of connected  device  and address is the unique is of bluetooth

}

}

bt.setOnDataReceivedListener(new OnDataReceivedListener()

{

public void onDataReceived(byte[] data,String message)

{

// when any message is get received

}

}

 

About the author

rohit.gujar
By rohit.gujar

Category