-
-
Save shlokj/f80d0902ad1a366ab03e178164968cfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.justrobotics.jrbluetoothcontrol; | |
import android.app.AlertDialog; | |
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.os.Handler; | |
import android.os.SystemClock; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.InputType; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
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.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.UUID; | |
import me.aflak.bluetooth.Bluetooth; | |
import me.aflak.bluetooth.BluetoothCallback; | |
import me.aflak.bluetooth.DiscoveryCallback; | |
public class MainActivity extends AppCompatActivity { | |
Bluetooth bluetooth; | |
private ArrayAdapter<String> mBTArrayAdapter; | |
String address,name; | |
public void composeEmail(String message) { | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.setType("*/*"); | |
intent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "shlokj@gmail.com" }); | |
intent.putExtra(Intent.EXTRA_SUBJECT, "Would like to get in touch"); | |
intent.putExtra(Intent.EXTRA_TEXT, message); | |
if (intent.resolveActivity(getPackageManager()) != null) { | |
startActivity(intent); | |
} | |
} | |
public void sendEmail () { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setTitle("Send a message: "); | |
final EditText input = new EditText(this); | |
input.setInputType(InputType.TYPE_CLASS_TEXT); | |
builder.setView(input); | |
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
final String Message = input.getText().toString(); | |
composeEmail(Message); | |
} | |
}); | |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.cancel(); | |
} | |
}); | |
builder.show(); | |
} | |
public void displayPairedDevices(){ | |
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); | |
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); | |
for (BluetoothDevice device : pairedDevices) | |
mBTArrayAdapter.add(device.getName() + "\n" + device.getAddress()); | |
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); | |
LayoutInflater inflater = getLayoutInflater(); | |
View convertView = (View) inflater.inflate(R.layout.dialog_btdevices, null); | |
alertDialog.setView(convertView); | |
alertDialog.setTitle("Select your device"); | |
alertDialog.setMessage("A JR Bluetooth device name is of the form JR_X"); | |
ListView devicesListView = (ListView) convertView.findViewById(R.id.mDevicesListView); | |
devicesListView.setAdapter(mBTArrayAdapter); | |
devicesListView.setOnItemClickListener(mDeviceClickListener); | |
alertDialog.show(); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
bluetooth = new Bluetooth(getApplicationContext()); | |
bluetoothOn(); | |
if (bluetooth.isEnabled()) { | |
displayPairedDevices(); | |
} | |
mBTArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); | |
List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>(); | |
if (bluetooth==null){ | |
Toast.makeText(getApplicationContext(),"Bluetooth null",Toast.LENGTH_SHORT).show(); | |
} | |
if (bluetooth!=null){ | |
Toast.makeText(getApplicationContext(),"Bluetooth not null",Toast.LENGTH_SHORT).show(); | |
} | |
Button openController = (Button) findViewById(R.id.open_controller); | |
openController.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent startController = new Intent(MainActivity.this,ControllerActivity.class); | |
startController.putExtra("ADDRESS",address); | |
startActivity(startController); | |
} | |
}); | |
Button openAccelController = (Button) findViewById(R.id.open_accel_controller); | |
openAccelController.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent startControllerAccel = new Intent(MainActivity.this,AccelerometerControl.class); | |
startActivity(startControllerAccel); | |
} | |
}); | |
bluetooth.setBluetoothCallback(new BluetoothCallback() { | |
@Override | |
public void onBluetoothTurningOn() { | |
} | |
@Override | |
public void onBluetoothOn() { | |
Toast.makeText(getBaseContext(), "onBluetoothOn", Toast.LENGTH_SHORT).show(); | |
displayPairedDevices(); | |
} | |
@Override | |
public void onBluetoothTurningOff() { | |
} | |
@Override | |
public void onBluetoothOff() { | |
} | |
@Override | |
public void onUserDeniedActivation() { | |
} | |
}); | |
bluetooth.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) {} | |
}); | |
} | |
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() { | |
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) { | |
String connectStatus=""; | |
if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) { | |
Toast.makeText(getBaseContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
String info = ((TextView) v).getText().toString(); | |
address = info.substring(info.length() - 17); | |
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show(); | |
name = info.substring(0, info.length() - 17); | |
if (bluetooth.isConnected()){ | |
connectStatus="Connected"; | |
} | |
if (!bluetooth.isConnected()){ | |
connectStatus="Not connected"; | |
} | |
// Toast.makeText(getBaseContext(), connectStatus, Toast.LENGTH_SHORT).show(); | |
// bluetooth.connectToAddress(address); | |
// Toast.makeText(getBaseContext(), "Connected (hopefully)", Toast.LENGTH_SHORT).show(); | |
// bluetooth.send("test"); | |
// Toast.makeText(getBaseContext(), "Sent data (hopefully)", Toast.LENGTH_SHORT).show(); | |
}}; | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
bluetooth.onStart(); | |
bluetooth.enable(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
bluetooth.onStop(); | |
} | |
private void bluetoothOn(){ | |
if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) { | |
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); | |
startActivityForResult(enableBtIntent, 1); | |
Toast.makeText(getApplicationContext(),"Bluetooth turned on",Toast.LENGTH_SHORT).show(); | |
} | |
else{ | |
bluetooth = new Bluetooth(getApplicationContext()); | |
mBTArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); | |
displayPairedDevices(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if(requestCode == 1){ | |
Log.d("Working:", "Bluetooth enabled"); | |
Toast.makeText(this, "Bluetooth enabled", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment