android.bluetooth
Provides classes that manage Bluetooth functionality, such as scanning for devices, connecting with devices, and managing data transfer between devices. The Bluetooth API supports both "Classic Bluetooth" and Bluetooth Low Energy.
For more information about Classic Bluetooth, see the Bluetooth guide. For more information about Bluetooth Low Energy, see the Bluetooth Low Energy (BLE) guide.
The Bluetooth APIs let applications:
- Scan for other Bluetooth devices (including BLE devices).
- //扫描其他的蓝牙设备包括使用BLE的蓝牙设备
- Query the local Bluetooth adapter for paired Bluetooth devices.
- //查询本地的蓝牙适配器以判断设备是否适合
- Establish RFCOMM channels/sockets.
- //建立串行通道或者socket
- Connect to specified sockets on other devices.
- 可以使用指定的协议与其他设备链接
- Transfer data to and from other devices.
- //实现数据的传输和处理
- Communicate with BLE devices, such as proximity sensors, heart rate monitors, fitness devices, and so on.
- //与其他BLE设备进行通信,包括血压仪等等
- Act as a GATT client or a GATT server (BLE).
- //可以作为GATT的客户端和服务端
To perform Bluetooth communication using these APIs, an application must declare the BLUETOOTH
permission. Some additional functionality, such as requesting device discovery, also requires the BLUETOOTH_ADMIN
permission.
//设置相应的蓝牙权限
Note: Not all Android-powered devices provide Bluetooth functionality.
//Android4.3即API18支持蓝牙4.0
蓝牙API中的接口
BluetoothAdapter.LeScanCallback 这个回调用以展示蓝牙扫描后的结果
使用到这个回调的两个方法(查找周围设备的方法)
startLeScan(LeScanCallback)
startLeScan(UUID[], LeScanCallback)
- 关于方法1
- This method was deprecated in API level 21.
usestartScan(List, ScanSettings, ScanCallback)
instead.
-
Starts a scan for Bluetooth LE devices.
Results of the scan are reported using the
onLeScan(BluetoothDevice, int, byte[])
callback.Requires
BLUETOOTH_ADMIN
permission.
This method was deprecated in API level 21.
use startScan(List, ScanSettings, ScanCallback)
instead.
这个方法在API21中被替换至下方法。
Starts a scan for Bluetooth LE devices, looking for devices that advertise given services.
Devices which advertise all specified services are reported using the onLeScan(BluetoothDevice, int, byte[])
callback.
Requires BLUETOOTH_ADMIN
permission.
serviceUuids UUID
: Array of services to look for目标设备
callback :BluetoothAdapter.LeScanCallback
: the callback LE scan results are delivered
该方法的返回值类型为TRUE或者FALSe
关于这个方法,是一个抽象的方法
onLeScan( BluetoothDevice device, int rssi, byte[] scanRecord)
startLeScan(BluetoothAdapter.LeScanCallback)
function.
public abstract void onLeScan (BluetoothDevice device, int rssi, byte[] scanRecord)
Callback reporting an LE device found during a device scan initiated by thestartLeScan(BluetoothAdapter.LeScanCallback)
function.
device BluetoothDevice
: Identifies the remote device
rssi int
: The RSSI value for the remote device as reported by the Bluetooth hardware. 0 if no RSSI value is available.
scanRecord byte
: The content内容 of the advertisement record 记录offered by the remote device.