Android Studio 之蓝牙串口通信(SPP)

撰写不易,记下笔记,全是干货源码:

一、先创建一个空project

  • 选择Empty Activity,然后Next

  • 选择工程位置与项目名称,然后点Finsh即可

二、设计布局activity_main.xml(如下)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="本地蓝牙设备:"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="设备名称"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="10dp"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="205dp"/>

    <Button
        android:id="@+id/button_open"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="打开蓝牙"
        android:textSize="20dp"
        app:layout_constraintEnd_toStartOf="@id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text1" />

    <Button
        android:id="@+id/button_close"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="关闭蓝牙"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/guideline2"
        app:layout_constraintTop_toBottomOf="@id/text2"/>

    <Button
        android:id="@+id/button_search"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="搜索蓝牙设备"
        android:textSize="20dp"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_open" />

    <Button
        android:id="@+id/button_check"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="已绑定设备"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toBottomOf="@+id/button_close" />


    <ListView
        android:id="@+id/listview1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#6D91AE"
        android:gravity="center"
        android:text="蓝牙列表"
        android:textSize="25dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_search" />

</androidx.constraintlayout.widget.ConstraintLayout>

三、再新建一个Empty Activity,作为连接后跳转的界面(命名为send_and_receive或自定义命名)

四、设计布局activity_send_and_receive.xml(如下)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Send_and_receive">

    <TextView
        android:id="@+id/textView_ble"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:gravity="fill_vertical"
        android:text="蓝牙状态:"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView_ble_connect"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="70dp"
        android:gravity="fill_vertical"
        android:text="已连接"
        android:textColor="#3F51B5"
        android:textSize="20dp"
        app:layout_constraintStart_toEndOf="@+id/textView_ble"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listView_receive"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/textView_send"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.538"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_receive" />

    <TextView
        android:id="@+id/textView_receive"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#6D91AE"
        android:gravity="center"
        android:text="接收区"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_ble" />

    <TextView
        android:id="@+id/textView_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#6D91AE"
        android:gravity="center"
        android:text="发送区"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/editText_send"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/editText_send"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button_send"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button_send"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="发送"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/editText_send" />


</androidx.constraintlayout.widget.ConstraintLayout>

五、在res文件下新建一个文件命名为menu,而后在该文件下新建一个菜单栏menu1.xml

六、menu1.xml菜单设计

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_button_connect" android:title="连接蓝牙设备"/>
    <item android:id="@+id/menu_button_disconnect" android:title="断开蓝牙设备"/>
    <item android:id="@+id/menu_button_return" android:title="返回上一页"/>
    <item android:id="@+id/menu_button_clean" android:title="清除数据"/>

</menu>

七、AndroidMainfest.xml文件中添加权限申请(如下)

<!-- 添加权限 -->
    <!-- 允许应用程序使用本机设备的蓝牙功能 -->
    <uses-permission android:name="android.permission.BLUETOOTH" /> <!-- 允许应用程序对蓝牙进行发现和配对,注:安装12新增的蓝牙权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <!-- 定位权限,蓝牙搜索需要 -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <!-- 允许应用程序连接到蓝牙设备 -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- 允许应用程序启用蓝牙广播功能 -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> <!-- 应用程序需访问设备的精准地理位置 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 应用程序需访问设备的粗略地理位置 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

八、新建一个蓝牙适配器类BluetoothController.java

代码如下:
package com.example.ble_0426;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 *蓝牙控制器
 */
public class BluetoothController {
    //成员变量
    private BluetoothAdapter mAdapter;
    private BluetoothSocket btSocket;
    private String TAG = "";

    String bluetoothName;

    /**
     * 构造函数
     */
    public BluetoothController()
    {
        //获取本地的蓝牙适配器
        mAdapter =BluetoothAdapter.getDefaultAdapter();
    }

    /**
     *获取本机蓝牙名称
     */
    public String getBluetoothName()
    {
        bluetoothName = mAdapter.getName();
        return bluetoothName;
    }
    /**
    * 打开蓝牙
    */
    public void turnOnBlueTooth(Activity activity, int requestCode)
    {
        if (!mAdapter.isEnabled())
        {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivityForResult(intent, requestCode);
        }
        else
            return;
    }

    /**
     * 关闭蓝牙
     */
    public void turnOffBlueTooth()
    {
        if (mAdapter.isEnabled())
        {
            mAdapter.disable();
        }
    }

    /**
     * 根据蓝牙地址找到相应的设备
     */
    public BluetoothDevice find_device(String addr)
        {
            return mAdapter.getRemoteDevice(addr);
        }

    /**
     *停止搜索设备
     */
    public void cancelSearch()
        {
            mAdapter.cancelDiscovery();
        }

    /**
     * 搜索蓝牙设备
     */
    public boolean searchBle()
    {
        assert (mAdapter !=null);
        if (mAdapter.isDiscovering())
        {
            mAdapter.cancelDiscovery();
            return false;
        }
        else {
            return mAdapter.startDiscovery();
        }
    }

    /**
     * 查看已绑定的蓝牙设备
     */
    public ArrayList<BluetoothDevice> getBleDeviceList()
    {
        return new ArrayList<BluetoothDevice>(mAdapter.getBondedDevices());
    }
}

九、MainActivity.java代码如下:

package com.example.ble_0426;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    //蓝牙权限列表
    public ArrayList<String> requestList = new ArrayList<>();
    //常量
    private static final int REQ_PERMISSION_CODE = 1;
    //实例化蓝牙控制器
    public BluetoothController btController = new BluetoothController();
    //弹窗
    private Toast mToast;

    //定义一个列表,存蓝牙设备地址,用于显示
    public ArrayList<String> deviceName = new ArrayList<>();
    //定义一个列表,存储蓝牙设备的地址
    public ArrayList<String> arrayList = new ArrayList<>();
    //定义一个适配器,用于刷新列表更新
    public ArrayAdapter adapter1;
    //搜索蓝牙广播
    private IntentFilter foundFilter;

    /**
     * 搜索蓝牙广播,将搜索到的蓝牙名称和地址添加到列表上
     */
    private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action))
            {
                String label;
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() == 12)
                {
                    label = "设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:已配对" + "\n";
                }
                else if (device.getBondState() == 10)
                {
                    label = "设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:未配对" + "\n";
                }
                else
                {
                    label = "设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:未知" + "\n";
                }
                if (!deviceName.contains(label))
                {
                    deviceName.add(label);      //将搜索到的设备添加到列表上
                    arrayList.add(device.getAddress());     //将搜索到的蓝牙地址添加到列表上
                    adapter1.notifyDataSetChanged();        //更新
                }
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                showToast("搜索结束");
                unregisterReceiver(this);
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
            {
                showToast("开始搜索");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //显示本机蓝牙名称
        TextView textView_name = (TextView) findViewById(R.id.text2);
        textView_name.setText(btController.getBluetoothName());

        //蓝牙状态改变信息
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        //搜索蓝牙广播
        foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        foundFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        foundFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        //实例化ArrayAdapter
        adapter1 = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,deviceName);
        //通过id获取ListView组件
        ListView listView = (ListView) findViewById(R.id.listview1);
        //添加到LisView组件中
        listView.setAdapter(adapter1);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CharSequence content = ((TextView) view).getText();
                String con = content.toString();
                String[] conArray = con.split("\n");
                String rightStr = conArray[1].substring(5,conArray[1].length());
                BluetoothDevice device = btController.find_device(rightStr);
                if (device.getBondState() == 10)
                {
                    btController.cancelSearch();
                    String label = "设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:未配对" + "\n";
                    deviceName.remove(label);
                    device.createBond();
                    label = "设备名:" + device.getName() +"\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:已配对" + "\n";
                    deviceName.add(label);
                    adapter1.notifyDataSetChanged();
                    showToast("配对:"+device.getName());
                }
                else
                {
                    btController.cancelSearch();
                    String label = "设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:已配对" + "\n";
                    if (deviceName.contains(label))
                    {
                        Intent intent = new Intent(MainActivity.this,Send_and_receive.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("deviceAddr", device.getAddress());
                        intent.putExtras(bundle);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });

        //自动打开蓝牙
        openBle();

        /**
         * 手动打开蓝牙
         */
        //通过id获取“打开蓝牙”按钮
        Button button_open = (Button) findViewById(R.id.button_open);
        //绑定按钮点击事件处理函数
        button_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openBle();
            }
        });

        /**
         * 关闭蓝牙
         */
        //通过id获取“关闭蓝牙”按钮
        Button button_close = (Button) findViewById(R.id.button_close);
        //绑定按钮点击事件处理函数
        button_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeBle();
            }
        });

        /**
         * 搜索蓝牙设备
         */
        //通过id获取”搜索蓝牙设备“按钮
        Button button_search = (Button) findViewById(R.id.button_search);
        //绑定按钮点击事件处理函数
        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPermision();     //获取蓝牙权限
                registerReceiver(bluetoothReceiver,foundFilter);     //注册广播
                //初始化各个列表
                arrayList.clear();
                deviceName.clear();
                adapter1.notifyDataSetChanged();    //更新
                btController.searchBle();
            }
        });

        /**
         * 查看已绑定设备
         */
        //通过id获取”已绑定设备“按钮
        Button button_check = (Button) findViewById(R.id.button_check);
        //绑定按钮点击事件处理函数
        button_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPermision();     //获取蓝牙权限
                //初始化各个列表
                arrayList.clear();
                deviceName.clear();
                adapter1.notifyDataSetChanged();    //更新
                //获取已绑定的蓝牙设备
                ArrayList<BluetoothDevice> bluetoothDevices = btController.getBleDeviceList();
                //更新列表
                for (int i = 0; i < bluetoothDevices.size(); i++)
                {
                    BluetoothDevice device = bluetoothDevices.get(i);
                    arrayList.add(device.getAddress());
                    if (device.getBondState() == 12) {
                        deviceName.add("设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:已配对" + "\n");
                    } else if (device.getBondState() == 10) {
                        deviceName.add("设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:未配对" + "\n");
                    } else {
                        deviceName.add("设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n" + "连接状态:未知" + "\n");
                    }
                    adapter1.notifyDataSetChanged();
                }
            }
        });

    }

    /**
     * 打开蓝牙
     */
    public void openBle()
    {
        getPermision();     //蓝牙权限
        btController.turnOnBlueTooth(this,1);
    }

    /**
     * 关闭蓝牙
     */
    public void closeBle()
    {
        getPermision();     //蓝牙权限
        btController.turnOffBlueTooth();
    }

    /**
     * 动态申请权限
     */
    public void getPermision()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
        {
            requestList.add(Manifest.permission.BLUETOOTH);
            requestList.add(Manifest.permission.BLUETOOTH_SCAN);
            requestList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
            requestList.add(Manifest.permission.BLUETOOTH_CONNECT);
            requestList.add(Manifest.permission.ACCESS_FINE_LOCATION);
            requestList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        if(requestList.size() != 0)
        {
            ActivityCompat.requestPermissions(this,requestList.toArray(new String[0]),REQ_PERMISSION_CODE);
        }
    }

    /**
     * 弹窗
     */
    public void showToast(String text)
    {
        if (mToast == null)
        {
            //初始化
            mToast = Toast.makeText(this,text,Toast.LENGTH_SHORT);
        }
        else
        {
            //修改显示文本
            mToast.setText(text);
        }
        //显示
        mToast.show();
    }
}

十、Send_and receive.java代码如下:

package com.example.ble_0426;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.UUID;

public class Send_and_receive extends AppCompatActivity {
    //蓝牙权限列表
    public ArrayList<String> requestList = new ArrayList<>();
    //常量
    private static final int REQ_PERMISSION_CODE = 1;
    //弹窗
    private Toast mToast;

    //蓝牙服务
    private BluetoothSocket bluetoothSocket;
    //实例化BleConnect
    private BleConnect bleConnect = new BleConnect();
    //实例化蓝牙适配器类
    public BluetoothController mController = new BluetoothController();

    //文本输入框
    public EditText editText;
    //接收区
    public ListView listView;
    //蓝牙状态文本显示
    public TextView text;

    //消息列表
    public ArrayList<String> listMessage = new ArrayList<>();
    //定义适配器
    public ArrayAdapter adapter1;

    //活动间消息传递
    public Handler mHandler;

    //读取数据线程
    public readMessage readmessage = new readMessage();

    private Bundle bundle;
    public Button button_send;
    public Menu button_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_and_receive);


        //获取intent
        Intent intent = getIntent();
        //获取intent传来的数据
        bundle = intent.getExtras();
        //连接服务
        bleConnect.connectDevice(mController.find_device(bundle.getString("deviceAddr")));
        //线程服务开始
        bleConnect.start();

        //文本输入框
        editText = (EditText) findViewById(R.id.editText_send);
        //接收区
        listView = (ListView) findViewById(R.id.listView_receive);
        //设置返回界面
        button_return = (Menu) findViewById(R.id.menu_button_return);
        //蓝牙状态文本显示
        text = (TextView) findViewById(R.id.textView_ble_connect);


        //实例化ArrayAdapter
        adapter1 = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,listMessage);
        //设置listView
        listView.setAdapter(adapter1);
        // 实例化Handler
        mHandler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        String s = msg.obj.toString();
                        listMessage.add("接收数据:" + s);
                        adapter1.notifyDataSetChanged();
                        break;
                }
            }
        };

        /**
         * 发送按钮
         */
        //通过id获取“发送”按钮
        button_send = (Button) findViewById(R.id.button_send);
        //绑定按钮点击事件处理函数
        button_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = editText.getText().toString();
                if (message.length() != 0)
                {
                    sendMessage(message);
                    listMessage.add("发送数据:" + message + "\n");
                    adapter1.notifyDataSetChanged();
                }
            }
        });
    }

    /**
     * 读取数据
     */
    private class readMessage extends Thread
    {
        private static final String TAG = "";

        public void run()
        {
            super.run();
            byte[] buffer = new byte[1024];
            int bytes;
            InputStream inputStream = null;

            try {
                inputStream = bluetoothSocket.getInputStream();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
            while (true)
            {
                try {
                    if ((bytes = inputStream.read(buffer)) > 0)
                    {
                        byte[] buf_data = new byte[bytes];
                        for (int i = 0; i < bytes; i++)
                        {
                            buf_data[i] = buffer[i];
                        }
                        String text = new String(buf_data, 0, bytes, "utf-8");        //接收的值inputstream 为 text
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = text;
                        mHandler.sendMessage(message);

                        if (text.equalsIgnoreCase("o"))
                        {
                            //o表示opend
                            showToast("open");
                        }
                        else if (text.equalsIgnoreCase("c"))
                        {
                            //c表示close
                            showToast("close");
                        }
                    }
                }
                catch (IOException e)
                {
                    try {
                        inputStream.close();
                    }
                    catch (IOException e1)
                    {
                        e1.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    /**
     * 发送数据
     */
    public void sendMessage(String label)
    {
        getPermision();     //获取权限
        if (bluetoothSocket == null)
        {
            showToast("没有连接");
            return;
        }
        try
        {
            OutputStream os = bluetoothSocket.getOutputStream();
            os.write(label.getBytes());     //发送出去的值为:label
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 连接蓝牙
     */
    private class BleConnect extends Thread
    {
        private void connectDevice(BluetoothDevice device)
        {
            try
            {
                getPermision();
                bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));        //蓝牙串口服务UUID(SPP)
                bluetoothSocket.connect();
                readmessage.start();
                showToast("蓝牙连接成功");

            }
            catch (IOException e)
            {
                e.printStackTrace();
                showToast("蓝牙连接失败");
            }
        }
    }

    /**
     * 断开蓝牙
     */
    public void disConnect()
    {
        try {
            bluetoothSocket.close();
            bluetoothSocket = null;
            button_send.setEnabled(false);
            showToast("蓝牙断开");
            text.setText("已断开");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     *菜单栏
     */
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu1,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.menu_button_connect:      //连接蓝牙,还未写
                break;
            case R.id.menu_button_disconnect:   //断开蓝牙
                getPermision();
                disConnect();
                break;
            case R.id.menu_button_return:       //返回上一页
                Intent intent = new Intent(Send_and_receive.this,MainActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_button_clean:       //清除数据
                adapter1.clear();
                break;
            default:
        }
        return true;
    }

    /**
     * 动态申请权限
     */
    public void getPermision()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
        {
            requestList.add(Manifest.permission.BLUETOOTH);
            requestList.add(Manifest.permission.BLUETOOTH_SCAN);
            requestList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
            requestList.add(Manifest.permission.BLUETOOTH_CONNECT);
            requestList.add(Manifest.permission.ACCESS_FINE_LOCATION);
            requestList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        if(requestList.size() != 0)
        {
            ActivityCompat.requestPermissions(this,requestList.toArray(new String[0]),REQ_PERMISSION_CODE);
        }
    }

    /**
     * 弹窗
     */
    public void showToast(String text)
    {
        if (mToast == null)
        {
            //初始化
            mToast = Toast.makeText(this,text,Toast.LENGTH_SHORT);
        }
        else
        {
            //修改显示文本
            mToast.setText(text);
        }
        //显示
        mToast.show();
    }
}

注:若上方代码有不理解的可一起在评论区讨论

效果运行如下

蓝牙串口通信

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,针对您的问题,我可以给出以下步骤: 1. 在 AndroidManifest.xml 文件中添加蓝牙权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 2. 在布局文件中添加一个按钮,用于连接蓝牙设备: ```xml <Button android:id="@+id/connect_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Connect" /> ``` 3. 在 Activity 中获取 BluetoothAdapter 对象,并启用蓝牙: ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // 设备不支持蓝牙 } else if (!bluetoothAdapter.isEnabled()) { // 请求用户打开蓝牙 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } ``` 4. 在 onActivityResult 方法中处理用户是否打开了蓝牙: ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_ENABLE_BT) { if (resultCode == RESULT_OK) { // 用户已打开蓝牙,可以进行连接操作 } else { // 用户未打开蓝牙,连接操作无法进行 } } } ``` 5. 在按钮的点击事件中连接蓝牙设备: ```java connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothSocket socket = null; try { socket = device.createRfcommSocketToServiceRecord(MY_UUID); socket.connect(); } catch (IOException e) { // 连接失败 } } }); ``` 以上就是基本的 SPP 蓝牙开发流程,您可以根据自己的需求进行修改和扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值