Android 与蓝牙模块通信

本文是一位Android初学者关于使用HC-05蓝牙模块进行通信的实践记录。通过USB-TTL连接蓝牙模块并设置波特率,然后在Android端创建界面,实现了蓝牙的连接和数据收发。在实现过程中遇到了连接失败的问题,最终通过CSDN问答找到解决方案,成功连接蓝牙模块。
摘要由CSDN通过智能技术生成

前言

安卓小白,想了解一下蓝牙通信,就做了这个东西

蓝牙模块

采用的是HC-05型号的蓝牙模块
在这里插入图片描述
usb-ttl蓝牙模块 连接
VCC接VCC GND接GND Tx Rx交叉连接
在这里插入图片描述
使用串口助手调试 波特率为9600

发送AT,返回OK  说明已经连接成功(需要加回车换行)

android端

先上效果图
在这里插入图片描述
在这里插入图片描述
界面搭建

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:background="#199fff"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">
        <TextView
            android:id="@+id/show_connection_device"
            android:text="未连接"
            android:layout_marginLeft="20dp"
            android:textColor="#ffffff"
            android:textSize="25sp"
            android:layout_centerInParent="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/show_menu_list"
            android:layout_centerInParent="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/menu_img"
            android:layout_width="30dp"
            android:layout_height="30dp" />
    </RelativeLayout>
    <include layout="@layout/content_main" />
</LinearLayout>

c

Android单片机与蓝牙模块连接起来,可以实现通过蓝牙模块发送和接收数据,下面是一个实例代码。 首先,需要在Android设备上打开蓝牙,并扫描周围的蓝牙设备。 ```java // 打开蓝牙 BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { Log.e("TAG", "该设备不支持蓝牙"); } else { if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } // 扫描蓝牙设备 private void searchBluetooth() { mBluetoothAdapter.startDiscovery(); // 注册广播接收器 IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, filter); } // 广播接收器 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // 获取蓝牙设备 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device != null) { Log.d("TAG", "发现蓝牙设备:" + device.getName() + " " + device.getAddress()); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { mBluetoothAdapter.cancelDiscovery(); } } }; ``` 然后,在Android设备上选择要连接的蓝牙设备,并进行连接操作。 ```java private void connectBluetooth() { BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mDeviceAddress); mBluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); try { mBluetoothSocket.connect(); Log.d("TAG", "连接成功"); } catch (IOException e) { Log.e("TAG", "连接失败:" + e.getMessage()); } } ``` 接下来,就可以开始进行数据的发送和接收操作了。 ```java // 发送数据 private void sendBluetooth(String data) { if (mBluetoothSocket != null) { try { OutputStream outputStream = mBluetoothSocket.getOutputStream(); outputStream.write(data.getBytes()); } catch (IOException e) { Log.e("TAG", "发送失败:" + e.getMessage()); } } } // 接收数据 private void receiveBluetooth() { if (mBluetoothSocket != null) { try { InputStream inputStream = mBluetoothSocket.getInputStream(); byte[] buffer = new byte[1024]; int len; StringBuilder sb = new StringBuilder(); while ((len = inputStream.read(buffer)) != -1) { sb.append(new String(buffer, 0, len)); } Log.d("TAG", "接收到数据:" + sb.toString()); } catch (IOException e) { Log.e("TAG", "接收失败:" + e.getMessage()); } } } ``` 以上就是一个Android单片机与蓝牙模块通信的实例代码。需要注意的是,在实际使用中,可能会存在多线程操作等问题,需要根据具体的情况进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值