Back to project page simple-android-bt.
The source code is released under:
MIT License
If you think the Android project simple-android-bt listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package io.generalpurpose.sandbox; /* w ww.j av a 2 s .c o m*/ import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.telephony.TelephonyManager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import java.io.IOException; import java.util.Set; import java.util.UUID; public class MainActivity extends Activity { private static final String TAG = "SandboxApp"; private final static int REQUEST_ENABLE_BT = 1; public final static int MESSAGE_READ = 2; Button initBtButton; Button connectButton; Button sendDataButton; Spinner pairedDevicesSpinner; ArrayAdapter<BluetoothDevice> pairedDevicesAdapter; BluetoothAdapter mBluetoothAdapter; EditText receivedText; EditText dataText; Toast btUpToast; Toast btDownToast; Handler mHandler; ReadWriteThread rwThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message inputMessage) { Log.d(TAG, String.format( "what = %d, arg1 = %d, arg2 = %d", inputMessage.what, inputMessage.arg1, inputMessage.arg2 )); } }; btUpToast = Toast.makeText(getApplicationContext(), "Bluetooth set up successfully", Toast.LENGTH_LONG); btDownToast = Toast.makeText(getApplicationContext(), "Bluetooth enable canceled", Toast.LENGTH_LONG); receivedText = (EditText)findViewById(R.id.receivedText); sendDataButton = (Button)findViewById(R.id.sendDataButton); initBtButton = (Button)findViewById(R.id.initBtButton); connectButton = (Button)findViewById(R.id.connectButton); dataText = (EditText)findViewById(R.id.dataText); pairedDevicesSpinner = (Spinner)findViewById(R.id.pairedDevicesSpinner); pairedDevicesAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item); pairedDevicesSpinner.setAdapter(pairedDevicesAdapter); receivedText.setEnabled(false); initBtButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast noBtToast = Toast.makeText(getApplicationContext(), "No Bluetooth available", Toast.LENGTH_LONG); noBtToast.show(); } else if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { btUpToast.show(); Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); pairedDevicesAdapter.clear(); // If there are paired devices if (pairedDevices.size() > 0) { // Loop through paired devices for (BluetoothDevice device : pairedDevices) { // Add the name and address to an array adapter to show in a ListView //pairedDevicesAdapter.add(device.getName() + "\n" + device.getAddress()); pairedDevicesAdapter.add(device); } } connectButton.setEnabled(true); connectButton.setClickable(true); } } }); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d(TAG, "connect button clicked!"); BluetoothDevice device = (BluetoothDevice)pairedDevicesSpinner.getSelectedItem(); Log.d(TAG, String.format("selected device has name %s and MAC %s", device.getName(), device.getAddress())); mBluetoothAdapter.cancelDiscovery(); // start connect thread here ConnectThread connectionThread = new ConnectThread( device, new ManageSocketRunnable() { @Override public void manageSocket(BluetoothSocket socket) { rwThread = new ReadWriteThread(socket, new UpdateReceivedRunnable() { @Override public void run(int count, byte[] data) { String decoded = new String(data); Log.d(TAG, String.format("Received text %s", decoded)); } }); rwThread.start(); Log.d(TAG, "rwThread started"); } }); connectionThread.start(); } }); sendDataButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String text = dataText.getText().toString(); Log.d(TAG, "send data button clicked!"); BluetoothDevice device = (BluetoothDevice)pairedDevicesSpinner.getSelectedItem(); Log.d(TAG, String.format("sending data %s", text)); if (rwThread.isAlive()) { rwThread.write(text.getBytes()); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_ENABLE_BT) { Log.d(TAG, String.format("request %d result %d", requestCode, resultCode)); if (resultCode == RESULT_OK) { btUpToast.show(); } else { btDownToast.show(); } } } }