蓝牙技术简介
蓝牙(Bluetooth)是一种无线技术标准,允许电子设备之间进行短距离的数据交换。在Android开发中,蓝牙技术广泛应用于设备之间的数据传输,如智能家居、健康监测、车载系统等。掌握蓝牙开发,将为你的Android应用增添更多可能性。
蓝牙开发环境搭建
1. 安装Android Studio
首先,你需要安装Android Studio,这是Android开发的官方IDE。在官网下载并安装最新版本的Android Studio,然后创建一个新的Android项目。
2. 添加蓝牙权限
在AndroidManifest.xml文件中,添加以下权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
3. 添加蓝牙API依赖
在build.gradle(Module: app)文件中,添加以下依赖:
implementation 'androidx.bluetooth:bluetooth:1.2.0'
蓝牙基础概念
1. 蓝牙设备
蓝牙设备分为两类:蓝牙中心设备(Central)和蓝牙外围设备(Peripheral)。中心设备负责发起连接、发送数据等操作,外围设备则负责接收数据、响应请求等。
2. 蓝牙服务
蓝牙服务(Bluetooth Service)是Android中用于管理蓝牙设备和服务的一种组件。通过蓝牙服务,你可以创建、启动、停止蓝牙服务,并管理连接状态。
3. 蓝牙广播(Broadcast)
蓝牙广播用于接收蓝牙设备的状态变化,如设备发现、连接、断开等。
连接蓝牙设备
1. 扫描设备
使用BluetoothAdapter获取蓝牙适配器对象,然后调用其startDiscovery()方法开始扫描附近的蓝牙设备。
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startDiscovery();
2. 获取设备信息
在扫描回调中,获取扫描到的设备信息,如设备名称、地址等。
bluetoothAdapter.getScanResults().forEach(result -> {
Log.d("Bluetooth", "Device Name: " + result.getName() + ", Device Address: " + result.getAddress());
});
3. 连接设备
获取设备对象后,调用其connectGatt()方法连接设备。
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
BluetoothGatt gatt = device.connectGatt(context, false, gattCallback);
4. 连接回调
实现BluetoothGattCallback接口,处理连接状态、服务发现、读写数据等回调。
BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.d("Bluetooth", "Connected to device");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.d("Bluetooth", "Disconnected from device");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d("Bluetooth", "Services discovered");
// 获取服务、特征、值等操作
}
}
// 其他回调方法...
};
数据传输
1. 读取数据
通过BluetoothGatt获取特征(Characteristic)对象,然后调用其readValue()方法读取数据。
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUUID).getCharacteristic(characteristicUUID);
gatt.readCharacteristic(characteristic);
2. 写入数据
通过BluetoothGatt获取特征(Characteristic)对象,然后调用其writeValue()方法写入数据。
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUUID).getCharacteristic(characteristicUUID);
characteristic.setValue(data);
gatt.writeCharacteristic(characteristic);
3. 监听数据变化
通过BluetoothGatt获取特征(Characteristic)对象,然后调用其setNotifyValue()方法监听数据变化。
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUUID).getCharacteristic(characteristicUUID);
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
实战案例
以下是一个简单的蓝牙数据传输案例,实现中心设备向外围设备发送数据,并接收外围设备返回的数据。
1. 外围设备(Peripheral)
创建一个外围设备,实现BluetoothGattServerCallback接口,处理连接、服务发现、读写数据等回调。
BluetoothGattServer gattServer = BluetoothGattServer.create(context, gattServerCallback);
gattServer.addService(service);
2. 中心设备(Central)
创建一个中心设备,实现BluetoothGattCallback接口,处理连接、服务发现、读写数据等回调。
BluetoothGatt gatt = device.connectGatt(context, false, gattCallback);
3. 数据传输
在中心设备中,调用外围设备的特征(Characteristic)对象的writeValue()方法发送数据。
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUUID).getCharacteristic(characteristicUUID);
characteristic.setValue(data);
gatt.writeCharacteristic(characteristic);
在外围设备中,调用特征(Characteristic)对象的setValue()方法接收数据。
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUUID).getCharacteristic(characteristicUUID);
characteristic.setValue(receivedData);
gatt.writeCharacteristic(characteristic);
总结
通过本文的介绍,相信你已经对Android蓝牙开发有了初步的了解。在实际开发过程中,你需要根据具体需求调整代码,并处理各种异常情况。希望本文能帮助你轻松上手Android蓝牙开发,实现更多有趣的应用。
