蓝牙API总结
BluetoothProfile(interface)
主要常量值:
表示目前GAP层的状态
STATE_CONNECTED
STATE_CONNECTING
STATE_DISCONNECTED
STATE_DISCONNECTING
公共方法(全是抽象的):
getConnectedDevices
返回值 list<BluetoothDevice>
获取已连接到的设备
getConnectionState(BluetoothDevice device)
返回值 获得某设备的连接状态
getDevicesMatchingConnectionStates(int[] states)
返回值 获得这一状态的设备列表
BluetoothAdapter.LeScanCallback(interface)
主要方法:
void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
回调函数,当LeScanCallback作为startLeScan函数参数时,宣告已经有一个函数被扫描到了
参数:
device 识别远程设备
rssi 如果蓝牙硬件能识别RSSI,则这个是它设别出的rssi,否则为0
scanRecord 远程设备广播的信息
BluetoothProfile.ServiceListener
主要方法:
onServiceConnected(int profile, BluetoothProfile proxy)
onServiceDisconnected(int profile)
当主机连接上服务端或者与其断开连接时的回调
第一个参数是识别码,第二个是profile 但是好像只能是音频模板,耳机模板,健康包模板之一
BluetoothAdapter public final
介绍:代表了本地蓝牙适配器。它能让你去执行一些底层蓝牙任务,例如启动发现设备,查询已配对设备,
用一个已知地址去建出一个设备模型,创建一个BluetoothServerSocket去监听来自其他设备的连接请求,或是
启动一个扫描动作。为了去得到一个蓝牙适配器,如果运行在Android4.3及其以上,可以使用getAdapter直接
获得。本质上来说,这也是你所有蓝牙行为的基本点。一旦你获得了本地适配器,你就能通过getBondedDevices()
得到一串已配对或者绑定的蓝牙设备,或者通过startDiscovery()去扫描蓝牙,或者创建getBondedDevices()
,通过listenUsingRfcommWithServiceRecord(String, UUID)去监听要来的连接请求,或者是去发起一段扫描。
这个类是线程安全的。
需要 BLUETOOTH 和 BLUETOOTH_ADMIN 权限
内嵌的类
BluetoothAdapter.LeScanCallback
回调,用来传递扫描结果
常量值
String ACTION_CONNECTION_STATE_CHANGED
意图 把本地适配器连接状态广播给远程profile
String ACTION_DISCOVERY_FINISHED
Broadcast Action: 已经完成了发现任务
String ACTION_DISCOVERY_STARTED
Broadcast Action: 发现任务开始
String ACTION_LOCAL_NAME_CHANGED
Broadcast Action: 本地适配器更换名字
String ACTION_REQUEST_DISCOVERABLE
Activity Action: 请求可发现模式
String ACTION_REQUEST_ENABLE
Activity Action: 请求打开蓝牙
String ACTION_SCAN_MODE_CHANGED
Broadcast Action: 扫描模式已改变
String ACTION_STATE_CHANGED
Broadcast Action: 本地蓝牙适配器的状态已改变
int ERROR
错误型号
String EXTRA_CONNECTION_STATE
ACTION_CONNECTION_STATE_CHANGED 附加物 代表连接状态
String EXTRA_DISCOVERABLE_DURATION
ACTION_REQUEST_DISCOVERABLE 附加物 请求可发现几秒钟.
String EXTRA_LOCAL_NAME
ACTION_LOCAL_NAME_CHANGED 适配器名字
String EXTRA_PREVIOUS_CONNECTION_STATE
ACTION_CONNECTION_STATE_CHANGED 附加物 代表之前的状态
String EXTRA_PREVIOUS_SCAN_MODE
ACTION_SCAN_MODE_CHANGED 附加物 代表之前的扫描模式
String EXTRA_PREVIOUS_STATE
ACTION_STATE_CHANGED 附加物 之前的功率状态
String EXTRA_SCAN_MODE
ACTION_SCAN_MODE_CHANGED 附加物 代表现在的扫描模式
String EXTRA_STATE
ACTION_STATE_CHANGED 附加物 代表现在的功率状态
int SCAN_MODE_CONNECTABLE
inquiry scan失能, page scan使能.
int SCAN_MODE_CONNECTABLE_DISCOVERABLE
inquiry scan使能, page scan使能.
int SCAN_MODE_NONE
inquiry scan失能, page scan失能.
int STATE_CONNECTED
已连接状态
int STATE_CONNECTING
连接状态
int STATE_DISCONNECTED
断开连接状态
int STATE_DISCONNECTING
正在断开连接状态
int STATE_OFF
适配器已关
int STATE_ON
适配器已打开
int STATE_TURNING_ON
适配器正在打开
int STATE_TURNING_OFF
适配器正在关闭
主要方法:
取消扫描:boolean cancelDiscovery()
判断一个地址是否格式合法:static boolean checkBluetoothAddress(String address)
取消使用proxy(健康包等代理): void closeProfileProxy(int profile, BluetoothProfile proxy)
断开连接,停止蓝牙系统,关闭蓝牙电源 boolean disable()
同上 使能有关蓝牙的一切: boolean enable()
返回本地蓝牙地址String getAddress()
返回BluetoothLeAdvertiser去执行广播行为 BluetoothLeAdvertiser getBluetoothLeAdvertiser()
返回BluetoothLeScannerBluetoothLeScanner去执行扫描操作 getBluetoothLeScanner()
返回已绑定的设备列表Set<BluetoothDevice> getBondedDevices()
返回默认的蓝牙适配器static BluetoothAdapter getDefaultAdapter()
返回广播最大字节数int getLeMaximumAdvertisingDataLength()
本地蓝牙名字String getName()
目前profile的连接状态:int getProfileConnectionState(int profile)
返回一个与当前profile相关的proxy :boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)
通过地址去得到一个蓝牙设备:BluetoothDevice getRemoteDevice(byte[] address)
获取本地扫描模式:int getScanMode()
获取本地适配器模式(功率?):int getState()
判断是否在进行发现boolean isDiscovering()
判断蓝牙是否能使用:boolean isEnabled()
ble是否支持的判断:
boolean isLe2MPhySupported()
boolean isLeCodedPhySupported()
某种广播是否支持的判断:
boolean isLeExtendedAdvertisingSupported()
boolean isLePeriodicAdvertisingSupported()
boolean isMultipleAdvertisementSupported()
不明觉厉:
boolean isOffloadedFilteringSupported()
boolean isOffloadedScanBatchingSupported()
BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid)
设置蓝牙名字:boolean setName(String name)
开始发现:boolean startDiscovery()
开启扫描:boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)
这个方法level21不能用,要用 startScan(List, ScanSettings, ScanCallback),并且在onLeScan里接受回调
停止扫描:void stopLeScan(BluetoothAdapter.LeScanCallback callback)
这个方法level21不能用,要用stopScan(ScanCallback).
BluetoothProfile(interface)
主要常量值:
表示目前GAP层的状态
STATE_CONNECTED
STATE_CONNECTING
STATE_DISCONNECTED
STATE_DISCONNECTING
公共方法(全是抽象的):
getConnectedDevices
返回值 list<BluetoothDevice>
获取已连接到的设备
getConnectionState(BluetoothDevice device)
返回值 获得某设备的连接状态
getDevicesMatchingConnectionStates(int[] states)
返回值 获得这一状态的设备列表
BluetoothAdapter.LeScanCallback(interface)
主要方法:
void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
回调函数,当LeScanCallback作为startLeScan函数参数时,宣告已经有一个函数被扫描到了
参数:
device 识别远程设备
rssi 如果蓝牙硬件能识别RSSI,则这个是它设别出的rssi,否则为0
scanRecord 远程设备广播的信息
BluetoothProfile.ServiceListener
主要方法:
onServiceConnected(int profile, BluetoothProfile proxy)
onServiceDisconnected(int profile)
当主机连接上服务端或者与其断开连接时的回调
第一个参数是识别码,第二个是profile 但是好像只能是音频模板,耳机模板,健康包模板之一
BluetoothAdapter public final
介绍:代表了本地蓝牙适配器。它能让你去执行一些底层蓝牙任务,例如启动发现设备,查询已配对设备,
用一个已知地址去建出一个设备模型,创建一个BluetoothServerSocket去监听来自其他设备的连接请求,或是
启动一个扫描动作。为了去得到一个蓝牙适配器,如果运行在Android4.3及其以上,可以使用getAdapter直接
获得。本质上来说,这也是你所有蓝牙行为的基本点。一旦你获得了本地适配器,你就能通过getBondedDevices()
得到一串已配对或者绑定的蓝牙设备,或者通过startDiscovery()去扫描蓝牙,或者创建getBondedDevices()
,通过listenUsingRfcommWithServiceRecord(String, UUID)去监听要来的连接请求,或者是去发起一段扫描。
这个类是线程安全的。
需要 BLUETOOTH 和 BLUETOOTH_ADMIN 权限
内嵌的类
BluetoothAdapter.LeScanCallback
回调,用来传递扫描结果
常量值
String ACTION_CONNECTION_STATE_CHANGED
意图 把本地适配器连接状态广播给远程profile
String ACTION_DISCOVERY_FINISHED
Broadcast Action: 已经完成了发现任务
String ACTION_DISCOVERY_STARTED
Broadcast Action: 发现任务开始
String ACTION_LOCAL_NAME_CHANGED
Broadcast Action: 本地适配器更换名字
String ACTION_REQUEST_DISCOVERABLE
Activity Action: 请求可发现模式
String ACTION_REQUEST_ENABLE
Activity Action: 请求打开蓝牙
String ACTION_SCAN_MODE_CHANGED
Broadcast Action: 扫描模式已改变
String ACTION_STATE_CHANGED
Broadcast Action: 本地蓝牙适配器的状态已改变
int ERROR
错误型号
String EXTRA_CONNECTION_STATE
ACTION_CONNECTION_STATE_CHANGED 附加物 代表连接状态
String EXTRA_DISCOVERABLE_DURATION
ACTION_REQUEST_DISCOVERABLE 附加物 请求可发现几秒钟.
String EXTRA_LOCAL_NAME
ACTION_LOCAL_NAME_CHANGED 适配器名字
String EXTRA_PREVIOUS_CONNECTION_STATE
ACTION_CONNECTION_STATE_CHANGED 附加物 代表之前的状态
String EXTRA_PREVIOUS_SCAN_MODE
ACTION_SCAN_MODE_CHANGED 附加物 代表之前的扫描模式
String EXTRA_PREVIOUS_STATE
ACTION_STATE_CHANGED 附加物 之前的功率状态
String EXTRA_SCAN_MODE
ACTION_SCAN_MODE_CHANGED 附加物 代表现在的扫描模式
String EXTRA_STATE
ACTION_STATE_CHANGED 附加物 代表现在的功率状态
int SCAN_MODE_CONNECTABLE
inquiry scan失能, page scan使能.
int SCAN_MODE_CONNECTABLE_DISCOVERABLE
inquiry scan使能, page scan使能.
int SCAN_MODE_NONE
inquiry scan失能, page scan失能.
int STATE_CONNECTED
已连接状态
int STATE_CONNECTING
连接状态
int STATE_DISCONNECTED
断开连接状态
int STATE_DISCONNECTING
正在断开连接状态
int STATE_OFF
适配器已关
int STATE_ON
适配器已打开
int STATE_TURNING_ON
适配器正在打开
int STATE_TURNING_OFF
适配器正在关闭
主要方法:
取消扫描:boolean cancelDiscovery()
判断一个地址是否格式合法:static boolean checkBluetoothAddress(String address)
取消使用proxy(健康包等代理): void closeProfileProxy(int profile, BluetoothProfile proxy)
断开连接,停止蓝牙系统,关闭蓝牙电源 boolean disable()
同上 使能有关蓝牙的一切: boolean enable()
返回本地蓝牙地址String getAddress()
返回BluetoothLeAdvertiser去执行广播行为 BluetoothLeAdvertiser getBluetoothLeAdvertiser()
返回BluetoothLeScannerBluetoothLeScanner去执行扫描操作 getBluetoothLeScanner()
返回已绑定的设备列表Set<BluetoothDevice> getBondedDevices()
返回默认的蓝牙适配器static BluetoothAdapter getDefaultAdapter()
返回广播最大字节数int getLeMaximumAdvertisingDataLength()
本地蓝牙名字String getName()
目前profile的连接状态:int getProfileConnectionState(int profile)
返回一个与当前profile相关的proxy :boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)
通过地址去得到一个蓝牙设备:BluetoothDevice getRemoteDevice(byte[] address)
获取本地扫描模式:int getScanMode()
获取本地适配器模式(功率?):int getState()
判断是否在进行发现boolean isDiscovering()
判断蓝牙是否能使用:boolean isEnabled()
ble是否支持的判断:
boolean isLe2MPhySupported()
boolean isLeCodedPhySupported()
某种广播是否支持的判断:
boolean isLeExtendedAdvertisingSupported()
boolean isLePeriodicAdvertisingSupported()
boolean isMultipleAdvertisementSupported()
不明觉厉:
boolean isOffloadedFilteringSupported()
boolean isOffloadedScanBatchingSupported()
BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid)
设置蓝牙名字:boolean setName(String name)
开始发现:boolean startDiscovery()
开启扫描:boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)
这个方法level21不能用,要用 startScan(List, ScanSettings, ScanCallback),并且在onLeScan里接受回调
停止扫描:void stopLeScan(BluetoothAdapter.LeScanCallback callback)
这个方法level21不能用,要用stopScan(ScanCallback).