查看: 5480|回复: 12

【三人行】第八篇、android蓝牙控制客户端

[复制链接]
  • TA的每日心情
    开心
    2014-4-3 10:09
  • 签到天数: 149 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2013-9-6 22:23:55 | 显示全部楼层 |阅读模式
    分享到:
    android控制客户端程序
    1、控制界面的布局
    控制界面主要运用了线性布局、相对布局和表格布局。整体采用线性布局,局部采用相对布局,而控制按钮采用了表格布局。控制界面的布局如图1所示:

    2、布局的代码如下:

    <?xml version="1.0"encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="wrap_content"

        android:layout_height="fill_parent"

        androidrientation="vertical" >

    <RelativeLayout

       android:id = "@+id/container"

        androidrientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        >

       <RelativeLayout

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"

          android:id= "@+id/edit_bottombar"

          android:layout_alignParentBottom= "true">

        <Button android:id="@+id/btn_disconnect"

                android:layout_width="65dp"

                android:layout_height="wrap_content"  

                android:layout_alignParentLeft ="true"

                android:text="断开"/>

            <Button android:id="@+id/btn_msg_send"

                android:layout_width="65dp"

                android:layout_height="wrap_content"  

                android:layout_alignParentRight ="true"

                android:text="发送"/>

            

            <EditText

                android:id="@+id/MessageText"

                android:layout_width="98dp"

                android:layout_height="wrap_content"

                android:layout_toRightOf="@+id/btn_disconnect"

                android:hint="说点什么呢?"

                android:textSize="15dip"

                  />

        </RelativeLayout>  

         <ListView

             android:id="@+id/list"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:layout_above="@id/edit_bottombar"

             android:layout_below="@id/container"

             android:layout_weight="1.0"

             android:divider="#ffc6c6c6"

             android:scrollingCache="false"

             android:visibility="visible"/>


         <TableLayout

             android:layout_width="match_parent"

             android:layout_height="wrap_content">


             <TableRow

                 android:id="@+id/tableRow1"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content">


                 <Button

                     android:id="@+id/button1"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:text="Button"

                     android:visibility="invisible"/>


                 <Button

                     android:id="@+id/start"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:layout_marginRight="0dp"

                     android:text="start"

                     android:width="120px"/>

             </TableRow>


             <TableRow

                 android:id="@+id/tableRow2"

                 android:layout_width="wrap_content"

                 android:layout_height="0dp"

                 android:layout_weight="1">


                 <Button

                     android:id="@+id/left"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:layout_marginRight="0dp"

                     android:text="left"

                     android:width="120px"/>


                 <Button

                     android:id="@+id/stop"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:text="stop"

                     android:width="120px"/>


                 <Button

                     android:id="@+id/right"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:text="right"

                     android:width="120px"/>

             </TableRow>


             <TableRow

                 android:id="@+id/tableRow3"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content">


                 <Button

                     android:id="@+id/button2"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:text="Button"

                     android:visibility="invisible"/>


                 <Button

                     android:id="@+id/back"

                     android:layout_width="wrap_content"

                     android:layout_height="wrap_content"

                     android:text="Back"/>

             </TableRow>

         </TableLayout>

    </RelativeLayout>

    </LinearLayout>

    3、android客户端的界面如图2所示:

    4、发送按钮的代码

    sendButton=(Button)findViewById(R.id.btn_msg_send);

    sendButton.setOnClickListener(new OnClickListener() {

    @Override

    public voidonClick(View arg0) {

    // TODO Auto-generated method stub

    String msgText =editMsgView.getText().toString();//获取编辑框内的内容

    if (msgText.length()>0) {

        sendMessageHandle(msgText);//发送编辑框的内容给串口   

        editMsgView.setText("");//清空编辑框

        editMsgView.clearFocus();

    //close InputMethodManager

    InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

                imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

    }

    else

    Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();

    }

    });

    5、控制按钮的代码
    以左转按钮为例:
    sendButton=(Button)findViewById(R.id.left);
    sendButton.setOnClickListener(new OnClickListener() {
    @Override
    public voidonClick(View arg0) {
    String msgText ="1"; // 发送左转命令“l”
    if (msgText.length()>0) {
    sendMessageHandle(msgText);//发送“l”给串口
    editMsgView.setText("");//清空编辑框
    editMsgView.clearFocus();
    //close InputMethodManager
    InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
    }
    else
    Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();
    }
    });

    图1 控制界面的布局

    图1 控制界面的布局

    图2 android控制界面

    图2 android控制界面
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2014-8-11 16:59
  • 签到天数: 33 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2013-9-6 22:30:30 | 显示全部楼层
    很好,很前端,顶顶
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2014-8-19 06:31
  • 签到天数: 296 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2013-9-7 07:20:09 | 显示全部楼层
    不错哦----------------------
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-5-12 10:32
  • 签到天数: 295 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2013-9-7 08:16:26 | 显示全部楼层
    牛逼啊~~~~~~~~~~
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2014-4-3 10:09
  • 签到天数: 149 天

    连续签到: 1 天

    [LV.7]常住居民III

     楼主| 发表于 2013-9-7 16:53:01 | 显示全部楼层
    xukaikai 发表于 2013-9-6 22:30
    很好,很前端,顶顶

    谢谢啊啊啊啊啊啊啊啊
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2014-4-3 10:09
  • 签到天数: 149 天

    连续签到: 1 天

    [LV.7]常住居民III

     楼主| 发表于 2013-9-7 16:53:40 | 显示全部楼层
    fj1161 发表于 2013-9-7 08:16
    牛逼啊~~~~~~~~~~

    过奖了
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2014-4-3 10:09
  • 签到天数: 149 天

    连续签到: 1 天

    [LV.7]常住居民III

     楼主| 发表于 2013-9-7 16:54:15 | 显示全部楼层
    吖坨 发表于 2013-9-7 07:20
    不错哦----------------------

    谢谢了啊啊
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2013-9-16 19:46
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2013-9-8 09:51:06 | 显示全部楼层
    嗯,毫无意义的代码,完全不涉及蓝牙部分
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2014-4-3 10:09
  • 签到天数: 149 天

    连续签到: 1 天

    [LV.7]常住居民III

     楼主| 发表于 2013-9-8 10:53:41 | 显示全部楼层
    xxn59 发表于 2013-9-8 09:51
    嗯,毫无意义的代码,完全不涉及蓝牙部分

    嗯,我会后续贴上
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2014-7-23 15:39
  • 签到天数: 246 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2013-9-12 09:08:59 | 显示全部楼层
    {:soso_e179:}{:soso_e179:}{:soso_e179:}{:soso_e179:}{:soso_e179:}{:soso_e179:}
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-21 19:30 , Processed in 0.206416 second(s), 34 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.