当前位置:首页 > 后端开发 > 正文

如何实现Android蓝牙开发?

在安卓开发中使用Java实现蓝牙功能,需操作蓝牙适配器、搜索设备、配对连接,并通过BluetoothSocket建立通信通道进行数据传输,关键步骤包括权限申请、设备发现、RFCOMM连接管理及数据读写。

在安卓应用中实现蓝牙功能需要处理权限请求、设备发现、配对连接和数据传输等核心环节,以下为详细实现步骤(基于Android API 30+):


基础配置

  1. 添加权限(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" /> <!-- Android 12以下需要 --> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- Android 12+ -->
  2. 检查设备支持

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // 设备不支持蓝牙 }


启用蓝牙

// 检查蓝牙是否开启 if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } // onActivityResult中处理结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_ENABLE_BT) { if (resultCode == RESULT_OK) { // 蓝牙已开启 } else { // 用户拒绝 } } }


发现设备

  1. 注册广播接收器

    private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String deviceName = device.getName(); // 可能为null String deviceAddress = device.getAddress(); // 添加到设备列表 } } }; // 注册 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter);
  2. 开始/停止搜索

    // 开始搜索 if (bluetoothAdapter.startDiscovery()) { // 搜索成功启动 } // 停止搜索 bluetoothAdapter.cancelDiscovery();


设备配对与连接

  1. 配对设备(系统自动处理):

    // 发起配对请求(系统弹窗) device.createBond();

  2. 建立RFCOMM连接

    数据传输

    1. 获取输入/输出流

      InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream();
    2. 发送数据

      String message = "Hello Bluetooth"; outputStream.write(message.getBytes());
    3. 接收数据(子线程循环读取):

      byte[] buffer = new byte[1024]; int bytes; while (true) { bytes = inputStream.read(buffer); String receivedMessage = new String(buffer, 0, bytes); // 在主线程更新UI runOnUiThread(() -> textView.setText(receivedMessage)); }


    关键注意事项

    1. 权限适配

      • Android 12+:需动态请求BLUETOOTH_CONNECT和BLUETOOTH_SCAN
      • Android 6-11:需动态请求ACCESS_FINE_LOCATION ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    2. 线程管理

      • 所有阻塞操作(连接、传输)必须放在子线程
      • 使用Handler或runOnUiThread更新UI
    3. 资源释放

      socket.close(); // 断开连接时释放资源 unregisterReceiver(receiver); // 注销广播
    4. 最佳实践建议

      1. UUID选择:使用标准SPP UUID 00001101-0000-1000-8000-00805F9B34FB
      2. 连接稳定性
        • 添加超时机制(如socket.connect()前调用device.setPairingConfirmation(true))
        • 实现心跳包保持长连接
      3. 兼容性处理
        • 检查BluetoothAdapter.getProfileProxy()支持协议
        • 使用BluetoothClass区分设备类型


      引用说明

      本文代码基于Android官方蓝牙开发文档,适配最新权限模型,关键API参考:

      免责声明:实际开发需测试不同安卓版本和设备,低版本设备可能需降级API实现兼容性。

0