蓝牙聊天工具

网上有很多关于蓝牙聊天工具的示例代码,对比参考并加入了自己的理解和创新,自己也做了一个蓝牙聊天工具,总体感觉还可以,下面进行分析一下。
首先定义了 一个Activity主界面,进行聊天信息的输出和发送 。先将聊天界面的XML布局文件贴出来:

activity_bluetooth_chat.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ListView
        android:id="@+id/chat_window_LV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:transcriptMode="alwaysScroll"
        android:stackFromBottom="true"
        android:layout_weight="2">
    </ListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="p"
            android:onClick="takePhotoClicked"/>
        <EditText
            android:id="@+id/input_ET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="bottom"/>
        <Button
            android:text="send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onSendBtnClicked"/>
    </LinearLayout>
</LinearLayout>

包括一个ListView和一个LineLayout布局(一个EditText和一个Button),该ListView用来显示聊天信息的,由一个ArrayAdapter类进行管理,每当收到或者发送 一条信息时,调用add(Object obj)方法可以同步显示出来,另外属性android:transcriptMode=”alwaysScroll”表示当内容 逐渐增多时,会出现滚动条进行帮助显示。 android:stackFromBottom=”true”表示item是从底部开始添加的。而android:layout_gravity=”bottom”>属性表示 该部件的位置,此处是在parent的底部。
聊天记录的ListView里每个item的layout为:

talk_note.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    xmlns:android="http://schemas.android.com/apk/res/android"></TextView>

下面来看主Activity,先给出用到的数据成员:

public class BluetoothChat extends AppCompatActivity {
   
   private static final String TAG = "BluetoothChat";

    public static boolean mBoundListenConnection = false;

    private static final String path = "/DCIM/camera/";

    /*
        @brief:
        the flag indicate that the window is in behind
     */
    public static  boolean WINDOW_BEHIND_FLAG  = true;


    /*
        @brief:
        the handler flag used to decide the action will be taken
     */
    public static final int NEW_DEVICE_READY_TO_CONNECT = 0;

    /*
     * @Brief:
     *     the intent request code
     */
    private static final int REQUEST_CODE_ENABLE_BLUETOOTH = 1001;
    private static final int REQUEST_CODE_SCAN_DEVICES  = 1002;
    public static final int REQUEST_CODE_CAMERA_ACTION = 1003;
    private static final int REQUEST_CODE_PHOTO_list = 1004
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值