给一个安卓开发蓝牙的示例代码
时间: 2024-01-26 17:04:24 浏览: 26
好的,以下是一个基于Android的蓝牙开发示例代码。该代码演示了如何使用Android的蓝牙API扫描和连接蓝牙设备:
```java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice targetDevice;
@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, 1);
} else {
// 开始扫描蓝牙设备
scanBluetoothDevice();
}
}
private void scanBluetoothDevice() {
// 开始扫描蓝牙设备
bluetoothAdapter.startDiscovery();
// 注册广播接收器,用于监听扫描到的蓝牙设备
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 如果扫描到新的蓝牙设备
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "扫描到蓝牙设备: " + device.getName() + " - " + device.getAddress());
// 如果扫描到目标设备,则停止扫描
if (device.getName().equals("My Bluetooth Device")) {
targetDevice = device;
bluetoothAdapter.cancelDiscovery();
}
}
// 如果扫描结束
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if (targetDevice != null) {
// 开始连接目标设备
connectBluetoothDevice(targetDevice);
} else {
Log.d(TAG, "未找到目标设备");
}
}
}
};
private void connectBluetoothDevice(BluetoothDevice device) {
// 创建一个新的蓝牙连接
BluetoothSocket socket = null;
try {
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
Log.d(TAG, "连接蓝牙设备成功");
} catch (IOException e) {
Log.d(TAG, "连接蓝牙设备失败: " + e.getMessage());
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
Log.d(TAG, "关闭蓝牙连接失败: " + e.getMessage());
}
}
}
}
}
```
在上面的代码中,我们首先获取了Android设备的蓝牙适配器,并检查蓝牙是否已开启。如果蓝牙未开启,则请求用户打开蓝牙。如果蓝牙已开启,则调用`scanBluetoothDevice()`方法开始扫描蓝牙设备。
在`scanBluetoothDevice()`方法中,我们调用`bluetoothAdapter.startDiscovery()`方法开始扫描蓝牙设备,并注册一个广播接收器,用于监听扫描到的蓝牙设备。当扫描到目标设备时,我们将其保存在`targetDevice`变量中,并调用`bluetoothAdapter.cancelDiscovery()`方法停止扫描。
最后,在`connectBluetoothDevice()`方法中,我们使用`targetDevice`变量创建一个新的蓝牙连接,并尝试连接设备。如果连接成功,则打印出一条日志信息。如果连接失败,则打印出失败原因。
需要注意的是,上面的代码只是一个演示示例,实际应用中还需要处理一些异常情况,如蓝牙设备未找到、蓝牙连接超时等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)