Android Bluetooth之应用层开发总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mediatec/article/details/88127640

Android Bluetooth之应用层开发总结


本篇主要包括如下内容:

  • 接口类介绍
  • 使用注意事项

一、类介绍

1,BluetoothAdapter

表示本地蓝牙适配器(蓝牙无线装置)。 BluetoothAdapter 是所有蓝牙交互的入口点。 利用它可以发现其他蓝牙设备,查询绑定(配对)设备的列表,使用已知的 MAC 地址实例化 BluetoothDevice,以及创建 BluetoothServerSocket 以侦听来自其他设备的通信。

2,BluetoothDevice

表示远程蓝牙设备。利用它可以通过 BluetoothSocket 请求与某个远程设备建立连接,或查询有关该设备的信息,例如设备的名称、地址、类和绑定状态等。

3,BluetoothSocket

表示蓝牙套接字接口(与 TCP Socket 相似)。这是允许应用通过 InputStream 和 OutputStream 与其他蓝牙设备交换数据的连接点。

4,BluetoothServerSocket

表示用于侦听传入请求的开放服务器套接字(类似于 TCP ServerSocket)。 要连接两台 Android 设备,其中一台设备必须使用此类开放一个服务器套接字。 当一台远程蓝牙设备向此设备发出连接请求时, BluetoothServerSocket 将会在接受连接后返回已连接的 BluetoothSocket。

5,BluetoothClass

描述蓝牙设备的一般特征和功能。 这是一组只读属性,用于定义设备的主要和次要设备类及其服务。一般用作设备类型提示。

6,BluetoothProfile

表示蓝牙配置文件的接口。蓝牙配置文件是适用于设备间蓝牙通信的无线接口规范。

7,BluetoothHeadset

提供蓝牙耳机支持,以便与手机配合使用。 其中包括蓝牙耳机和免提(1.5 版)配置文件。

8,BluetoothA2dp

定义高质量音频如何通过蓝牙连接和流式传输,从一台设备传输到另一台设备。

9,BluetoothHealth

表示用于控制蓝牙服务的健康设备配置文件代理。

10,BluetoothHealthCallback

用于实现 BluetoothHealth 回调的抽象类。您必须扩展此类并实现回调方法,以接收关于应用注册状态和蓝牙通道状态变化的更新内容。

11,BluetoothHealthAppConfiguration

表示第三方蓝牙健康应用注册的应用配置,以便与远程蓝牙健康设备通信。

12,BluetoothProfile.ServiceListener

在 BluetoothProfile IPC 客户端连接到服务(即,运行特定配置文件的内部服务)或断开服务连接时向其发送通知的接口。

二、使用注意事项

1,蓝牙权限

android.permission.BLUETOOTH:应用清单中需要添加此权限才能执行蓝牙通信,包括请求、接受连接和数据传输等。

android.permission.BLUETOOTH_ADMIN:应用清单中需要添加此权限才能启动设备发现或操作蓝牙设置等操作。

2,配对和连接的区别

设备完成配对后,将会保存关于该设备的基本信息(例如设备名称、类和 MAC 地址),并且可使用相关接口读取这些信息。 被配对意味着两台设备知晓彼此的存在,具有可用于身份验证的共享链路密钥,并且能够与彼此建立加密连接。 被连接意味着设备当前共享一个 RFCOMM 通道,并且能够向彼此传输数据。

3,BluetoothAdapter

3.1 获取BluetoothAdapter

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
}

3.2 启用蓝牙(弹框,也可以透过BluetoothAdapter API调用)

if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new    
    Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

此时,会弹出对话框让用户选择确认。也可以直接调用BluetoothAdapter.enable()

3.3 查找配对的设备

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

3.4 发现设备

mBluetoothAdapter.startDiscovery();

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

3.4 启用可检测性

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

此时,会弹出对话框让用户选择确认。补充一点,对应无屏设备可以透过反射机制直接调用BluetoothAdapter.setScanMode()

3.5 连接设备(即socket编程)

服务端:

  1. 调用BluetoothAdapter.listenUsingRfcommWithServiceRecord(String, UUID) 返回BluetoothServerSocket
  2. 调用BluetoothServerSocket.accept()开始侦听连接请求,accetp()为阻塞调用。成功后,返回已连接的BluetoothSocket,紧接着便可以透过这个socket完成读或写操作。

客户端:

  1. 使用 BluetoothDevice.createRfcommSocketToServiceRecord(UUID) 获取 BluetoothSocket,UUID 必须与服务器设备在使用 listenUsingRfcommWithServiceRecord(String, UUID) 开放其 BluetoothServerSocket 时所用的 UUID 相匹配。
  2. 然后调用BluetoothSocket.connect()发起连接 ,connect为阻塞调用。

注意:因为有阻塞调用,为了不影响UI主线程,不可避免的牵涉到多线程编程。更多使用细节请参考另一篇文章:Android Bluetooth 之 BluetoothChat分析

参考:https://developer.android.google.cn/guide/topics/connectivity/bluetooth

猜你喜欢

转载自blog.csdn.net/mediatec/article/details/88127640