You are on page 1of 3

package com.jalc.example.

toggleled2;
import
import
import
import
import

java.io.IOException;
java.io.OutputStream;
java.util.ArrayList;
java.util.Set;
java.util.UUID;

import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.os.AsyncTask;
android.os.Bundle;
android.app.Activity;
android.app.ListActivity;
android.bluetooth.BluetoothAdapter;
android.bluetooth.BluetoothDevice;
android.bluetooth.BluetoothSocket;
android.content.Intent;
android.view.Menu;
android.view.View;
android.view.View.OnClickListener;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.ListView;
android.widget.Toast;

public class MainActivity extends ListActivity {


private
private
private
private
private
thDevice>();
private

Button btToggle;
ArrayAdapter<String> mArrayAdapter;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket btSocket;
ArrayList<BluetoothDevice> btDeviceArray = new ArrayList<Bluetoo
ConnectAsyncTask connectAsyncTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.
simple_list_item_1);
setListAdapter(mArrayAdapter);
// Instance AsyncTask
connectAsyncTask = new ConnectAsyncTask();
//Get Bluettoth Adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Check smartphone support Bluetooth
if(mBluetoothAdapter == null){
//Device does not support Bluetooth
Toast.makeText(getApplicationContext(), "Not support blu
etooth", 5).show();
finish();
}
// Check Bluetooth enabled
if(!mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTI

ON_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
// Queryng paried devices
Set<BluetoothDevice> pariedDevices = mBluetoothAdapter.getBonded
Devices();
if(pariedDevices.size() > 0){
for(BluetoothDevice device : pariedDevices){
mArrayAdapter.add(device.getName() + "\n" + devi
ce.getAddress());
btDeviceArray.add(device);
}
}
btToggle = (Button) findViewById(R.id.btToggle);
btToggle.setOnClickListener(btToggleOnClickListener);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id
) {
BluetoothDevice device = btDeviceArray.get(position);
connectAsyncTask.execute(device);
}
// Click event on Button
private OnClickListener btToggleOnClickListener = new OnClickListener()
{
@Override
public void onClick(View v) {
OutputStream mmOutStream = null;
try {
if(btSocket.isConnected()){
mmOutStream = btSocket.getOutputStream()
;
mmOutStream.write(new String("L").getByt
es());
}
} catch (IOException e) { }
}
};
private class ConnectAsyncTask extends AsyncTask<BluetoothDevice, Intege
r, BluetoothSocket>{
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;

@Override
protected BluetoothSocket doInBackground(BluetoothDevice... devi
ce) {
mmDevice = device[0];
try {
String mmUUID = "00001101-0000-1000-8000-00805F9
B34FB";
mmSocket = mmDevice.createInsecureRfcommSocketTo
ServiceRecord(UUID.fromString(mmUUID));
mmSocket.connect();
} catch (Exception e) { }
return mmSocket;
}
@Override
protected void onPostExecute(BluetoothSocket result) {
btSocket = result;
//Enable Button
btToggle.setEnabled(true);
}