Android平臺 藍牙串口驅動代碼(部分)-立哥開發

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class ThinBTClient extends Activity implements Runnable {

private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private InputStream InStream = null;
public boolean BluetoothFlag = true;
public int runState = 0;// 0:停止 1:前進 2:後退
public int tansState = 0;
SensorManager mySensorManager; // SensorManager對象引用

Button mButtonF;
Button mButtonB;
Button mButtonL;
Button mButtonR;
Button mButtonS;
Button mButtongotoactivity01;

TextView myText;
SeekBar seekBarPWM;

private static final UUID MY_UUID = UUID
        .fromString("00001101-0000-1000-8000-00805F9B34FB");

private static String address = null;                                    // <==要連接的藍牙設備MAC地址
 
/* 提示選擇一個要連接的服務器 */
/* 跳轉到搜索的藍牙設備列表區,進行選擇 */
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myText = (TextView) findViewById(R.id.myText);
    myText.setText("藍牙還不可用,請稍後...");
    seekBarPWM = (SeekBar) findViewById(R.id.seekBarPWM);
    seekBarPWM.setMax(100);
    seekBarPWM.setProgress(30);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "藍牙設備不可用,請打開藍牙!", Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        DisplayToast("藍牙未打開,程序將自動打開藍牙!");
        mBluetoothAdapter.enable();
    }

    // 設置拖動條改變監聽器
    OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            int data = seekBarPWM.getProgress();
            myText.setText("當前進度:"   data);
            String message;
            byte[] msgBuffer = new byte[5];
            try {
                outStream = btSocket.getOutputStream();

            } catch (IOException e) {
                e.printStackTrace();
            }
            msgBuffer[0] = 0;
            msgBuffer[1] = (byte) (data >> 24 & 0xff);
            msgBuffer[2] = (byte) (data >> 16 & 0xff);
            ;
            msgBuffer[3] = (byte) (data >> 8 & 0xff);
            ;
            msgBuffer[4] = (byte) (data >> 0 & 0xff);
            ;

            try {
                outStream.write(msgBuffer);

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章