connection between two android phones

Posted by user1770346 on Stack Overflow See other posts from Stack Overflow or by user1770346
Published on 2012-10-28T10:58:17Z Indexed on 2012/10/28 11:00 UTC
Read the original article Hit count: 193

Filed under:

I m not able to connect my android device to other device(either android or non-android)via bluetooth.After detecting the devices from my android phone,i m not able to connect it to selected device from the list.The main problem is it not showing connectivity conformation message in selected device from list.How can i recover from this problem. please help me.Thanks

My code for searching device is:(BluetoothSearchActivity.java)

public class BluetoothSearchActivity extends Activity {

ArrayAdapter<String> btArrayAdapter;
BluetoothAdapter mBluetoothAdapter;
TextView stateBluetooth;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ImageView BluetoothSearchImageView=new ImageView(this);
    BluetoothSearchImageView.setImageResource(R.drawable.inner1);

    setContentView(BluetoothSearchImageView);
    setContentView(R.layout.activity_bluetooth_search);

    mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

    ListView listDevicesFound=(ListView) findViewById(R.id.myList);

    btArrayAdapter=new ArrayAdapter<String> (BluetoothSearchActivity.this,android.R.layout.simple_list_item_1);
    listDevicesFound.setAdapter(btArrayAdapter);

    registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));

     btArrayAdapter.clear();  
     mBluetoothAdapter.startDiscovery();  


    listDevicesFound.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
            Intent i6=new Intent(getApplicationContext(),AcceptThread.class);
            startActivity(i6);
        }
    });
}

private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action=intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            btArrayAdapter.add(device.getName()+"\n"+device.getAddress());
            btArrayAdapter.notifyDataSetChanged();

            Log.d("BluetoothSearchActivity",device.getName()+"\n"+device.getAddress());
        }
    }   
};

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(ActionFoundReceiver);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_bluetooth_search, menu);
    return true;
}

}

and my connectivity code is:(AcceptThread.java)

class ConnectThread extends Thread {

private static final UUID MY_UUID=UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

private static final String ConnectThread = null;

BluetoothSocket mSocket;
BluetoothDevice mDevice;
BluetoothAdapter mBluetoothAdapter;

public ConnectThread(BluetoothDevice device) {
    BluetoothSocket temp=null;
    mDevice=device;

    try{
        temp=mDevice.createRfcommSocketToServiceRecord(MY_UUID);
    }catch(IOException e) { }
    mSocket=temp;
}

public void run() {
    mBluetoothAdapter.cancelDiscovery();

    try{
        Log.i(ConnectThread,"starting to connect");
        mSocket.connect();
    }catch(IOException connectException) { 
        Log.e(ConnectThread,"connection Failed");
        try{
            mSocket.close();
        }catch(IOException closeException){ }
        return;
    }
}

public void cancel() {
    try{
        mSocket.close();
    }catch(IOException e) { }
}

}

public class AcceptThread extends Thread{

private static final String NAME="BluetoothAcceptThread";
private static final UUID MY_UUID=UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

BluetoothServerSocket mServerSocket;
BluetoothAdapter mBluetoothAdapter;

public AcceptThread() {
    BluetoothServerSocket temp=null;

    try{
        temp=mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID);
    }catch(IOException e){ }

    mServerSocket=temp;
}

public void run() {
    BluetoothSocket socket=null;

    while(true) {
        try{
            socket=mServerSocket.accept();
        }catch(IOException e) {
            break;
        }

        if(socket!=null) {
            try {
                mServerSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }
}

public void cancel() {
    try{
        mServerSocket.close();
    }catch(IOException e) { }
}

}

© Stack Overflow or respective owner

Related posts about android