Вы находитесь на странице: 1из 5

package com.thesis.msu.

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;

public class MainActivity extends ActionBarActivity implements SensorEventListen


er{
BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
BluetoothSocket serverSocket;
OutputStream outputStream;
UUID uuid;
Sensor accelerometer;
SensorManager sm;
BluetoothDevice device;
Method m;
float x;
float y;
int z;
int a;
int click = 0;
int clicked = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), "No Bluetooth Detected", Toa
st.LENGTH_SHORT).show();
finish();
} else {
if (!btAdapter.isEnabled()) {

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

serverSocket = (BluetoothSocket) m.invoke(device1,1);


serverSocket.connect();
outputStream = serverSocket.getOutputStream();
String message = x +","+ y + "," + z +"," + a;
outputStream.write(message.getBytes());
click = 0;
if(outputStream != null){
outputStream.flush();
}
outputStream.close();
}else{
Toast.makeText(getApplicationContext(),"Server Unavailable",
Toast.LENGTH_SHORT).show();
}
}
}
}

private void init() {


btAdapter = BluetoothAdapter.getDefaultAdapter();
uuid = UUID.fromString("04c6093b-0000-1000-8000-00805f9b34fb");
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORM
AL);
Button leftClick = (Button)findViewById(R.id.LeftClick);
leftClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click = 1;
}
});
Button upPixel = (Button)findViewById(R.id.UpPixel);
upPixel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click = 4;
}
});
Button downPixel = (Button)findViewById(R.id.DownPixel);
downPixel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click = 5;
}
});
Button leftPixel = (Button)findViewById(R.id.LeftPixel);

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

Button rightClick = (Button)findViewById(R.id.RightClick);


rightClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click = 2;
}
});
}

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

Вам также может понравиться