引言
在移动设备中,蓝牙技术是一种非常实用的短距离通信技术。Android系统作为全球最受欢迎的移动操作系统之一,内置了强大的蓝牙功能。本篇文章将带你从零开始学习Android蓝牙开发,包括设备配对和数据传输的技巧,让你轻松掌握这一技术。
蓝牙技术简介
1. 蓝牙是什么?
蓝牙(Bluetooth)是一种无线技术标准,旨在实现固定设备、移动设备和个人局域网之间的短距离通信。它使用2.4GHz的ISM频段,最大传输距离为10米。
2. 蓝牙的优势
- 短距离通信,节省电量
- 传输速率高,可达1Mbps
- 安全性高,采用加密技术
- 兼容性好,支持多种设备
Android蓝牙开发环境搭建
1. 开发工具
- Android Studio:Android官方开发工具,支持蓝牙开发
- Android SDK:包含蓝牙开发所需的API
2. 蓝牙模块
- Android 4.3及以上版本:内置蓝牙模块,无需额外安装
- Android 4.2以下版本:需要安装蓝牙模块
设备配对
1. 配对流程
- 打开蓝牙设备
- 打开手机蓝牙
- 搜索蓝牙设备
- 选择要配对的设备
- 输入配对码(如需)
2. 代码实现
// 打开蓝牙
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter.isEnabled()) {
// 蓝牙已开启
} else {
// 蓝牙未开启,请求开启
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
// 搜索蓝牙设备
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
// 显示已配对的设备
}
} else {
// 没有已配对的设备,开始搜索
scanLeDevice(true);
}
// 扫描蓝牙设备
private void scanLeDevice(boolean enable) {
if (enable) {
bluetoothAdapter.startLeScan(mLeScanCallback);
} else {
bluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
// 蓝牙扫描回调
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// 处理扫描到的设备
}
};
数据传输
1. 数据传输方式
- SPP(串口通信)
- GATT(属性协议)
2. SPP数据传输
// 连接蓝牙设备
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
// 发送数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write(data);
// 接收数据
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int bytes = inputStream.read(buffer);
String receivedData = new String(buffer, 0, bytes);
// 关闭连接
socket.close();
3. GATT数据传输
// 连接GATT服务器
BluetoothGatt gatt = device.connectGatt(context, false, mGattCallback);
// 读取属性
BluetoothGattCharacteristic characteristic = gatt.getCharacteristic(characteristicUUID);
if (characteristic != null) {
gatt.readCharacteristic(characteristic);
}
// 写入属性
BluetoothGattCharacteristic characteristic = gatt.getCharacteristic(characteristicUUID);
if (characteristic != null) {
characteristic.setValue(data);
gatt.writeCharacteristic(characteristic);
}
// 监听属性变化
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUUID);
if (descriptor != null) {
gatt.setCharacteristicNotification(characteristic, true);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
// GATT回调
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// 处理连接状态变化
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
// 处理服务发现
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// 处理读取属性
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// 处理写入属性
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
// 处理属性变化
}
};
总结
通过本文的介绍,相信你已经掌握了从零开始学习Android蓝牙开发的方法。在实际开发过程中,还需要不断积累经验,解决各种问题。希望本文能帮助你轻松实现设备配对与数据传输。