在 Android 开辟中,实现蓝牙毗连装备通常通过 BluetoothAdapter、BluetoothDevice、BluetoothSocket 等类来实现。你可以利用这些 API 来搜索蓝牙装备、配对装备以及通过蓝牙进行通信。
以下是实现蓝牙毗连装备的具体步调,包含装备扫描、毗连以及数据传输的 Java 代码示例。
1. 添加权限
起首,在 AndroidManifest.xml 中添加须要的权限和蓝牙设置:
- <uses-permission android:name="android.permission.BLUETOOTH" />
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- Android 12 及以上需要 -->
- <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <!-- Android 12 及以上需要 -->
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 扫描设备需要位置权限 -->
- <uses-feature android:name="android.hardware.bluetooth" />
- <uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />
复制代码 2. 初始化 BluetoothAdapter
BluetoothAdapter 是 Android 蓝牙功能的入口点。通过它可以启用蓝牙、搜索装备、建立毗连等。
- BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- // 检查设备是否支持蓝牙
- if (bluetoothAdapter == null) {
- Log.e("Bluetooth", "设备不支持蓝牙");
- } else {
- // 如果蓝牙未打开,请求用户打开蓝牙
- if (!bluetoothAdapter.isEnabled()) {
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- }
复制代码 3. 扫描装备
通过 BluetoothAdapter 进行装备扫描,可以找到附近可毗连的蓝牙装备。
- // 定义一个 BroadcastReceiver 来处理找到的设备
- private final BroadcastReceiver receiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (BluetoothDevice.ACTION_FOUND.equals(action)) {
- // 从 Intent 中获取 BluetoothDevice 对象
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- String deviceName = device.getName();
- String deviceAddress = device.getAddress(); // 设备的 MAC 地址
- Log.d("Bluetooth", "找到设备:" + deviceName + " [" + deviceAddress + "]");
- }
- }
- };
- // 开始扫描蓝牙设备
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- registerReceiver(receiver, filter); // 注册 BroadcastReceiver
- bluetoothAdapter.startDiscovery(); // 开始扫描
复制代码 在这里,扫描到的装备将通过 BroadcastReceiver 处理,并打印出装备名称和 MAC 地点。
4. 配对装备
在扫描到装备后,利用装备的 MAC 地点来配对和毗连蓝牙装备。
- // 假设已经通过扫描获得设备对象 device
- BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); // 使用设备的 MAC 地址获取设备对象
- // 检查是否已配对
- if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
- // 尝试配对
- device.createBond();
- } else {
- Log.d("Bluetooth", "设备已配对");
- }
复制代码 5. 建立蓝牙毗连
要与蓝牙装备通信,须要通过 BluetoothSocket 创建毗连。通常利用的是 RFCOMM 协议。
- UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // 标准串口服务 UUID
- BluetoothSocket bluetoothSocket = null;
- try {
- // 创建一个 BluetoothSocket 并连接到设备
- bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
- bluetoothSocket.connect(); // 连接设备
- Log.d("Bluetooth", "蓝牙连接成功");
- } catch (IOException e) {
- e.printStackTrace();
- Log.e("Bluetooth", "蓝牙连接失败");
- }
复制代码 这里的 MY_UUID 是一个标准的串口 UUID,用于创建与装备的毗连。
6. 数据传输
建立毗连后,可以利用输入输出流来进行数据的读写操作。
- // 获取输入输出流
- InputStream inputStream = null;
- OutputStream outputStream = null;
- try {
- inputStream = bluetoothSocket.getInputStream();
- outputStream = bluetoothSocket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 发送数据
- String message = "Hello from Android!";
- try {
- outputStream.write(message.getBytes());
- Log.d("Bluetooth", "数据发送成功");
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 接收数据
- byte[] buffer = new byte[1024];
- int bytes;
- try {
- bytes = inputStream.read(buffer);
- String receivedMessage = new String(buffer, 0, bytes);
- Log.d("Bluetooth", "收到数据: " + receivedMessage);
- } catch (IOException e) {
- e.printStackTrace();
- }
复制代码 7. 整理和释放资源
在利用完蓝牙后,要关闭 BluetoothSocket 并释放资源。
- try {
- if (bluetoothSocket != null) {
- bluetoothSocket.close(); // 关闭连接
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
复制代码 完整示例代码:
- public class BluetoothActivity extends AppCompatActivity {
- private BluetoothAdapter bluetoothAdapter;
- private BluetoothSocket bluetoothSocket;
- private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private final String deviceAddress = "00:11:22:33:44:55"; // 替换为目标设备的 MAC 地址
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
-
- if (bluetoothAdapter == null) {
- Log.e("Bluetooth", "设备不支持蓝牙");
- return;
- }
- if (!bluetoothAdapter.isEnabled()) {
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- // 开始扫描设备
- startDeviceDiscovery();
- // 配对设备并连接
- BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
- try {
- bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
- bluetoothSocket.connect();
- Log.d("Bluetooth", "蓝牙连接成功");
- } catch (IOException e) {
- e.printStackTrace();
- Log.e("Bluetooth", "蓝牙连接失败");
- }
- // 发送数据
- sendData("Hello from Android!");
- // 接收数据
- receiveData();
- }
- private void startDeviceDiscovery() {
- // 扫描设备的逻辑
- }
- private void sendData(String message) {
- try {
- OutputStream outputStream = bluetoothSocket.getOutputStream();
- outputStream.write(message.getBytes());
- Log.d("Bluetooth", "数据发送成功");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void receiveData() {
- try {
- InputStream inputStream = bluetoothSocket.getInputStream();
- byte[] buffer = new byte[1024];
- int bytes = inputStream.read(buffer);
- String receivedMessage = new String(buffer, 0, bytes);
- Log.d("Bluetooth", "收到数据: " + receivedMessage);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 总结:
- 蓝牙适配器:通过 BluetoothAdapter 查抄蓝牙状态并进行操作。
- 装备扫描:通过 BluetoothAdapter.startDiscovery() 和 BroadcastReceiver 监听发现装备。
- 装备配对:通过 BluetoothDevice.createBond() 实现配对。
- 毗连装备:通过 BluetoothSocket 建立毗连。
- 数据传输:通过输入输出流实现数据发送与接收。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |