Android Studio在类微信程序完成“蓝牙聊天功能”实现蓝牙通信

项目运行截图

(采用真机调试两部手机均连接在电脑上打开开发者模式的usb调试,在两部手机上运行后即可实现通信,真机调试在上一次的百度地图定位里有讲)
1、还未连接
在这里插入图片描述
2、页面视图(添加连接对象以及显示自己在线信息和退出)
在这里插入图片描述
3、华为手机选择蓝牙页面
在这里插入图片描述
4、小米手机选择蓝牙页面
在这里插入图片描述
5、华为手机连接上小米手机页面以及小米手机连接华为手机页面
在这里插入图片描述
在这里插入图片描述
6、华为手机和小米手机的通信
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

通信原理

Android Studio近距离通信:Bluetooth
蓝牙(Bluetooth)是-种支持设备短距离(一般10m内)通信的无线电技术,能在包括移动电话、PDA、无线耳机、笔记本电脑、相关外设等众多设备之间进行无线信息交换。蓝牙采用分散式网络结构以及快跳频和短包技术,支持点对点及一点对多点通信,工作在全球通用的2.4GHzISM (即工业、科学、医学)频段,其数据速率为1Mbps。蓝牙采用时分双工传输方案实现全双工传输。
Android所有关于蓝牙开发的类都在android.bluetooth包下,只有8个类:
BluetoothAdapter 本地蓝牙适配器
BluetoothClass 蓝牙类(主要包括服务和设备)
BluetoothClass.Device 蓝牙设备类
BluetoothClass.Device.Major 蓝牙设备管理
BluetoothClass.Service 蓝牙服务类
BluetoothDevice 蓝牙设备(远程蓝牙设备)
BluetoothServiceSocket 监听蓝牙连接的类
BluetoothSocket 蓝牙连接类

蓝牙权限

首先需要AndroidManifest.xml文件中添加操作蓝牙的权限。
下面是普通权限,只需要在清单文件里注册,不需要在程序里动态申请。

    //Android 6.0涉及到的权限
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    //允许程序连接到已配对的蓝牙设备。
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    //允许程序发现和配对蓝牙设备。
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    //Android 6.0涉及到的权限
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>

strings.xml

在文件res/values/strings.xml里,添加程序运行过程中的状态描述文本及配色代码等,其代码如下:

<resources>
    <string name="app_name">MyWeChat</string>
    <string name="weixin">微信</string>
    <string name="frd">朋友</string>
    <string name="contact">通讯录</string>
    <string name="settings">设置</string>
    <string name="name">姓名</string>
    <string name="phone">电话</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>

    <string name="send">发送</string>
    <string name="not_connected">你没有链接一个设备</string>
    <string name="bt_not_enabled_leaving">蓝牙不可用,离开聊天室</string>
    <string name="title_connecting">链接中...</string>
    <string name="title_connected_to">连接到:</string>
    <string name="title_not_connected">无链接</string>
    <string name="scanning">蓝牙设备搜索中...</string>
    <string name="select_device">选择一个好友链接</string>
    <string name="none_paired">没有配对好友</string>
    <string name="none_found">附近没有发现好友</string>
    <string name="title_paired_devices">已配对好友</string>
    <string name="title_other_devices">其它可连接好友</string>
    <string name="button_scan">搜索好友</string>
    <string name="connect">我的好友</string>
    <string name="discoverable">设置在线</string>
    <string name="back">退出</string>
    <string name="startVideo">开始聊天</string>
    <string name="stopVideo">结束聊天</string>
</resources>

tab01.xml

添加1个Toolbar控件,其内包含2个水平的TextView控件;在Toolbar控件的下方添加1个ListView控件,用于显示聊天内容;最后在ListView控件的下方添加水平放置的1个EditText控件和一个Button控件。使用垂直线性布局并嵌套水平线性布局实现的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/title_left_text"
                style="?android:attr/windowTitleStyle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:gravity="center|start"
                android:layout_weight="1"
                android:ellipsize="end"
                android:singleLine="true" />
            <TextView
                android:id="@+id/title_right_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:ellipsize="end"
                android:gravity="end|center"
                android:singleLine="true"
                android:textColor="#808080" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <ListView android:id="@+id/in"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText android:id="@+id/edit_text_out"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="bottom" />
        <Button android:id="@+id/button_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/send"/>
    </LinearLayout>
</LinearLayout>

菜单文件option_menu.xml

供主Activity使用的菜单文件res/menu/optionmenu.xml
关于如何在android建立menu文件夹请参考链接

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/scan"
        android:icon="@android:drawable/ic_menu_myplaces"
        android:title="@string/connect" />
    <item android:id="@+id/discoverable"
        android:icon="@android:drawable/ic_menu_view"
        android:title="@string/discoverable" />
    <item android:id="@+id/back"
        android:icon="@android:drawable/ic_menu_close_clear_cancel"
        android:title="@string/back" />
</menu>

device_list.xml

选择好友(即已经配对过的蓝牙设备)的界面布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:id="@+id/title_paired_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_paired_devices"
        android:visibility="gone"
        android:background="#666"
        android:textColor="#fff"
        android:paddingLeft="5dp" />
    <ListView android:id="@+id/paired_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <TextView android:id="@+id/title_new_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_other_devices"
        android:visibility="gone"
        android:background="#666"
        android:textColor="#fff"
        android:paddingLeft="5dp" />
    <!--android:visibility="gone"表示不占空间的隐藏,invisible是占空间-->
    <ListView android:id="@+id/new_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" />
    <Button android:id="@+id/button_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_scan" />
</LinearLayout>

ChatService.java

编写用于蓝牙会话的服务组件ChatService.java,其文件代码如下

本程序ChatService是蓝牙会话的服务程序
定义了3个内部类:AcceptThread(接受新连接)、ConnectThread(发出连接)和ConnectedThread (已连接)
public class ChatService {
   
    /*
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值