了解蓝牙技术
在开始Android蓝牙开发之前,我们需要先了解什么是蓝牙技术。蓝牙是一种无线技术,用于在短距离内(一般10米以内)传输数据。它广泛应用于手机、耳机、智能家居设备等领域。Android设备内置了蓝牙模块,使得开发者可以轻松实现蓝牙功能。
开发环境搭建
安装Android Studio
首先,我们需要安装Android Studio,这是Android开发的官方IDE。在官网下载并安装最新版本的Android Studio,然后配置SDK和模拟器。
# 安装Android Studio
wget https://dl.google.com/dl/android/studio/install/3.5.3.0/r/studio-eclipse-ide-2020.3.1.0-linux.zip
unzip studio-eclipse-ide-2020.3.1.0-linux.zip
cd studio-eclipse-ide-2020.3.1.0-linux
./studio.sh
# 配置SDK和模拟器
cd ~
mkdir android-sdk
cd android-sdk
mkdir platform-tools
mkdir tools
cd platform-tools
wget https://developer.android.com/studio/releases/platform-tools/latest/download/platform-tools-latest-linux.zip
unzip platform-tools-latest-linux.zip
cd ~
cd android-sdk
mkdir add-ons
cd add-ons
mkdir extras
cd extras
wget https://developer.android.com/studio/releases/platforms/latest/download/platforms-latest-linux.zip
unzip platforms-latest-linux.zip
创建新项目
在Android Studio中创建一个新项目,选择“Empty Activity”模板。
蓝牙基础
蓝牙通信原理
蓝牙通信基于主从模式,主设备负责发起连接和数据传输,从设备被动响应。通信过程包括扫描、配对、连接和数据传输等步骤。
蓝牙设备扫描
在Android中,我们可以使用BluetoothAdapter类来扫描附近的蓝牙设备。
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
// 处理已配对的设备
}
蓝牙设备配对
当找到目标设备后,我们需要进行配对操作。
BluetoothDevice device = ...;
BluetoothDevice bondDevice = bluetoothAdapter.getBondedDevice(device.getAddress());
if (bondDevice == null) {
BluetoothDevice bondDevice = bluetoothAdapter.createBond();
// 处理配对结果
}
蓝牙设备连接
配对成功后,我们可以连接到设备。
BluetoothSocket socket = bondDevice.createRfcommSocketToServiceRecord(BluetoothDevice.class);
socket.connect();
蓝牙数据传输
数据发送
连接成功后,我们可以通过BluetoothSocket发送数据。
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes("Hello, Bluetooth!");
outputStream.flush();
outputStream.close();
数据接收
在从设备端,我们需要监听数据接收事件。
BluetoothServerSocket serverSocket = new BluetoothServerSocket(1234);
serverSocket.listen();
BluetoothSocket socket = serverSocket.accept();
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String message = inputStream.readUTF();
inputStream.close();
socket.close();
serverSocket.close();
蓝牙安全
在蓝牙通信过程中,我们需要注意安全问题。建议使用加密通信,避免敏感数据泄露。
实战案例:蓝牙遥控器
下面我们将通过一个蓝牙遥控器案例来展示蓝牙开发的实战过程。
遥控器端
- 创建一个新项目,选择“Empty Activity”模板。
- 在
res/layout/activity_main.xml中添加按钮和文本视图。
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送数据" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- 在
MainActivity.java中实现蓝牙发送功能。
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// ... 扫描、配对、连接等操作 ...
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes("Hello, Bluetooth!");
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
控制端
- 创建一个新项目,选择“Empty Activity”模板。
- 在
res/layout/activity_main.xml中添加按钮和文本视图。
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接收数据" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- 在
MainActivity.java中实现蓝牙接收功能。
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// ... 扫描、配对、连接等操作 ...
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BluetoothServerSocket serverSocket = new BluetoothServerSocket(1234);
serverSocket.listen();
BluetoothSocket socket = serverSocket.accept();
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String message = inputStream.readUTF();
inputStream.close();
socket.close();
serverSocket.close();
TextView textView = findViewById(R.id.textView);
textView.setText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
总结
通过本文的介绍,相信你已经对Android蓝牙开发有了初步的了解。在实际开发过程中,你需要根据具体需求调整代码,并注意安全问题。希望本文能帮助你轻松上手Android蓝牙开发。
