android蓝牙单片机显示波形Word下载.docx

上传人:b****6 文档编号:21005888 上传时间:2023-01-26 格式:DOCX 页数:16 大小:203KB
下载 相关 举报
android蓝牙单片机显示波形Word下载.docx_第1页
第1页 / 共16页
android蓝牙单片机显示波形Word下载.docx_第2页
第2页 / 共16页
android蓝牙单片机显示波形Word下载.docx_第3页
第3页 / 共16页
android蓝牙单片机显示波形Word下载.docx_第4页
第4页 / 共16页
android蓝牙单片机显示波形Word下载.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

android蓝牙单片机显示波形Word下载.docx

《android蓝牙单片机显示波形Word下载.docx》由会员分享,可在线阅读,更多相关《android蓝牙单片机显示波形Word下载.docx(16页珍藏版)》请在冰豆网上搜索。

android蓝牙单片机显示波形Word下载.docx

?

xmlversion="

1.0"

encoding="

utf-8"

>

LinearLayoutxmlns:

android="

"

android:

layout_width="

fill_parent"

layout_height="

orientation="

vertical"

>

TextView

wrap_content"

text="

@string/hello"

LinearLayout

android:

horizontal"

Button

id="

@+id/button"

@string/button"

<

@+id/sin"

@string/sin"

Button

android:

@+id/clear"

@string/clear"

/LinearLayout>

SurfaceView

@+id/show"

TextView

@+id/myview"

@string/myview"

/>

在strings.xml:

resources>

stringname="

hello"

HelloWorld,OscilloscopeActivity!

/string>

app_name"

Oscilloscope<

button"

开启蓝牙<

myview"

参数显示:

sin"

sin<

buttonstart"

搜索<

buttonselect"

选择设备<

buttonsend"

发送<

input"

pleaseinput<

clear"

清除<

/resources>

在Oscilloscope.java

packagecom.ways;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.util.Iterator;

importjava.util.Set;

importjava.util.Timer;

importjava.util.TimerTask;

importjava.util.UUID;

importandroid.app.Activity;

importandroid.bluetooth.BluetoothAdapter;

importandroid.bluetooth.BluetoothDevice;

importandroid.bluetooth.BluetoothSocket;

importandroid.content.Intent;

importandroid.graphics.Canvas;

importandroid.graphics.Color;

importandroid.graphics.Paint;

importandroid.graphics.Rect;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.view.Menu;

importandroid.view.MenuItem;

importandroid.view.SurfaceHolder;

importandroid.view.SurfaceView;

importandroid.view.View;

importandroid.view.SurfaceHolder.Callback;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.TextView;

publicclassOscilloscopeextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

finalintHEIGHT=320;

//设置画图范围高度

finalintWIDTH=450;

//画图范围宽度

finalintX_OFFSET=5;

//x轴(原点)起始位置偏移画图范围一点

privateintcx=X_OFFSET;

//实时x的坐标

intcenterY=HEIGHT/2;

//y轴的位置

TextViewmyview=null;

//画布下方显示获取数据的地方

finalUUIDuuid=UUID.fromString("

00001101-0000-1000-8000-00805F9B34FB"

);

//uuid此为单片机蓝牙模块用

//还有其他的uuid,这个可以再XX查到,暂不清楚其中的差别

finalBluetoothAdaptermBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

//获取本手机的蓝牙适配器

staticintREQUEST_ENABLE_BT=1;

//一个常量而已,开启蓝牙时使用

BluetoothSocketsocket=null;

//用于数据传输的socket

intREAD=1;

//一个常量,用于传输数据消息队列的识别字

publicConnectedThreadthread=null;

//连接蓝牙设备线程

staticinttemp=0;

//临时变量用于保存接收到的数据

privateSurfaceHolderholder=null;

//画图使用,可以控制一个SurfaceView

privatePaintpaint=null;

//画笔

SurfaceViewsurface=null;

//

Timertimer=newTimer();

//一个时间控制的对象,用于控制实时的x的坐标,

//使其递增,类似于示波器从前到后扫描

TimerTasktask=null;

//时间控制对象的一个任务

/*关于画图类的几点说明

*SurfaceView是View的继承类,这个视图里

*内嵌了一个专门用于绘制的Surface。

可以控制这个Surface的格式和尺寸。

*SurfaceView控制这个Surface的绘制位置。

*

*实现过程:

继承SurfaceView并实现SurfaceHolder.Callback接口------>

*SurfaceView.getHolder()获得SurfaceHolder对象----->

SurfaceHolder.addCallback(callback)

*添加回调函数----->

surfaceHolder.lockCanvas()获得Canvas对象并锁定画布------>

*Canvas绘画------->

SurfaceHolder.unlockCanvasAndPost(Canvascanvas)结束锁定画图,

*并提交改变,将图形显示。

*这里用到了一个类SurfaceHolder,可以把它当成surface的控制器,

*用来操纵surface。

处理它的Canvas上画的效果和动画,控制表面,大小,像素等

*其中有几个常用的方法,锁定画布,结束锁定画布

**/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myview=(TextView)findViewById(R.id.myview);

//获取控件对象

Buttonbluetooth=(Button)findViewById(R.id.button);

Buttonsin=(Button)findViewById(R.id.sin);

Buttonclear=(Button)findViewById(R.id.clear);

surface=(SurfaceView)findViewById(R.id.show);

//初始化SurfaceHolder对象

holder=surface.getHolder();

holder.setFixedSize(WIDTH+50,HEIGHT+100);

//设置画布大小,要比实际的绘图位置大一点

paint=newPaint();

paint.setColor(Color.GREEN);

//画波形的颜色是绿色的,区别于坐标轴黑色

paint.setStrokeWidth(3);

bluetooth.setOnClickListener(newMyButtonListener());

//添加按钮监听器开启蓝牙开启连接通信线程

clear.setOnClickListener(newMyButtonClearListener());

//添加按钮监听器清除TextView内容

holder.addCallback(newCallback(){//按照上面注释,添加回调函数

publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){

drawBack(holder);

//如果没有这句话,会使得在开始运行程序,整个屏幕没有白色的画布出现

//直到按下按键,因为在按键中有对drawBack(SurfaceHolderholder)的调用

}

publicvoidsurfaceCreated(SurfaceHolderholder){

//TODOAuto-generatedmethodstub

publicvoidsurfaceDestroyed(SurfaceHolderholder){

}

});

//添加按钮监听器开启画图线程

sin.setOnClickListener(newOnClickListener(){

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

newDrawThread().start();

//线程启动

}

classMyButtonListenerimplementsOnClickListener{

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

//如果没有打开蓝牙,此时打开蓝牙

if(!

mBluetoothAdapter.isEnabled()){

IntentenableBtIntent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);

//此处我已经知道我对应的蓝牙模块的地址,所以省去扫描,配对的过程,

//如果从头开始的话,需要添加很多内容。

/*

*这是获得在手机中已经存储的已经配对过的蓝牙信息

*Set<

BluetoothDevice>

devices=mBluetoothAdapter.getBondedDevices();

if(devices.size()>

0){

for(Iteratoriterator=devices.iterator();

iterator.hasNext();

){

BluetoothDevicebluetoothdevice=(BluetoothDevice)iterator.next();

System.out.println(bluetoothdevice.getAddress());

myview.append("

\n"

+bluetoothdevice.getAddress());

//这里就是获取地址当然这里又不止一个信息,所有配对的信息都有,

*你要选择蓝牙模块的地址,其实地址只是一个说法,它实际上是一个虚拟通道,

*我也不太清楚,就称之为地址了

}

}*/

BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice("

20:

13:

03:

18:

10:

09"

try{

socket=device.createRfcommSocketToServiceRecord(uuid);

//建立连接

BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

//取消搜索蓝牙设备,不写也罢,因为我压根没搜索

socket.connect();

//建立连接,如果连接成功,此时蓝牙模块中的显示当前连接状态的指示灯就不会闪烁了,

//此时意味着连接成功了

}catch(IOExceptione){

e.printStackTrace();

thread=newConnectedThread(socket);

//开启通信的线程

thread.start();

}

classMyButtonClearListenerimplementsOnClickListener{

myview.setText("

Handlerhandler=newHandler(){//这是处理消息队列的Handler对象

@Override

publicvoidhandleMessage(Messagemsg){

//处理消息

if(msg.what==READ){

Stringstr=(String)msg.obj;

//类型转化

myview.append("

"

+str);

//显示在画布下方的TextView中

super.handleMessage(msg);

};

/*

*关于此类,因为我的手机端只需要从蓝牙读取数据,不需要发送,所以就不必实现输出流

*其实相比起来,输出流更简单

privateclassConnectedThreadextendsThread{

privatefinalBluetoothSocketmmSocket;

privatefinalInputStreammmInStream;

privatefinalOutputStreammmOutStream;

//构造函数

publicConnectedThread(BluetoothSocketsocket){

mmSocket=socket;

InputStreamtmpIn=null;

OutputStreamtmpOut=null;

//Gettheinputandoutputstreams,usingtempobjectsbecause

//memberstreamsarefinal

try{

tmpIn=socket.getInputStream();

//获取输入流

tmpOut=socket.getOutputStream();

//获取输出流

}catch(IOExceptione){}

mmInStream=tmpIn;

mmOutStream=tmpOut;

publicvoidrun(){

byte[]buffer=newbyte[1024];

//bufferstoreforthestream

intbytes;

//bytesreturnedfromread()

//KeeplisteningtotheInputStreamuntilanexceptionoccurs

while(true){

try{

//ReadfromtheInputStream

bytes=mmInStream.read(buffer);

//bytes数组返回值,为buffer数组的长度

//SendtheobtainedbytestotheUIactivity

Stringstr=newString(buffer);

temp=byteToInt(buffer);

//用一个函数实现类型转化,从byte到int

handler.obtainMessage(READ,bytes,-1,str)

.sendToTarget();

//压入消息队列

}catch(Exceptione){

System.ou

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1