package com.example.emrisb.ledcontrol_withbt;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
public static final UUID myUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static final int baglanti = 0;
public static final int smsOku = 1;
OutputStream outputStream;
InputStream inputStream;
ArrayAdapter<String> adapterList;
BluetoothAdapter btAdapter;
ArrayList<String> eslesen;
ArrayList<BluetoothDevice> aygitlar;
IntentFilter filter;
BroadcastReceiver receiver;
Set<BluetoothDevice> deviceArray;
private Button btn_on, btn_off;
private TextView txt_durum;
private ListView liste;
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case baglanti:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
txt_durum.setText("Baglandi.");
break;
case smsOku:
byte[] bufOku = (byte[]) msg.obj;
String str = new String(bufOku);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_durum = (TextView) findViewById(R.id.tDurum);
liste = (ListView) findViewById(R.id.liste);
liste.setOnItemClickListener(this);
adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, 0);
liste.setAdapter(adapterList);
btAdapter = BluetoothAdapter.getDefaultAdapter();
eslesen = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
aygitlar = new ArrayList<BluetoothDevice>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice aygit = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
aygitlar.add(aygit);
String s ="";
for (int i = 0; i < eslesen.size(); i++) {
if (aygit.getName().equals(eslesen.get(i))) {
s = "(eslesti)";
}
}
adapterList.add(aygit.getName() + "" + s + "\n" + aygit.getAddress());
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (btAdapter.getState() == btAdapter.STATE_OFF) {
btAdapter_Ac();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
if (btAdapter == null) {
txt_durum.setText("Bluetooth yok.");
Toast.makeText(getApplicationContext(), "Cihaziniza bluetooth yok", Toast.LENGTH_LONG).show();
} else {
if (btAdapter.isEnabled()) {
btAdapter_Ac();
}
secim();
startDiscovery();
btn_on = (Button) findViewById(R.id.btn_on);
btn_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ledON();
} catch (IOException e) {
}
}
});
btn_off = (Button) findViewById(R.id.btn_off);
btn_off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ledOFF();
} catch (IOException e) {
}
}
});
}
}
private void btAdapter_Ac() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
private void secim() {
deviceArray = btAdapter.getBondedDevices();
if (deviceArray.size() > 0) {
for (BluetoothDevice aygit : deviceArray) {
eslesen.add(aygit.getName());
}
}
}
private void startDiscovery() {
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//BT aygitini secme islemi burada yapilir.
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
if (adapterList.getItem(position).contains("(eslesti)")) {
BluetoothDevice secilen_aygit = aygitlar.get(position);
ConnectThread connect = new ConnectThread(secilen_aygit);
connect.start();
txt_durum.setText("Baglandi!");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth aygitina baglanilamadi", Toast.LENGTH_SHORT).show();
txt_durum.setText("Baglanamadi!");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Bluetooth aygitinizi acmaniz gerekmektedir.", Toast.LENGTH_SHORT).show();
finish();
}
}
public void ledON() throws IOException {
outputStream.write("1" .getBytes());
}
public void ledOFF() throws IOException {
outputStream.write("2" .getBytes());
}
private class ConnectThread extends Thread {
private final BluetoothSocket btSocket;
private final BluetoothDevice btDevice;
public ConnectThread(BluetoothDevice aygit) {
BluetoothSocket tmp = null;
btDevice = aygit;
try {
tmp = aygit.createRfcommSocketToServiceRecord(myUuid);
} catch (IOException e) {
txt_durum.setText("Baglanti yok");
}
btSocket = tmp;
}
@Override
public void run() {
btAdapter.cancelDiscovery();
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e1) {
}
return;
}
mHandler.obtainMessage(baglanti, btSocket).sendToTarget();
}
public void cancel() {
try {
btSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket btSocket1;
public ConnectedThread(BluetoothSocket socket) {
btSocket1 = socket;
InputStream tmpIN = null;
OutputStream tmpOUT = null;
try {
tmpIN = socket.getInputStream();
tmpOUT = socket.getOutputStream();
} catch (IOException e) {
}
inputStream = tmpIN;
outputStream = tmpOUT;
}
public void run() {
byte[] buffer;
int bytes;
while (true) {
try {
buffer = new byte[1024];
bytes = inputStream.read(buffer);
} catch (IOException e) {
break;
}
}
}
public void cancel() {
try {
btSocket1.close();
} catch (IOException e) {
}
}
}
}