当前位置: 网学 > 编程文档 > Android > 正文

Android手机蓝牙控制

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: myeducs.cn 发布时间: 13/03/17

【网学网提醒】:网学会员,鉴于大家对Android手机蓝牙控制十分关注,会员在此为大家搜集整理了“Android手机蓝牙控制”一文,供大家参考学习!


    Android手机端控制程序的编写:
    首先打开Eclipse,当然之前的Java开发环境和安卓开发工具自己得先配置好,这里就不多说了,网上教程一大摞。
    然后新建一个Android项目,修改布局文件main.xml,代码如下:
    
        android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="schemas.android/apk/res/android"
    >
        android:id="@+id/btnF"
    android:layout_width="100px"
    android:layout_height="60px"
    android:text="前进"
    android:layout_x="130px"
    android:layout_y="62px"
    >
    
        android:id="@+id/btnL"
    android:layout_width="100px"
    android:layout_height="60px"
    android:text="左转"
    android:layout_x="20px"
    android:layout_y="152px"
    >
    
        android:id="@+id/btnR"
    android:layout_width="100px"
    android:layout_height="60px"
    android:text="右转"
    android:layout_x="240px"
    android:layout_y="152px"
    >
    
        android:id="@+id/btnB"
    android:layout_width="100px"
    android:layout_height="60px"
    android:text="后退"
    android:layout_x="130px"
    android:layout_y="242px"
    >
    
        android:id="@+id/btnS"
    android:layout_width="100px"
    android:layout_height="60px"
    android:text="停止"
    android:layout_x="130px"
    android:layout_y="152px"
    >
    
    
    这个布局文件的效果就是如视频中所示的手机操作界面。
    然后是权限声明,这一步不能少,否则将无法使用安卓手机的蓝牙功能。
    权限声明如下:
    打开AndroidManifest.xml文件,修改代码如下:
    
        package="com.ThinBTClient.www"
    android:versionCode="1"
    android:versionName="1.0">
    
    
    
        android:label="@string/app_name">
    
    
    
    

    
    
    
    其中红色、加粗部分就是要添加的权限声明。
    然后编写Activity中的执行代码,这些代码的作用就是发送指令,控制小车的运动。
    代码如下:
    packagecom.ThinBTClient.www;
    importandroid.app.Activity;
    importandroid.os.Bundle;
    importjava.io.IOException;
    importjava.io.OutputStream;
    importjava.util.UUID;
    importandroid.app.Activity;
    importandroid.bluetooth.BluetoothAdapter;
    importandroid.bluetooth.BluetoothDevice;
    importandroid.bluetooth.BluetoothSocket;
    importandroid.content.DialogInterface;
    importandroid.content.DialogInterface.OnClickListe
    ner;
    importandroid.os.Bundle;
    importandroid.provider.ContactsContract.CommonDataKinds.Event;
    importandroid.util.Log;
    importandroid.view.MotionEvent;
    importandroid.view.View;
    importandroid.widget.Button;
    importandroid.widget.Toast;
    publicclassThinBTClientextendsActivity{
    privatestaticfinalStringTAG="THINBTCLIENT";
    privatestaticfinalbooleanD=true;
    privateBluetoothAdaptermBluetoothAdapter=null;
    privateBluetoothSocketbtSocket=null;
    privateOutputStreamoutStream=null;
    ButtonmButtonF;
    ButtonmButtonB;
    ButtonmButtonL;
    ButtonmButtonR;
    ButtonmButtonS;
    privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    privatestaticStringaddress="00:11:03:21:00:43";//<==要连接的蓝牙设备MAC地址
    /**Calledwhentheactivityisfirstcreated.*/
    @Override
    publicvoidonCreate(BundlesavedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //前进
    mButtonF=(Button)findViewById(R.id.btnF);
    mButtonF.setOnTouchListener(newButton.OnTouchListener(){
    @Override
    publicbooleanonTouch(Viewv,MotionEventevent){
    //TODOAuto-generatedmethodstub
    Stringmessage;
    byte[]msgBuffer;
    intaction=event.getAction();
    switch(action)
    {
    caseMotionEvent.ACTION_DOWN:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="1";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    caseMotionEvent.ACTION_UP:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="0";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    }
    returnfalse;
    }
    });
    //后退
    mButtonB=(Button)findViewById(R.id.btnB);
    mButtonB.setOnTouchListener(newButton.OnTouchListener(){
    @Override
    publicbooleanonTouch(Viewv,MotionEventevent){
    //TODOAuto-generatedmethodstub
    Stringmessage;
    byte[]msgBuffer;
    intaction=event.getAction();
    switch(action)
    {
    caseMotionEvent.ACTION_DOWN:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="3";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    caseMotionEvent.ACTION_UP:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="0";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch
    (IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    }
    returnfalse;
    }
    });
    //左转
    mButtonL=(Button)findViewById(R.id.btnL);
    mButtonL.setOnTouchListener(newButton.OnTouchListener(){
    @Override
    publicbooleanonTouch(Viewv,MotionEventevent){
    //TODOAuto-generatedmethodstub
    Stringmessage;
    byte[]msgBuffer;
    intaction=event.getAction();
    switch(action)
    {
    caseMotionEvent.ACTION_DOWN:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="2";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    caseMotionEvent.ACTION_UP:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="0";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    }
    returnfalse;
    }
    });
    //右转
    mButtonR=(Button)findViewById(R.id.btnR);
    mButtonR.setOnTouchListener(newButton.OnTouchListener(){
    @Override
    publicbooleanonTouch(Viewv,MotionEventevent){
    //TODOAuto-generatedmethodstub
    Stringmessage;
    byte[]msgBuffer;
    intaction=event.getAction();
    switch(action)
    {
    caseMotionEvent.ACTION_DOWN:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="4";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    caseMotionEvent.ACTION_UP:
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    message="0";
    msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    break;
    }
    returnfalse;
    }
    });
    //停止
    mButtonS=(Button)findViewById(R.id.btnS);
    mButtonS.setOnTouchListener(newButton.OnTouchListener(){
    @Override
    publicbooleanonTouch(Viewv,MotionEventevent){
    //TODOAuto-generatedmethodstub
    if(event.getAction()==MotionEvent.ACTION_DOWN)
    try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    Stringmessage="0";
    byte[]msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    returnfalse;
    }
    });
    if(D)
    Log.e(TAG,"+++ONCREATE+++");
    mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
    if(mBluetoothAdapter==null){
    Toast.makeT
    ext(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();
    finish();
    return;
    }
    if(!mBluetoothAdapter.isEnabled()){
    Toast.makeText(this,"PleaseenableyourBluetoothandre-runthisprogram.",Toast.LENGTH_LONG).show();
    finish();
    return;
    }
    if(D)
    Log.e(TAG,"+++DONEINONCREATE,GOTLOCALBTADAPTER+++");
    }
    @Override
    publicvoidonStart(){
    super.onStart();
    if(D)Log.e(TAG,"++ONSTART++");
    }
    @Override
    publicvoidonResume(){
    super.onResume();
    if(D){
    Log.e(TAG,"+ONRESUME+");
    Log.e(TAG,"+ABOUTTOATTEMPTCLIENTCONNECT+");
    }
    BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
    try{
    btSocket=device.createRfcommSocketToServiceRecord(MY_UUID);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);
    }
    mBluetoothAdapter.cancelDiscovery();
    try{
    btSocket.connect();
    Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");
    }catch(IOExceptione){
    try{
    btSocket.close();
    }catch(IOExceptione2){
    Log.e(TAG,"ONRESUME:Unabletoclosesocketduringconnectionfailure",e2);
    }
    }
    //Createadatastreamsowecantalktoserver.
    if(D)
    Log.e(TAG,"+ABOUTTOSAYSOMETHINGTOSERVER+");
    /*try{
    outStream=btSocket.getOutputStream();
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
    }
    Stringmessage="1";
    byte[]msgBuffer=message.getBytes();
    try{
    outStream.write(msgBuffer);
    }catch(IOExceptione){
    Log.e(TAG,"ONRESUME:Exceptionduringwrite.",e);
    }
    */
    }
    @Override
    publicvoidonPause(){
    super.onPause();
    if(D)
    Log.e(TAG,"-ONPAUSE-");
    if(outStream!=null){
    try{
    outStream.flush();
    }catch(IOExceptione){
    Log.e(TAG,"ONPAUSE:Couldn'tflushoutputstream.",e);
    }
    }
    try{
    btSocket.close();
    }catch(IOExceptione2){
    Log.e(TAG,"ONPAUSE:Unabletoclosesocket.",e2);
    }
    }
    @Override
    publicvoidonStop(){
    super.onStop();
    if(D)Log.e(TAG,"--ONSTOP--");
    }
    @Override
    publicvoidonDestroy(){
    super.onDestroy();
    if(D)Log.e(TAG,"---ONDESTROY---");
    }
    }
    在这个程序中蓝牙模块的MAC地址给写进去了,其实更合理一点应该让程序运行后搜索周围蓝牙设备,然后选择需要连接的设备,获取它的MAC地址再连接。
    
  • 上一篇资讯: Android技巧合集
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号