蓝牙
- 该部分集成在插件中
通过该API,我们可以连接蓝牙设备,并发送数据.
# 导包
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
获取已绑定的蓝牙设备
获取所有已经绑定过的蓝牙设备
- 方法
BleDevice.get_bonded_devices()
- 返回值
BleDevice[] 数组
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
devices = BleDevice.get_bonded_devices()
for device in devices:
print("已绑定的蓝牙设备:",device.name)
自动查找并连接设备
如果蓝牙已绑定过并且在线,尝试自动查找并连接.连接成功后返回BleDevice
- 方法
BleDevice.auto_connect(timeout = 2000)
- 返回值
已连接的 BleDevice ,可以通过该对象,发送数据,读取信息等.
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice.auto_connect(timeout = 2000)
if device :
print(f"找到蓝牙设备",device.name,device.mac_address)
蓝牙设备对象
构建一个蓝牙设备对象
可以通过mac地址构建一个蓝牙设备对象
- 方法
BleDevice(mac:str)
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
连接蓝牙
主动连接蓝牙,通过auto_connect方法获取的蓝牙对象,不需要调用该方法,除非已断开了连接.
- 方法
BleDevice(mac:str).connect(timeout=2000)
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
timeout | str | 是 | 开发者账号,目前需要时vip用户才可使用 |
- 返回值
bool返回值 True:连接成功,False:连接成功
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
res = device.connect()
if res:
print("蓝牙连接成功")
是否连接成功
判断蓝牙连接状态
- 方法
BleDevice(mac:str).is_connected()
- 返回值
bool返回值 True:连接成功,False:连接成功
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
device.connect()
res = device.is_connected()
if res:
print("蓝牙连接成功")
蓝牙名称
获取蓝牙名称
- 属性
BleDevice(mac:str).name
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
res = device.connect()
if res:
print("蓝牙名称:",res.name)
蓝牙mac地址
获取蓝牙mac地址
- 属性
BleDevice(mac:str).mac_address
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
res = device.connect()
if res:
print("蓝牙名称:",res.mac_address)
发送数据
向蓝牙设备写入数据
- 属性
BleDevice(mac:str).write(characteristic: str, data: Union[bytes, str], timeout=5000)
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
characteristic | str | 是 | 设备uuid接收地址 |
data | Union[bytes, str] | 是 | 要发送的数据,支持bytes或str,str会自动转换为utf8的bytes数据 |
timeout | int | 否 | 发送数据的超时时间 |
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
res = device.connect()
if res:
uid = "aeb5487e-58e1-9880-caf5-ae18361b26c9"
device.write(uid,"home")
读取数据
从蓝牙设备里读取数据
- 属性
BleDevice(mac:str).read(characteristic:str, timeout=5000)
- 参数
参数 | 类型 | 是否必填 | 说明 |
---|---|---|---|
characteristic | str | 是 | 设备uuid接收地址 |
timeout | int | 否 | 发送数据的超时时间 |
- 示例
from ascript.android import plug
plug.load("bluetooth:1.1")
from bluetooth import BleDevice
device = BleDevice("E4:B0:63:CA:D0:8E")
res = device.connect()
if res:
uid = "aeb5487e-58e1-9880-caf5-ae18361b26c9"
res = device.read(uid)
print(res)