相关项目的下载链接点击打开链接

继本项目之后实现了语音识别点击打开链接

1.因为涉及的代码较多,本人分成三篇介绍,这是第一篇,先让大家看一下界面和工程文件,如下

2,.Manifest中蓝牙权限的声明,其中包含用到的活动

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sacnbluetooth">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/myicon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".BluetoothInit">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BluetoothServiceOrClient"
            android:label="BluetoothServiceOrClient"/>
        <activity android:name=".SearchBluetoothDevice"
            android:label="SearchBluetoothDevice"/>
    </application>

</manifest>

3.布局文件的声明

3.1  init_layout


<?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">

    <Button
        android:id="@+id/open_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开蓝牙"/>
    <Button
        android:id="@+id/close_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭蓝牙"/>
    <Button
        android:id="@+id/pair_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="配对设备"
        android:onClick="onMakeDiscoverableButtonClicked"/>
    <Button
        android:id="@+id/discoverable_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="可被搜索"
        android:onClick="onMakeDiscoverableButtonClicked"/>
    <Button
        android:id="@+id/service_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="服务端"
        android:onClick="onOpenServerSocketButtonClicked"/>
    <Button
        android:id="@+id/client_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="客户端"
        android:onClick="onOpenClientSocketButtonClicked"/>
    <ListView
        android:id="@+id/pair_devices_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

3.2  msg_layout


<?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="wrap_content">

    <LinearLayout
        android:id="@+id/receive_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_margin="10dp"
        android:background="@drawable/message_left">

        <TextView
            android:id="@+id/receive_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:maxWidth="250dp"></TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/send_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/message_right">

        <TextView
            android:id="@+id/send_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:maxWidth="250dp"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

3.3  search_layout


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

    ----允许搜索的进度条,默认样式
    <ProgressBar
        android:id="@+id/start_discovery_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/bottom_button">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始查找"
            android:textAllCaps="false"
            android:id="@+id/start_search"/>
    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view"
        android:layout_above="@id/bottom_button">
    </ListView>

</RelativeLayout>

3.4  chat_layout


<?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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id = "@+id/container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id= "@+id/edit_bottombar"
        android:layout_alignParentBottom = "true">

        <Button android:id="@+id/disConnectButton"
            android:layout_width="65dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft ="true"
            android:text="断开"/>

        <Button android:id="@+id/msgSendButton"
            android:layout_width="65dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight ="true"
            android:text="发送"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height = "wrap_content"
            android:layout_toLeftOf="@id/msgSendButton"
            android:layout_toRightOf="@+id/disConnectButton"
            android:hint = "说点什么呢?"
            android:textSize="15dip"
            android:maxLines="3"
            android:id = "@+id/msgEditText"/>
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/msg_RecycleView"
        android:layout_above="@id/edit_bottombar">
    </android.support.v7.widget.RecyclerView>
    <!--
    <ListView
        android:id="@+id/msgListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollingCache="false"
        android:divider="#ffc6c6c6"
        android:layout_weight="1.0"
        android:layout_above = "@id/edit_bottombar" />
    -->

</RelativeLayout>
</LinearLayout>

4.继续请看下一篇

android Bluetooth 开发2之打开、关闭、搜索、允许搜索、查看:点击打开链接


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐