package de.tudarmstadt.informatik.hostage.sync.bluetooth; import java.util.UUID; import android.util.Log; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import de.tudarmstadt.informatik.hostage.R; public class BluetoothSync extends Activity{ public static final int CONNECTING = 0x0; public static final int CONNECTION_ESTABLISHED = 0x1; public static final int CONNECTION_FAILED = 0x2; public static final int SYNC_SUCCESSFUL = 0x3; public static final int SYNC_FAILED = 0x4; public static UUID serviceUUID; private BluetoothAdapter mBluetoothAdapter; private ArrayAdapter arrayAdapter; private ServerThread serverThread; private ClientThread clientThread; private CommunicationThread commThread; private TextView mInfoText; private ListView listView; private LinearLayout layout; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth); serviceUUID = UUID.fromString(getResources().getString(R.string.UUID)); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); arrayAdapter = new ArrayAdapter(this, R.layout.list_view_bluetooth_devices); setLayoutElement(); registerBroadcastReceiver(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth mInfoText.setText("Bluetooth is not available on this device."); } else if (!mBluetoothAdapter.isEnabled()) { mInfoText.setText("Enable Bluetooth before synchronizing."); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(enableBtIntent); } else { startConnectionListener(); chooseDevice(); } } @Override public void onDestroy(){ super.onDestroy(); if(mRecieverRegistered){ unregisterBroadcastReceiver(); } if(commThread != null) { commThread.cancel(); } if(clientThread != null){ clientThread.cancel(); } if(serverThread != null){ serverThread.cancel(); } } private void chooseDevice(){ arrayAdapter.clear(); if (!mBluetoothAdapter.startDiscovery()) return; mInfoText.setText("Choose Device for synchronizing:\n"); layout.addView(listView); setContentView(layout); } private void startConnectionListener() { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); serverThread = new ServerThread(mHandler, getResources().getString(R.string.app_name)); serverThread.start(); } protected void manageConnectedSocket(BluetoothSocket socket) { mBluetoothAdapter.cancelDiscovery(); unregisterBroadcastReceiver(); layout.removeView(listView); String deviceName = socket.getRemoteDevice().getName(); mInfoText.setText("Synchronizing with " + deviceName + "..."); commThread = new CommunicationThread(this, socket, mHandler); commThread.start(); } // Create a BroadcastReceiver for ACTION_FOUND private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView arrayAdapter.add(device.getName() + "\n" + device.getAddress()); arrayAdapter.notifyDataSetChanged(); }else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){ int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); Log.i("BluetoothSync", state + ""); if(state == BluetoothAdapter.STATE_ON){ startConnectionListener(); chooseDevice(); }else if(state == BluetoothAdapter.STATE_OFF || state == BluetoothAdapter.STATE_TURNING_OFF){ mInfoText.setText("Enable Bluetooth before synchronizing."); layout.removeView(listView); } } } }; private boolean mRecieverRegistered = false; // Register the BroadcastReceiver private void registerBroadcastReceiver() { IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter. ACTION_STATE_CHANGED); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy mRecieverRegistered = true; } private void unregisterBroadcastReceiver(){ unregisterReceiver(mReceiver); mRecieverRegistered = false; } private void setLayoutElement(){ mInfoText = (TextView) findViewById(R.id.bluetoothInfoText); layout = (LinearLayout) findViewById(R.id.bluetoothLayout); listView = new ListView(this); listView.setAdapter(arrayAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String deviceInfo = arrayAdapter.getItem(position); String mac = deviceInfo.substring(deviceInfo.indexOf("\n") + 1); String name = deviceInfo.substring(0, deviceInfo.indexOf("\n")); mHandler.obtainMessage(CONNECTING, name).sendToTarget(); clientThread = new ClientThread(mBluetoothAdapter.getRemoteDevice(mac), mHandler); clientThread.start(); } }); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch(msg.what){ case CONNECTING: layout.removeView(listView); mInfoText.setText("Connecting to " + (String)msg.obj + "!"); break; case CONNECTION_ESTABLISHED: BluetoothSocket socket = (BluetoothSocket) msg.obj; manageConnectedSocket(socket); break; case CONNECTION_FAILED: mInfoText.setText("Failed to connect to device!"); break; case SYNC_SUCCESSFUL: mInfoText.setText("Synchronization successfull!"); break; case SYNC_FAILED: mInfoText.setText("Synchronization failed!"); break; } } }; }