蓝牙技术简介
蓝牙(Bluetooth)是一种无线技术标准,用于短距离的数据交换。它广泛应用于各种设备,如手机、电脑、智能家居设备等。在Android开发中,蓝牙编程是实现设备间通信的重要手段之一。
蓝牙编程基础
1. 蓝牙设备扫描与连接
在Android中,使用蓝牙设备需要进行以下步骤:
- 开启蓝牙功能:在AndroidManifest.xml文件中添加蓝牙权限,并在代码中检查蓝牙是否开启,如果没有开启则提示用户开启。
if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
- 扫描蓝牙设备:使用BluetoothAdapter的startDiscovery方法开始扫描附近的蓝牙设备。
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
// 显示已配对的设备
}
}
bluetoothAdapter.startDiscovery();
- 连接蓝牙设备:扫描到设备后,使用BluetoothDevice的connect方法连接设备。
BluetoothDevice device = ...; // 获取要连接的设备
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
2. 蓝牙数据传输
连接到蓝牙设备后,可以进行数据传输。以下是一些常用的数据传输方式:
- 串口通信:将蓝牙设备视为串口,使用串口通信库(如SerialPort)进行数据读写。
SerialPort serialPort = new SerialPort(socket, 9600, 8, 'N', 1);
InputStream inputStream = serialPort.getInputStream();
OutputStream outputStream = serialPort.getOutputStream();
- Socket通信:使用Socket进行数据传输。
Socket socket = ...; // 获取蓝牙Socket
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
3. 蓝牙调试
在蓝牙编程过程中,调试非常重要。以下是一些调试技巧:
- 查看日志:在代码中添加Log语句,方便查看程序运行过程。
Log.d("Bluetooth", "连接成功");
使用调试工具:使用Android Studio的调试工具,如断点、变量监视等。
使用串口调试助手:将蓝牙设备连接到电脑,使用串口调试助手查看数据传输过程。
蓝牙编程实战
以下是一个简单的蓝牙编程实战案例:实现一个简单的蓝牙串口通信程序。
创建Android项目:在Android Studio中创建一个新的Android项目。
添加蓝牙权限:在AndroidManifest.xml文件中添加蓝牙权限。
编写代码:
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket socket;
private OutputStream outputStream;
private InputStream inputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void connectToDevice(BluetoothDevice device) {
try {
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendData(String data) {
try {
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private void receiveData() {
try {
byte[] buffer = new byte[1024];
int bytes = inputStream.read(buffer);
String receivedData = new String(buffer, 0, bytes);
// 处理接收到的数据
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行程序:将手机连接到电脑,运行程序,然后连接到蓝牙设备。在程序中发送数据,可以在串口调试助手中查看接收到的数据。
通过以上步骤,您已经成功完成了蓝牙编程的入门。在实际开发过程中,您可以根据需求对程序进行扩展和优化。
