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

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值