开源个安卓程序:蓝牙遥控智能车程序-单片机控制安卓上位机

    大二寒假学做蓝牙遥控智能车,也就学了安卓编程,写了这个安卓遥控程序。这是第一版本,用按钮实现控制的,等我把重力感应控制的那个程序做稳定了再开源。
    上个图:
           


    主要参考安卓SDK自带的开源例程,所以觉得还是有必要开源代码的,不想自己建工程可以直接下载apk包:
    ishare.iask.sina.com.cn/f/37552296.html
http://download.csdn.net/detail/canyue102/5749027
博主最近在电脑上自建了博客,以后也用那个了,欢迎关注访问,里面也有很多有用资源:

http://www.embbnux.com


开发环境: ubuntu+eclipse+adt+java
主要程序:
JAVA程序:
 
  一、BluetoothChat.java

 

package com.dongyubtcar.www;

import java.io.IOException;
import java.io.OutputStream;

import android.app.ActionBar;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Menu;
//import android.view.View;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
importandroid.provider.ContactsContract.CommonDataKinds.Event;
import android.content.DialogInterface;
import com.dongyubtcar.www.R;
//import android.content.DialogInterface.OnClickListener;

public class BluetoothChat extends Activity {
    //Debugging
    privatestatic final String TAG = "BluetoothChat";
    privatestatic final boolean D = true;

    // Messagetypes sent from the BluetoothChatService Handler
    publicstatic final int MESSAGE_STATE_CHANGE = 1;
    publicstatic final int MESSAGE_READ = 2;
    publicstatic final int MESSAGE_WRITE = 3;
    publicstatic final int MESSAGE_DEVICE_NAME = 4;
    publicstatic final int MESSAGE_TOAST = 5;

    // Key namesreceived from the BluetoothChatService Handler
    publicstatic final String DEVICE_NAME = "device_name";
    publicstatic final String TOAST = "toast";

    // Intentrequest codes
    privatestatic final int REQUEST_CONNECT_DEVICE_SECURE = 1;
    privatestatic final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
    privatestatic final int REQUEST_ENABLE_BT = 3;
    privateOutputStream outStream = null;
    privateBluetoothSocket btSocket = null;
    // LayoutViews
   // private ListViewmConversationView;
    privateEditText mOutEditText;
    //privateButton mSendButton;
    ButtonmButton1;
    ButtonmButton2;
    ButtonmButton3;
    ButtonmButton4;
    ButtonmButton5;
    ButtonmButton6;
    ButtonmButton7;

    // Name ofthe connected device
    privateString mConnectedDeviceName = null;
    // Arrayadapter for the conversation thread
    //privateArrayAdapter<String>mConversationArrayAdapter;
    // Stringbuffer for outgoing messages
    privateStringBuffer mOutStringBuffer;
    // LocalBluetooth adapter
    privateBluetoothAdapter mBluetoothAdapter = null;
    // Memberobject for the chat services
    privateBluetoothChatService mChatService = null;


   @Override
    public voidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       if(D) Log.e(TAG, "+++ ON CREATE +++");

       // Set up the window layout
       setContentView(R.layout.main);

       // Get local Bluetooth adapter
       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

       // If the adapter is null, then Bluetooth is not supported
       if (mBluetoothAdapter == null) {
           Toast.makeText(this, "Bluetooth is not available",Toast.LENGTH_LONG).show();
           finish();
           return;
       }
    }

   @Override
    public voidonStart() {
       super.onStart();
       if(D) Log.e(TAG, "++ ON START ++");

       // If BT is not on, request that it be enabled.
       // setupChat() will then be called during onActivityResult
       if (!mBluetoothAdapter.isEnabled()) {
           Intent enableIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
           startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
       // Otherwise, setup the chat session
       } else {
           if (mChatService == null) setupChat();
       }
       //setupButtons();
    }

   @Override
    publicsynchronized void onResume() {
       super.onResume();
       if(D) Log.e(TAG, "+ ON RESUME +");

       // Performing this check in onResume() covers the case in which BTwas
       // not enabled during onStart(), so we were paused to enableit...
       // onResume() will be called when ACTION_REQUEST_ENABLE activityreturns.
       if (mChatService != null) {
           // Only if the state is STATE_NONE, do we know that we haven'tstarted already
           if (mChatService.getState() == BluetoothChatService.STATE_NONE){
             // Start the Bluetooth chat services
             mChatService.start();
           }
       }
    }

    private voidsetupChat() {
       Log.d(TAG, "setupChat()");

       // Initialize the array adapter for the conversation thread
       //mConversationArrayAdapter = newArrayAdapter<String>(this,R.layout.main);
       //mConversationView = (ListView) findViewById(R.id.in);
       //mConversationView.setAdapter(mConversationArrayAdapter);

       // Initialize the compose field with a listener for the returnkey
       //mOutEditText = (EditText) findViewById(R.id.edit_text_out);
       //mOutEditText.setOnEditorActionListener(mWriteListener);

       // Initialize the send button with a listener that for clickevents
       //mSendButton = (Button) findViewById(R.id.button_send);
       
       // Initialize the BluetoothChatService to perform bluetoothconnections
       
       mButton1=(Button) findViewById(R.id.stop);
       mButton1.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "0";
               sendMessage(message);
               message = "0";
               sendMessage(message);
           }
       });
       
       mButton2=(Button) findViewById(R.id.forward);
       mButton2.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "2";
               sendMessage(message);
               message = "2";
               sendMessage(message);
           }
       });
       mButton3=(Button) findViewById(R.id.back);
       mButton3.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "8";
               sendMessage(message);
               message = "8";
               sendMessage(message);
           }
       });
       mButton4=(Button) findViewById(R.id.left);
       mButton4.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "4";
               sendMessage(message);
               message = "4";
               sendMessage(message);
           }
       });
       mButton5=(Button) findViewById(R.id.right);
       mButton5.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "6";
               sendMessage(message);
               message = "6";
               sendMessage(message);
           }
       });
       mButton6=(Button) findViewById(R.id.led);
       mButton6.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "L";
               sendMessage(message);
               message = "L";
               sendMessage(message);
           }
       });
       mButton7=(Button) findViewById(R.id.sound);
       mButton7.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // Send a message using content of the edit text widget
               //TextView view = (TextView)findViewById(R.id.edit_text_out);
               //String message = view.getText().toString();
               Stringmessage = "S";
               sendMessage(message);
               message = "S";
               sendMessage(message);
           }
       });
       mChatService = new BluetoothChatService(this, mHandler);

       // Initialize the buffer for outgoing messages
       mOutStringBuffer = new StringBuffer("");
    }
   
   @Override
    publicsynchronized void onPause
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值