Android Open Source - Android-To-PC-Keyboard Main Activity






From Project

Back to project page Android-To-PC-Keyboard.

License

The source code is released under:

MIT License

If you think the Android project Android-To-PC-Keyboard listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rmawong.mobile_keyboard;
//from   w  w  w  .j  av a  2s  . co m
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnItemClickListener{

  ArrayAdapter<String> listAdapter;
  EditText textField;
  ListView listView;
  BluetoothAdapter btAdapter;
  Set<BluetoothDevice> devicesArray;
  ArrayList<String> pairedDevices;
  ArrayList<BluetoothDevice> devices;
  IntentFilter filter;
  BroadcastReceiver receiver;
  public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  protected static final int SUCCESS_CONNECT = 0;
  protected static final int MESSAGE_READ = 1;
  String tag = "debugging";
  ConnectedThread connectedThread = null;
  boolean isSend = false;
  boolean isConnected = false;
  Handler mHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
      super.handleMessage(msg);
      switch(msg.what){
      case SUCCESS_CONNECT:
        connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
        Toast.makeText(getApplicationContext(), "CONNECT", Toast.LENGTH_SHORT).show();
        String s = "Byron is the best!";
        connectedThread.write(s.getBytes());
        Log.i(tag, "connected");
        break;
      case MESSAGE_READ:
        byte[] readBuf =(byte[])msg.obj;
        String string = new String(readBuf);
        Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
        Log.i(tag, "Message is read");
        break;
      }
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
      getSupportFragmentManager().beginTransaction()
      .add(R.id.container, new PlaceholderFragment()).commit();
    }

    textField=(EditText)findViewById(R.id.textField);

    textField.setSelection(textField.getText().length());
    textField.addTextChangedListener(new TextWatcher(){
      public void afterTextChanged(Editable s) {
        if (isSend == false) {
          String string = textField.getText().toString();
          if (isConnected == false) {
            isSend = true;
            textField.setText("0");
            textField.setSelection(textField.getText().length());
            isSend = false;
            Toast.makeText(getApplicationContext(), "Device is not connected to server!", Toast.LENGTH_LONG).show();
            return;
          } else if (string.length() > 1 && Character.isSpaceChar(string.charAt(1)))
            string = string.substring(0,0)+' '+string.substring(1, string.length());
          if (string.length() <= 1) {
            String out = "{BACKSPACE}";
            connectedThread.write(out.getBytes());
          } else if (string.length() > 1) {
            connectedThread.write(string.substring(1).getBytes());
          }
          Log.i(tag, string);
          isSend = true;
          textField.setText("0");
          textField.setSelection(textField.getText().length());
          isSend = false;
        }
      }
      public void beforeTextChanged(CharSequence s, int start, int count, int after){}
      public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

    textField.setOnKeyListener(new View.OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" or "backspace" button
        if (isConnected == false) {
          textField.setText("0");
          Toast.makeText(getApplicationContext(), "Device is not connected to server!", Toast.LENGTH_LONG).show();
          return false;
        }
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
          if (keyCode == KeyEvent.KEYCODE_ENTER) {
            String out = "{ENTER}";
            connectedThread.write(out.getBytes());
            return true;
          } else if (keyCode == KeyEvent.KEYCODE_DEL) {
            String out = "{BACKSPACE}";
            connectedThread.write(out.getBytes());
          }
        }
        return false;
      }
    });

    init();
    if(btAdapter==null){
      Toast.makeText(getApplicationContext(), "No bluetooth detected", Toast.LENGTH_SHORT).show();
      finish();
    } else {
      if(!btAdapter.isEnabled()){
        turnOnBT();
      }

      getPairedDevices();
      startDiscovery();
    }
  }

  private void startDiscovery() {
    // TODO Auto-generated method stub
    btAdapter.cancelDiscovery();
    btAdapter.startDiscovery();
  }

  private void turnOnBT() {
    // TODO Auto-generated method stub
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent, 1);
  }

  private void getPairedDevices() {
    // TODO Auto-generated method stub
    devicesArray = btAdapter.getBondedDevices();
    if(devicesArray.size()>0){
      for(BluetoothDevice device:devicesArray){
        pairedDevices.add(device.getName());
      }
    }
  }
  
  private void init() {
    listView=(ListView)findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    listAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
    listView.setAdapter(listAdapter);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    pairedDevices = new ArrayList<String>();
    filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    devices = new ArrayList<BluetoothDevice>();
    receiver = new BroadcastReceiver(){
      public void onReceive(android.content.Context context, Intent intent) {
        String action = intent.getAction();

        if(BluetoothDevice.ACTION_FOUND.equals(action)){
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          devices.add(device);
          String s = "";
          //run some code
          for(int a = 0; a < pairedDevices.size(); a++){
            if(device.getName().equals(pairedDevices.get(a))) {
              //append
              s = "(Paired)";
              break;
            }
          }

          listAdapter.add(device.getName() +" "+ s +" "+"\n"+device.getAddress());
        }

        else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
          //run some code
        }
        else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){

        }
        else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
          if(btAdapter.getState() == btAdapter.STATE_OFF){
            turnOnBT();
          }
        }
      }
    };

    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(receiver, filter);
    filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
  }

  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    unregisterReceiver(receiver);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_CANCELED) {
      Toast.makeText(getApplicationContext(), "Bluetooth must be enabled to continue", Toast.LENGTH_SHORT).show();
      finish();
    }
  }

  @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);
  }

  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_main, container,
          false);
      return rootView;
    }
  }

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    if(true/*listAdapter.getItem(arg2).contains("Paired")*/){
      BluetoothDevice selectedDevice = devices.get(arg2);
      Toast.makeText(getApplicationContext(), "device is paired", Toast.LENGTH_SHORT).show();
      ConnectThread connect = new ConnectThread(selectedDevice);
      connect.start();
    } else {
      Toast.makeText(getApplicationContext(), "device is not paired", Toast.LENGTH_SHORT).show();
      Intent intentOpenBluetoothSettings = new Intent();
      intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 
      startActivity(intentOpenBluetoothSettings); 
    }
  }

  private class ConnectThread extends Thread {

    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
      // Use a temporary object that is later assigned to mmSocket,
      // because mmSocket is final
      BluetoothSocket tmp = null;
      mmDevice = device;

      // Get a BluetoothSocket to connect with the given BluetoothDevice
      try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
      } catch (IOException e) { }
      mmSocket = tmp;
    }

    public void run() {
      // Cancel discovery because it will slow down the connection
      btAdapter.cancelDiscovery();

      try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
      } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
          mmSocket.close();
        } catch (IOException closeException) { }
        return;
      }

      // Do work to manage the connection (in a separate thread)
      mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
      isConnected = true;
      ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) mmSocket);

    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
      try {
        mmSocket.close();
      } catch (IOException e) { }
    }
  }


  private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
      mmSocket = socket;
      InputStream tmpIn = null;
      OutputStream tmpOut = null;

      // Get the input and output streams, using temp objects because
      // member streams are final
      try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
      } catch (IOException e) { }

      mmInStream = tmpIn;
      mmOutStream = tmpOut;
    }

    public void run() {
      byte[] buffer = new byte[1024];  // buffer store for the stream
      int bytes; // bytes returned from read()

      // Keep listening to the InputStream until an exception occurs
      while (true) {
        try {
          // Read from the InputStream
          buffer = new byte[1024];
          bytes = mmInStream.read(buffer);
          // Send the obtained bytes to the UI activity
          mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
        } catch (IOException e) {
          break;
        }
      }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
      try {
        mmOutStream.write(bytes);
        mmOutStream.flush();
      } catch (IOException e) { }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
      try {
        mmSocket.close();
      } catch (IOException e) { }
    }
  }

}




Java Source Code List

com.rmawong.mobile_keyboard.MainActivity.java