ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:203KB ,
资源ID:21005888      下载积分:2 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/21005888.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(android蓝牙单片机显示波形Word下载.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

1、?xml version=1.0 encoding=utf-8LinearLayout xmlns:android= android:layout_width=fill_parentlayout_height=orientation=vertical TextViewwrap_contenttext=string/helloLinearLayout android:horizontalButton id=+id/buttonstring/button SurfaceView +id/showTextView +id/myviewstring/myview /在strings.xml:resou

2、rcesstring name=helloHello World, OscilloscopeActivity!/stringapp_nameOscilloscopebutton开启蓝牙myview参数显示:sinsinbuttonstart搜索buttonselect选择设备buttonsend发送inputplease inputclear清除在Oscilloscope.javapackage com.ways;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import ja

3、va.util.Iterator;import java.util.Set;import java.util.Timer;import java.util.TimerTask;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 a

4、ndroid.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.MenuItem;import android.view.SurfaceHolder;import android.view.Sur

5、faceView;import android.view.View;import android.view.SurfaceHolder.Callback;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class Oscilloscope extends Activity /* Called when the activity is first created. */ final int HEIGHT=320; /设置画图范围高

6、度 final int WIDTH=450; /画图范围宽度 final int X_OFFSET = 5; /x轴(原点)起始位置偏移画图范围一点 private int cx = X_OFFSET; /实时x的坐标 int centerY = HEIGHT /2; /y轴的位置 TextView myview = null; /画布下方显示获取数据的地方 final UUID uuid = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB); /uuid 此为单片机蓝牙模块用 /还有其他的uuid,这个可以再XX查到,暂不清楚其中的差

7、别 final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); /获取本手机的蓝牙适配器 static int REQUEST_ENABLE_BT = 1; / 一个常量而已,开启蓝牙时使用 BluetoothSocket socket = null; /用于数据传输的socket int READ = 1; /一个常量,用于传输数据消息队列的识别字 public ConnectedThread thread = null; /连接蓝牙设备线程 static int temp = 0; /临时

8、变量用于保存接收到的数据 private SurfaceHolder holder = null; /画图使用,可以控制一个SurfaceView private Paint paint = null; /画笔 SurfaceView surface = null; / Timer timer = new Timer(); /一个时间控制的对象,用于控制实时的x的坐标, /使其递增,类似于示波器从前到后扫描 TimerTask task = null; /时间控制对象的一个任务 /* 关于画图类的几点说明 * SurfaceView 是View的继承类,这个视图里 * 内嵌了一个专门用于绘制的

9、Surface。可以控制这个Surface的格式和尺寸。 * SurfaceView控制这个Surface的绘制位置。 * * 实现过程:继承SurfaceView并实现SurfaceHolder.Callback接口- * SurfaceView.getHolder()获得SurfaceHolder对象-SurfaceHolder.addCallback(callback) * 添加回调函数-surfaceHolder.lockCanvas()获得Canvas对象并锁定画布- * Canvas绘画-SurfaceHolder.unlockCanvasAndPost(Canvas canvas

10、)结束锁定画图, * 并提交改变,将图形显示。 * 这里用到了一个类SurfaceHolder,可以把它当成surface的控制器, * 用来操纵surface。处理它的Canvas上画的效果和动画,控制表面,大小,像素等 * 其中有几个常用的方法,锁定画布,结束锁定画布 * */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); myview = (TextView)findViewById(R.

11、id.myview); /获取控件对象 Button bluetooth =(Button)findViewById(R.id.button); Button sin =(Button)findViewById(R.id.sin); Button clear =(Button)findViewById(R.id.clear); surface = (SurfaceView)findViewById(R.id.show); /初始化SurfaceHolder对象 holder = surface.getHolder(); holder.setFixedSize(WIDTH+50, HEIGHT+

12、100); /设置画布大小,要比实际的绘图位置大一点 paint = new Paint(); paint.setColor(Color.GREEN); /画波形的颜色是绿色的,区别于坐标轴黑色 paint.setStrokeWidth(3); bluetooth.setOnClickListener(new MyButtonListener(); /添加按钮监听器 开启蓝牙 开启连接通信线程 clear.setOnClickListener(new MyButtonClearListener(); /添加按钮监听器 清除TextView内容 holder.addCallback(new Ca

13、llback() /按照上面注释,添加回调函数 public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) drawBack(holder); /如果没有这句话,会使得在开始运行程序,整个屏幕没有白色的画布出现 /直到按下按键,因为在按键中有对drawBack(SurfaceHolder holder)的调用 public void surfaceCreated(SurfaceHolder holder) / TODO Auto-generated method stub public voi

14、d surfaceDestroyed(SurfaceHolder holder) ); /添加按钮监听器 开启画图线程 sin.setOnClickListener(new OnClickListener() public void onClick(View v) / TODO Auto-generated method stub new DrawThread().start(); /线程启动 class MyButtonListener implements OnClickListener public void onClick(View v) / TODO Auto-generated m

15、ethod stub /如果没有打开蓝牙,此时打开蓝牙 if (!mBluetoothAdapter.isEnabled() Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); /此处我已经知道我对应的蓝牙模块的地址,所以省去扫描,配对的过程, /如果从头开始的话,需要添加很多内容。 /* * 这是获得在手机中已经存储的已经配对过的蓝牙信息 * Setdevices = mBlu

16、etoothAdapter.getBondedDevices(); if(devices.size()0) for(Iterator iterator = devices.iterator();iterator.hasNext();) BluetoothDevice bluetoothdevice = (BluetoothDevice) iterator.next(); System.out.println(bluetoothdevice.getAddress(); myview.append( n+bluetoothdevice.getAddress(); /这里就是获取地址 当然这里又不止

17、一个信息,所有配对的信息都有, * 你要选择蓝牙模块的地址,其实地址只是一个说法,它实际上是一个虚拟通道, * 我也不太清楚,就称之为地址了 */ BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(20:13:03:18:10:09 try socket = device.createRfcommSocketToServiceRecord(uuid); /建立连接 BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); /取消搜索蓝牙设备,不写也罢,因为我压根没搜索 so

18、cket.connect(); /建立连接,如果连接成功,此时蓝牙模块中的显示当前连接状态的指示灯就不会闪烁了, /此时意味着连接成功了 catch (IOException e) e.printStackTrace(); thread = new ConnectedThread(socket); /开启通信的线程 thread.start(); class MyButtonClearListener implements OnClickListener myview.setText( Handler handler = new Handler() /这是处理消息队列的Handler对象 Ov

19、erride public void handleMessage(Message msg) /处理消息 if (msg.what=READ) String str = (String)msg.obj; /类型转化 myview.append( +str); /显示在画布下方的TextView中 super.handleMessage(msg); ; /* * 关于此类,因为我的手机端只需要从蓝牙读取数据,不需要发送,所以就不必实现输出流 * 其实相比起来,输出流更简单 private class ConnectedThread extends Thread private final Blue

20、toothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; /构造函数 public ConnectedThread(BluetoothSocket socket) mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; / Get the input and output streams, using temp objects because / member st

21、reams are final try tmpIn = socket.getInputStream(); /获取输入流 tmpOut = socket.getOutputStream(); /获取输出流 catch (IOException e) mmInStream = tmpIn; mmOutStream = tmpOut; public void run() byte buffer = new byte1024; / buffer store for the stream int bytes; / bytes returned from read() / Keep listening t

22、o the InputStream until an exception occurs while (true) try / Read from the InputStream bytes = mmInStream.read(buffer); /bytes数组返回值,为buffer数组的长度 / Send the obtained bytes to the UI activity String str = new String(buffer); temp = byteToInt(buffer); /用一个函数实现类型转化,从byte到int handler.obtainMessage(READ, bytes, -1, str) .sendToTarget(); /压入消息队列 catch (Exception e) System.ou

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

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