Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
bluetoothconnection;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
android.bluetooth.BluetoothAdapter;
android.bluetooth.BluetoothDevice;
android.bluetooth.BluetoothSocket;
android.content.Intent;
android.hardware.Sensor;
android.hardware.SensorEvent;
android.hardware.SensorEventListener;
android.hardware.SensorManager;
android.os.Bundle;
android.support.v7.app.ActionBarActivity;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.widget.Button;
android.widget.RadioGroup;
android.widget.Toast;
android.widget.ToggleButton;
import
import
import
import
import
import
java.io.IOException;
java.io.OutputStream;
java.lang.reflect.InvocationTargetException;
java.lang.reflect.Method;
java.util.Set;
java.util.UUID;
turnOnBT();
}
}
}
public void onToggleButton(View v){
boolean checked = ((ToggleButton)v).isChecked();
if(checked){
clicked=3;
}else
clicked=0;
}
@Override
protected void onResume() {
super.onResume();
try {
getPairedDevices();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private void turnOnBT() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
private void getPairedDevices() throws IOException, NoSuchMethodException, I
nvocationTargetException, IllegalAccessException {
devicesArray = btAdapter.getBondedDevices();
if(devicesArray.size() > 0){
for(BluetoothDevice device1 : devicesArray){
//pairedDevices.add(device.getName());
if(device1.getName().toString().equals("OVERHEAD-PC")){
device = device1;
m = device1.getClass().getMethod("createRfcommSocket",new Cl
ass[]{int.class});
leftPixel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click = 6;
}
});
Button rightPixel = (Button)findViewById(R.id.RightPixel);
rightPixel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click = 7;
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data
) {
super.onActivityResult(requestCode, resultCode, data);
// if(resultCode == RESULT_CANCELED){
//Toast.makeText(getApplicationContext(),"Bluetooth must be enabled"
,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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent event) {
x
y
z
a
=
=
=
=
event.values[0];
event.values[1];
click;
clicked;
try {
getPairedDevices();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}