1.蓝牙串口模块使用SPP-CA模块
蓝牙串口模块就是使用单片机的TX ,RX与蓝牙模块通信,单片机通过TX发送数据给蓝牙模块,然后蓝牙模块在通过蓝牙协议把数据发送出去,蓝牙模块与Android的通信方式使用spp协议。
2.蓝牙通信
蓝牙的通信需要服务端和客户端,客户端搜索蓝牙设备,链接服务端。主要是通过socket通信,相关的知识可以学习了解下socket通信。链接建立起来,通信时数据的读写是通过输入输出流的方式读写。
3.Android蓝牙架构和api说明
查看《Android系统原理及开发要点详解》和《深入理解Android网络编程》里的蓝牙相关章节,2本书里相关的章节有详细的介绍。
4.uuid
Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.
这是Android官方文档的说明,如果是连接蓝牙串口模块使用通用的uuid 00001101-0000-1000-8000-00805F9B34FB,如果是Android手机间的通信链接,可以使用生成的uuid,客服端和服务端的uuid必须相同
5.下面展示的代码是蓝牙串口控制arduino开发板LED灯的亮灭
MainActivity代码:
package net.flyget.bluetoothhelper;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
private List<Integer> mBuffer;
private ListView lv;
List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();;
private SimpleAdapter adapter;
private static final String TAG = "MainActivity";
private BluetoothAdapter mBluetoothAdapter;
private ConnectThread mConnectThread;
public ConnectedThread mConnectedThread;
private Button mClearBtn, mScanBtn, mSendBtn;
private EditText mEditText;
private String mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = "Bluetooth Debug Helper";
setTitle(mTitle);
mClearBtn = (Button) findViewById(R.id.clearBtn);
mScanBtn = (Button) findViewById(R.id.scanBtn);
mSendBtn = (Button) findViewById(R.id.sendBtn);
mClearBtn.setOnClickListener(this);
mScanBtn.setOnClickListener(this);
mSendBtn.setOnClickListener(this);
lv = (ListView) findViewById(R