Skip to content

04 经典蓝牙模块

Xiaomi-Yule edited this page Jul 5, 2021 · 2 revisions

miot/device/bluetooth

经典蓝牙设备操作类 sdk 10023 仅支持Android。iOS因为苹果的MFI认证(麻烦且收益低,暂时生态链公司及其它iot合作公司使用),暂没有经典蓝牙设备可以和iPhone通讯。 本文件提供了经典蓝牙设备的创建,连接,读写,断连。此处不再提供example。

Export: public
Doc_name: 经典蓝牙模块
Doc_index: 4
Doc_directory: bluetooth
Since: 10023


module.exports ⏏

经典蓝牙设备操作类

Kind: Exported interface


module.exports.create() ⇒ [ 'Promise' ].<any>

初始化经典蓝牙,返回的数据没有实际作用, 执行到catch表示初始化失败。

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then,失败进入catch
Since: 10023


module.exports.connectSocket(macAddress, transport) ⇒ [ 'Promise' ].<any>

根据device 的mac 地址,与中心设备建立socket 链接, 返回的数据没有实际作用, 执行到catch表示连接失败

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then,失败进入catch
Since: 10023

Param Type Description
macAddress string 中心设备mac地址。格式类似:"AA:BB:CC:DD:EE:FF"
transport string 连接中心设备的相应服务的UUID,格式类似:"1000000-0000-0000-000000000001"

module.exports.disconnectSocket() ⇒ [ 'Promise' ].<any>

断开与中心设备的socket连接, 返回的数据没有实际作用, 执行到catch表示断开连接失败

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then,失败进入catch
Since: 10023


module.exports.write(data) ⇒ [ 'Promise' ].<any>

向蓝牙设备写入数据, 返回的数据没有实际作用, 执行到catch表示写失败

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then,失败进入catch
Since: 10023

Param Type
data string

module.exports.prepareBluetoothProfile(profile) ⇒ [ 'Promise' ].<any>

事先准备要需要的BluetoothProfile, 具体的类型是profile, 具体的数值参考Android Api: BluetoothProfile.HEADSET,BluetoothProfile.A2DP HEADSET = 1;A2DP = 2;HEALTH = 3;

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then, 返回对应的profile,失败进入catch
Since: 10023

Param Type
profile int

module.exports.connectBluetoothProfile(macAddress, profile) ⇒ [ 'Promise' ].<any>

连接类型为profile(比如BluetoothProfile.HEADSET,BluetoothProfile.A2DP) 的蓝牙服务

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then, 返回值没有实际作用,失败进入catch
Since: 10023

Param Type Description
macAddress string 需要查询的设备macAddress
profile int BluetoothProfile 接口类的类型( BluetoothProfile.HEADSET,BluetoothProfile.A2DP等)

module.exports.disconnectBluetoothProfile(macAddress, profile) ⇒ [ 'Promise' ].<any>

断开类型为profile(比如BluetoothProfile.HEADSET,BluetoothProfile.A2DP) 的蓝牙服务

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then, 返回值没有实际作用,失败进入catch
Since: 10023

Param Type
macAddress string
profile int

module.exports.getBluetoothProfileState(macAddress, profile) ⇒ [ 'Promise' ].<any>

获取类型为profile的BluetoothProfile的当前状态, 返回值有四个选项,参考android api : BluetoothProfile.STATE_DISCONNECTED等 STATE_DISCONNECTED = 0; STATE_CONNECTING = 1;STATE_CONNECTED = 2;TATE_DISCONNECTING = 3;

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then, 返回值{"state": 0},失败进入catch
Since: 10023

Param Type
macAddress string
profile int

module.exports.destroy() ⇒ [ 'Promise' ].<any>

销毁蓝牙服务

Kind: static method of module.exports
Returns: [ 'Promise' ].<any> - 成功进入then,失败进入catch
Since: 10023


module.exports~ClassicBluetoothEvent : object

经典蓝牙事件名集合

Kind: inner namespace of module.exports


ClassicBluetoothEvent.classicBlueBondStateChanged

BondStateChange 状态改变事件 返回的数据格式为:{"macAddress": "xxx", "state":xxx} state 取值为:BOND_BONDING = 11;BOND_NONE = 10;BOND_BONDED = 12;

Kind: static property of ClassicBluetoothEvent


ClassicBluetoothEvent.classicBlueConnectionStateChanged

经典蓝牙连接状态改变事件 返回的数据格式为:{"macAddress": "xxx", "state":xxx} state 取值为:DISCONNECTED = 0;CONNECTING = 1;CONNECTED = 2;DISCONNECTING = 3;NO_STATE = 4;

Kind: static property of ClassicBluetoothEvent


ClassicBluetoothEvent.classicBlueReceivedData

收到数据事件 返回的数据格式为:{"macAddress": "xxx", "data":"xxx"}

Kind: static property of ClassicBluetoothEvent


Clone this wiki locally