package com.ThinBTClient.www;
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();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Toast.makeText(getApplicationContext(),
// "onStartTrackingTouch",
// Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(getApplicationContext(),
// "onStopTrackingTouch",
// Toast.LENGTH_SHORT).show();
}
};
// 为拖动条绑定监听器
seekBarPWM.setOnSeekBarChangeListener(osbcl);
// 前进
mButtonF = (Button) findViewById(R.id.btnF);
mButtonF.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
runState = 1;
sendCmd("a");
Log.d("Season", "前进");
break;
case MotionEvent.ACTION_UP:
runState = 0;
sendCmd("g");
Log.d("Season", "停止运转");
break;
}
return false;
}
});
// 后退
mButtonB = (Button) findViewById(R.id.btnB);
mButtonB.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
runState = 2;
sendCmd("b");
Log.d("Season", "后退");
break;
case MotionEvent.ACTION_UP:
runState = 0;
sendCmd("g");
Log.d("Season", "停止yuzhaun");
break;
}
return false;
}
});
// 左转
mButtonL = (Button) findViewById(R.id.btnL);
mButtonL.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
sendCmd("c");
break;
case MotionEvent.ACTION_UP:
sendCmd("C");
break;
}
return false;
}
});
// 右转
mButtonR = (Button) findViewById(R.id.btnR);
mButtonR.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
sendCmd("d");
break;
case MotionEvent.ACTION_UP:
sendCmd("D");
break;
}
return false;
}
});
mButtongotoactivity01 = (Button) findViewById(R.id.gotoactivity01);
mButtongotoactivity01.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
Intent enabler = new Intent(ThinBTClient.this,
DiscoveryActivity.class);
startActivity(enabler);
break;
}
return false;
}
});
// 停止
mButtonS = (Button) findViewById(R.id.btnS);
mButtonS.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("Season", "停止");
sendCmd("g");
break;
case MotionEvent.ACTION_UP:
sendCmd("G");
Log.d("Season", "停止");
break;
}
return false;
}
});
mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // 获得SensorManager对象
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Bundle data = msg.getData();
Log.d("Season", "in the thread:age=" + data.getInt("age")
+ " name=" + data.getString("name"));
try {
InStream = btSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] tmp = new byte[5];
try {
InStream.read(tmp, 0, 5);
myText.setText(new String(tmp, 0, 5) + " ");
System.out.println(new String(tmp, 0, 5) + " ");
} catch (IOException e) {
e.printStackTrace();
}
// Thread.sleep(1000);
}
MyHandler(Looper looper) {
super(looper);
}
MyHandler() {
}
}
// 开发实现了SensorEventListener接口的传感器监听器
private SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {// 重写onAccuracyChanged方法,在此为空实现
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
没有合适的资源?快使用搜索试试~ 我知道了~
安卓android 蓝牙串口助手 源代码
共69个文件
class:32个
png:12个
xml:9个
4星 · 超过85%的资源 需积分: 47 1.4k 下载量 124 浏览量
2014-08-16
15:10:03
上传
评论 14
收藏 638KB RAR 举报
温馨提示
安卓android 蓝牙串口助手 源代码 ThinBTClient.rar
资源推荐
资源详情
资源评论
收起资源包目录
android 蓝牙串口助手 源代码 ThinBTClient.rar (69个子文件)
ThinBTClient
.project 848B
project.properties 562B
proguard.cfg 1KB
src
com
ThinBTClient
www
Activity01.java 2KB
SamplesUtils.java 2KB
DiscoveryActivity.java 4KB
ThinBTClient.java 12KB
AndroidManifest.xml 1KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
ic_action_search.png 3KB
menu
activity01.xml 203B
drawable-mdpi
icon.png 3KB
ic_action_search.png 3KB
layout
activity01.xml 1KB
discovery.xml 489B
main.xml 2KB
drawable-xhdpi
ic_action_search.png 3KB
values
colors.xml 1KB
strings.xml 353B
styles.xml 1KB
assets
gen
com
ThinBTClient
www
BuildConfig.java 162B
R.java 3KB
libs
android-support-v4.jar 330KB
default.properties 362B
.classpath 380B
bin
resources.ap_ 17KB
ThinBTClient.apk 155KB
classes
com
ThinBTClient
www
R$layout.class 467B
R$style.class 480B
ThinBTClient$3.class 1KB
ThinBTClient$7.class 1KB
ThinBTClient.class 7KB
SamplesUtils$1.class 1KB
BuildConfig.class 349B
R$drawable.class 699B
R$attr.class 346B
ThinBTClient$4.class 1KB
ThinBTClient$2.class 2KB
DiscoveryActivity.class 6KB
ThinBTClient$8.class 1KB
DiscoveryActivity$5.class 832B
ThinBTClient$5.class 1KB
ThinBTClient$6.class 1KB
SamplesUtils$1$1.class 860B
ThinBTClient$MyHandler.class 3KB
SamplesUtils.class 2KB
Activity01.class 2KB
DiscoveryActivity$1.class 1KB
R$id.class 794B
DiscoveryActivity$3.class 1KB
R.class 693B
R$string.class 554B
R$menu.class 400B
ThinBTClient$MyThread.class 2KB
DiscoveryActivity$2.class 1KB
Activity01$1.class 1KB
DiscoveryActivity$4.class 1KB
R$color.class 507B
ThinBTClient$1.class 2KB
AndroidManifest.xml 1KB
jarlist.cache 119B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
ic_action_search.png 409B
drawable-mdpi
icon.png 2KB
ic_action_search.png 311B
drawable-xhdpi
ic_action_search.png 491B
classes.dex 329KB
共 69 条
- 1
sinat_19203961
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页