Skip to content

Instantly share code, notes, and snippets.

@shlokj
Last active April 11, 2019 14:48
Show Gist options
  • Save shlokj/12c4e2c62ca0f5284c5c3c041775654f to your computer and use it in GitHub Desktop.
Save shlokj/12c4e2c62ca0f5284c5c3c041775654f to your computer and use it in GitHub Desktop.
package in.justrobotics.jrbluetoothcontrol;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.drm.DrmStore;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;
import me.aflak.bluetooth.Bluetooth;
import me.aflak.bluetooth.BluetoothCallback;
import me.aflak.bluetooth.DeviceCallback;
import me.aflak.bluetooth.DiscoveryCallback;
public class ControllerActivity extends AppCompatActivity {
private ListView mDevicesListView;
private ImageView Up, Down, Left, Right;
private int commandFromForward, commandFromReverse, commandFromLeft, commandFromRight;
private final String TAG = ControllerActivity.class.getSimpleName();
private Handler mHandler;
private BluetoothSocket mBTSocket = null;
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // "random" unique identifier
private final static int REQUEST_ENABLE_BT = 1; // used to identify adding bluetooth names
private final static int MESSAGE_READ = 2; // used in bluetooth handler to identify message update
private final static int CONNECTING_STATUS = 3; // used in bluetooth handler to identify message status
Bluetooth bluetoothObject;
String address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controller);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
bluetoothObject = new Bluetooth(getApplicationContext());
bluetoothObject.onStart();
bluetoothObject.enable();
bluetoothObject.setBluetoothCallback(new BluetoothCallback() {
@Override
public void onBluetoothTurningOn() {
}
@Override
public void onBluetoothOn() {
}
@Override
public void onBluetoothTurningOff() {
}
@Override
public void onBluetoothOff() {
}
@Override
public void onUserDeniedActivation() {
}
});
bluetoothObject.setDiscoveryCallback(new DiscoveryCallback() {
@Override
public void onDiscoveryStarted() {
}
@Override
public void onDiscoveryFinished() {
}
@Override
public void onDeviceFound(BluetoothDevice device) {
}
@Override
public void onDevicePaired(BluetoothDevice device) {
}
@Override
public void onDeviceUnpaired(BluetoothDevice device) {
}
@Override
public void onError(String message) {
}
});
bluetoothObject.setDeviceCallback(new DeviceCallback() {
@Override
public void onDeviceConnected(BluetoothDevice device) {
bluetoothObject.send("onDeviceConnected\n");
Log.d(TAG, "onDeviceConnected: Sent data maybe");
// Toast.makeText(getApplicationContext(), "Sent data maybe", Toast.LENGTH_SHORT).show();
}
@Override
public void onDeviceDisconnected(BluetoothDevice device, String message) {
}
@Override
public void onMessage(String message) {
}
@Override
public void onError(String message) {
}
@Override
public void onConnectError(BluetoothDevice device, String message) {
}
});
Up = (ImageView) findViewById(R.id.upfwd);
Down = (ImageView) findViewById(R.id.downbk);
Left = (ImageView) findViewById(R.id.leftlt);
Right = (ImageView) findViewById(R.id.rightrt);
commandFromForward = 0;
commandFromReverse = 0;
commandFromLeft = 0;
commandFromRight = 0;
address=getIntent().getExtras().getString("ADDRESS");
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_SHORT).show();
if (bluetoothObject!=null && address!=null){
Toast.makeText(getApplicationContext(), "Both addr and bt not null, proceeding to connect", Toast.LENGTH_SHORT).show();
bluetoothObject.connectToAddress(address);
// bluetoothObject.send("Hello\n");
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
Up.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (bluetoothObject.isConnected()){
Toast.makeText(getApplicationContext(), "Attempting to send data", Toast.LENGTH_SHORT).show();
bluetoothObject.send("upClicked");
Toast.makeText(getApplicationContext(), "Probably sent data!!", Toast.LENGTH_SHORT).show();
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
commandFromForward = 0;
}
return false;
}
});
Down.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
commandFromReverse = 2;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
commandFromReverse = 0;
}
return false;
}
});
Left.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
commandFromLeft = 4;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
commandFromLeft = 0;
}
return false;
}
});
Right.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
commandFromRight = 8;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
commandFromRight = 0;
}
return false;
}
});
}
/* final Handler ha=new Handler();
ha.postDelayed(new Runnable() {
@Override
public void run() {
if (mConnectedThread != null) {
sendCumulativeCommand(commandFromForward,commandFromReverse,commandFromLeft,commandFromRight);
}
ha.postDelayed(this, 100);
}
}, 100);
}*/
public void sendCumulativeCommand ( int fd, int bk, int rt, int lt){
int cumulativeCommand = fd + bk + rt + lt;
}
@Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.stdc_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
int id = item.getItemId();
switch (id) {
case R.id.howtouse_controller:
displayAboutStandardController();
break;
case android.R.id.home:
this.finish();
break;
}
return true;
}
public void displayAboutStandardController () {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("About the standard controller");
builder.setMessage(R.string.about_stdc);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment