Note that there are some explanatory texts on larger screens.

plurals
  1. POBluetooth sending data
    text
    copied!<p>Im new at android development. I'm developing an app where first of all, I have a main activity where I must turn On/Off the bluetooth, make it discoverable, search devices and connect to them. </p> <p>After this, I go to other activity where I have a text view with a edittext and a send button, where I can write something and send it. </p> <p>I've got working all the bluetooth enabling/disabling stuff, but now I need to connect to new found devices and then enter to the chat type activity and send. </p> <p>I now this is pretty much work, but it would be great if you can give me an example of how can I do this. This is the code i have:</p> <pre><code>public class BTActivity extends Activity { // Intent request codes private static final int REQUEST_DISCOVERABLE_BT = 0; private static final int REQUEST_CONNECT_DEVICE = 1; private static final int REQUEST_ENABLE_BT = 2; // Debugging private static final String TAG = "BluetoothChat"; private static final boolean D = true; // Name of the connected device public static String mConnectedDeviceName = null; // Array adapter for device list private ArrayAdapter&lt;String&gt; mArrayAdapter; // Local Bluetooth adapter private BluetoothAdapter mBluetoothAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button button1 = (Button) findViewById(R.id.boton1); final Button button2 = (Button) findViewById(R.id.boton2); final Button button3 = (Button) findViewById(R.id.boton3); final Button button4 = (Button) findViewById(R.id.boton4); final Button button5 = (Button) findViewById(R.id.boton5); button5.setOnClickListener(new OnClickListener() { public void onClick(View view) { lanzarComunicacion (null); } }); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); finish(); return; } button3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } }); button2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //LLAMA A LA DIALOG ACTIVITY PARA VISUALIZAR LOS DISPOSITIVOS LanzarBusqueda(null); } }); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (!mBluetoothAdapter.isDiscovering()) { Context context = getApplicationContext(); CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } } }); button4.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mBluetoothAdapter.disable(); Context context = getApplicationContext(); CharSequence text = "TURNING_OFF BLUETOOTH"; int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context, text, 15); toast.show(); } }); } public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CONNECT_DEVICE: if (resultCode == Activity.RESULT_OK) { connectDevice(data); } } } private void connectDevice(Intent data) { String address = data.getExtras().getString(DeviceListDialog.EXTRA_DEVICE_ADDRESS); BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); BTCommunication.mChatService.connect(device); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.bt, menu); return true; } public void lanzarComunicacion (View view) { Intent i = new Intent(this, BTCommunication.class); startActivity(i); } public void LanzarBusqueda (View view) { Intent i = new Intent(this, DeviceListDialog.class); startActivity(i); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload