|
- package com.AccelerometerDemo.www;import java.io.IOException;
- import java.io.OutputStream;
- import java.util.List;
- import java.util.UUID;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.widget.Toast;
- public class AccelerometerDemo extends Activity implements SensorEventListener{
- /** Called when the activity is first created. */
- private SensorManager sensorManager;
- private MySurfaceView view;
- private Object[] accelerometer={
- "X轴加速度值:","Y轴加速度值:","Z轴加速度值:",
- };
- private BluetoothAdapter mBluetoothAdapter = null;
- private BluetoothSocket btSocket = null;
- private OutputStream outStream = null;
- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private static String address = "00:11:03:21:00:43"; // <==要连接的蓝牙设备MAC地址
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
- view=new MySurfaceView(this);
- setContentView(view);
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (mBluetoothAdapter == null) {
- Toast.makeText(this, "蓝牙设备不可用,请打开蓝牙!", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- if (!mBluetoothAdapter.isEnabled()) {
- Toast.makeText(this, "请打开蓝牙并重新运行程序!", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- }
- @Override
- public void onStart() {
- super.onStart();
- }
- @Override
- protected void onResume(){
- super.onResume();
- List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
- if(sensors.size()>0){
- sensorManager.registerListener(this, sensors.get(0),SensorManager.SENSOR_DELAY_FASTEST);
- }
- DisplayToast("正在尝试连接智能小车,请稍后····");
- BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
- try {
- btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- DisplayToast("套接字创建失败!");
- }
- DisplayToast("成功连接智能小车!可以开始操控了~~~");
- mBluetoothAdapter.cancelDiscovery();
- try {
- btSocket.connect();
- DisplayToast("连接成功建立,数据连接打开!");
- } catch (IOException e) {
- try {
- btSocket.close();
- } catch (IOException e2) {
- DisplayToast("连接没有建立,无法关闭套接字!");
- }
- }
- }
- @Override
- protected void onPause(){
- super.onPause();
- sensorManager.unregisterListener(this);
- if (outStream != null) {
- try {
- outStream.flush();
- } catch (IOException e) {
- }
- }
- try {
- btSocket.close();
- } catch (IOException e2) {
- DisplayToast("套接字关闭失败!");
- }
- }
- @Override
- public void onStop() {
- super.onStop();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- // TODO Auto-generated method stub
- view.onValueChanged(event.values);
- }
- public void DisplayToast(String str)
- {
- Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
- toast.setGravity(Gravity.TOP, 0, 220);
- toast.show();
- }
- class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
- private float x,y,z;
- public MySurfaceView(Context context){
- super(context);
- getHolder().addCallback(this);
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- x=(getWidth())/2;
- y=(getHeight())/2;
- onValueChanged(new float[3]);
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- @SuppressWarnings("static-access")
- void onValueChanged(float[] values){
- String message;
- byte[] msgBuffer;
- z=(int)(2+values[2]/5);
- x=(getWidth())/2;
- y=(getHeight())/2;
- x-=values[0]*10;
- y+=values[1]*10;
- Canvas canvas=getHolder().lockCanvas();
- if(canvas!=null){
- Paint paint=new Paint();
- Paint paint1=new Paint();
- paint1.setAntiAlias(true);
- paint1.setColor(Color.RED);
- paint1.setTextSize(38);
- paint.setAntiAlias(true);
- paint.setColor(Color.BLUE);
- paint.setTextSize(36);
- canvas.drawColor(Color.WHITE);
- canvas.drawText("BY liuviking", 50, 50, paint1);
- for(int i=0;i<values.length;i++)
- {
- canvas.drawText(accelerometer+":"+(int)values, 0, paint.getTextSize()*(i+1)+90,paint);
- }
- getHolder().unlockCanvasAndPost(canvas);
- }
- int yaxis=(int)values[1];
- int xaxis=(int)values[0];
- int flag=0;
- if(xaxis>=3 && yaxis>2 && yaxis<8)
- {
- flag=2;//xaxis大于3,前进标志为4,车子左转
- }
- else if(xaxis<=-3 && yaxis>2 && yaxis<8)//xaxis小于-3,前进标志为2,车子右转
- {
- flag=4;
- }
- //如果前后方向有信号:
- else if(yaxis>=8 && xaxis>-3 && xaxis<3)
- {
- flag=3;//zaxis大于9,前进标志为1,车子前进
- }
- else if(yaxis<=3 && xaxis>-3 && xaxis<3)
- {
- flag=1;//zaxis小于2,前进标志为3,车子后退
- }
- else
- {
- flag=0;
- }
- switch(flag)
- {
- case 0:
- try {
- outStream = btSocket.getOutputStream();
- } catch (IOException e) {
- }
- message = "0";
- msgBuffer = message.getBytes();
- try {
- outStream.write(msgBuffer);
- } catch (IOException e) {
- }
- break;
- case 1:
- try {
- outStream = btSocket.getOutputStream();
- } catch (IOException e) {
- }
- message = "1";
- msgBuffer = message.getBytes();
- try {
- outStream.write(msgBuffer);
- } catch (IOException e) {
- }
- break;
- case 2:
- try {
- outStream = btSocket.getOutputStream();
- } catch (IOException e) {
- }
- message = "2";
- msgBuffer = message.getBytes();
- try {
- outStream.write(msgBuffer);
- } catch (IOException e) {
- }
- break;
- case 3:
- try {
- outStream = btSocket.getOutputStream();
- } catch (IOException e) {
- }
- message = "3";
- msgBuffer = message.getBytes();
- try {
- outStream.write(msgBuffer);
- } catch (IOException e) {
- }
- break;
- case 4:
- try {
- outStream = btSocket.getOutputStream();
- } catch (IOException e) {
- }
- message = "4";
- msgBuffer = message.getBytes();
- try {
- outStream.write(msgBuffer);
- } catch (IOException e) {
- }
- break;
- }
- }
- }
- }
复制代码
本人第一代的重力感应控制蓝牙小车手机端源代码,比较简单,但是可以作为参考
AccelerometerDemo.rar
(50.55 KB, 下载次数: 425)
|
|