http://download.csdn.net/detail/canyue102/5749027
博主最近在电脑上自建了博客,以后也用那个了,欢迎关注访问,里面也有很多有用资源:
开发环境: ubuntu+eclipse+adt+java
主要程序:
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