WebApp快捷打包
蓝牙 BLE
转到模块插件

敬告:此 DEMO 演示为开放测试页面,仅用于开发者快速测试体验应用功能,请严格遵守开发者协议,了解更多

JS-SDK 引用方式:

♦ 普通网页 script 方式加载:下载最新版 jsBridge-v20240326.zip,请在页面上调用 jsBridge 接口之前引用 jsbridge-mini.js 库;

♦ js module 方式引用:npm install ym-jsbridge 具体请参考 npm package

♦ 要求 iPhone 4s / iOS6.0 以上,Android 4.3 以上;

♦ 需硬件支持 蓝牙 4.0,即 BLE(BluetoothLowEnergy);

getState 获取蓝牙设备状态

jsBridge.ble.getState(function(state) {
  alert(state);
});
/**
state 整数类型
1: poweredOn 设备开启状态 -- 可用状态
2: poweredOff 设备关闭状态
3: resetting 正在重置状态
4: unauthorized 设备未授权状态
5: unknown 初始的时候是未知的
6: unsupported 设备不支持
**/

requestPermission 请求蓝牙权限

//请在调用其他接口之前请求蓝牙权限
//获得权限之后再调用业务接口,否则可能出现闪退现象
jsBridge.ble.requestPermission(function(granted) {
  alert(granted ? "已授权" : "未授权");
});

requestEnable 请求启用蓝牙

//当蓝牙功能停用时,请求启用蓝牙
jsBridge.ble.requestEnable(function(succ) {
  if (!succ) {
    alert("失败");
  }
});

getBondedDevices 获取已配对的蓝牙设备

//需预先开启蓝牙功能,不然获取不到。
jsBridge.ble.getBondedDevices(function(data) {
  showData(data);
});

getName 获取本机蓝牙设备名称

jsBridge.ble.getName(function(name) {
  alert(name);
});

getPeripheral 获取当前扫描到的所有设备信息

waiting();
jsBridge.ble.getPeripheral(function(data) {
  showData(data);
});
/**
data 为 json 数组
[
  {
    uuid: "",  //字符串,设备 uuid(MAC 地址)
    name: "",  //字符串,设备名称
    rssi: -50  //数字,信号强度
  },
  ...
]
**/

isScanning 判断是否正在扫描中

jsBridge.ble.isScanning(function(yes) {
  alert(yes);
});
/**
yes 布尔类型,是否正在扫描中
**/

isConnected 判断与指定设备是否处于已连接状态

uuid:

jsBridge.ble.isConnected({
  uuid: "{{isConnected.mac}}"  //必须,字符串类型,目标设备的 uuid(MAC 地址)
}, function(yes) {
  alert(yes);
});
/**
yes 布尔类型,是否已连接
**/

scan 开始扫描搜索附近的蓝牙4.0设备

• 系统会不断的扫描更新附近的蓝牙4.0设备信息,反复通过回调函数通知您,也可以调用 getPeripheral 接口获取搜索到的设备;

• 调用此接口会先清空已经搜索到的设备;

• 最长连续扫描30秒钟会自动停止,调用 stopScan 手动停止扫描;

• 搜索到需要的设备后应尽快停止扫描,以降低电量消耗;

waiting();
jsBridge.ble.scan(function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 数组
[
  {
    uuid: "",  //字符串,设备 uuid(MAC 地址)
    name: "",  //字符串,设备名称
    rssi: -50  //数字,信号强度
  },
  ...
]
**/

stopScan 停止扫描搜索附近设备

jsBridge.ble.stopScan(function(yes) {
  alert(yes);
});
/**
yes 布尔类型,是否已停止
**/

connect 连接指定设备

uuid:

jsBridge.ble.connect({
  uuid: "{{connect.mac}}"  //必须,字符串,目标设备的 uuid(MAC 地址)
}, function(succ, err) {
  if (succ) {
    alert("连接成功");
  } else {
    alert(err);
  }
});

disconnect 断开与指定设备的连接

uuid:

jsBridge.ble.disconnect({
  uuid: "{{disconnect.mac}}"  //必须,字符串,目标设备的 uuid(MAC 地址)
}, function(succ, err) {
  if (succ) {
    alert("断开成功");
  } else {
    alert(err);
  }
});

discoverServices 获取指定设备的所有服务

uuid:

waiting();
jsBridge.ble.discoverServices({
  //必须,字符串,设备的 uuid(MAC 地址)
  uuid: "{{discoverServices.mac}}"
}, function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 字符串数组类型,返回此设备所有服务的 UUID (serviceUUID) 集
[
  "",  //字符串,服务 UUID (serviceUUID)
  ...
]
**/

discoverCharacteristics 获取指定设备服务的所有特征(Characteristic)

uuid:

serviceUUID:

waiting();
jsBridge.ble.discoverCharacteristics({
  //必须,字符串,设备的 uuid(MAC 地址)
  uuid       : "{{discoverCharacteristics.mac}}",
  //必须,字符串,服务 uuid,调用 discoverServices 可获取
  serviceUUID: "{{discoverCharacteristics.service}}"
}, function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 数组类型,返回所有特征集
[
  {
    uuid              : '', //字符串,设备 uuid(MAC 地址)
    serviceUUID       : '', //字符串,服务的 UUID 
    characteristicUUID: '', //字符串,特征的 UUID
    permissions       : '', //字符串,特征的权限,可能值:
                        //readable
                        //writeable
                        //readEncryptionRequired
                        //writeEncryptionRequired
                        //其他(数字代码串)
    properties         : '', //字符串,特征的属性,可能值:
                        //broadcast
                        //read
                        //writeWithoutResponse
                        //write
                        //notify
                        //indicate
                        //authenticatedSignedWrites
                        //extendedProperties
                        //notifyEncryptionRequired
                        //indicateEncryptionRequired
                        //其他(数字代码串)
    value              : '' //字符串,Hex 16进制值字符串(请自行转换为 byte 流)
  },
  ...
] 
**/

discoverDescriptors 获取指定设备特征的所有描述符(Descriptor)

uuid:

serviceUUID:

characteristicUUID:

waiting();
jsBridge.ble.discoverDescriptors({
  //必须,字符串,设备的 uuid(MAC 地址)
  uuid       : "{{discoverDescriptors.mac}}",
  //必须,字符串,服务 uuid,调用 discoverServices 可获取
  serviceUUID: "{{discoverDescriptors.service}}",
  //必须,字符串,特征 uuid,调用 discoverCharacteristics 可获取
  characteristicUUID: "{{discoverDescriptors.characteristic}}"
}, function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 数组类型,返回所有描述符集
[
  {
    uuid              : '', //字符串,设备 uuid(MAC 地址)
    serviceUUID       : '', //字符串,服务的 UUID 
    characteristicUUID: '', //字符串,特征的 UUID
    descriptorUUID    : '', //字符串,特征的 UUID
    value             : '', //字符串,Hex 16进制值字符串(请自行转换为 byte 流)
  },
  ...
] 
**/

setNotify 监听指定设备特征的数据回发

• 每有数据回发都会触发回调函数;

• 调用 disconnect 断开连接则自动停止监听;

uuid:

serviceUUID:

characteristicUUID:

waiting();
jsBridge.ble.setNotify({
  //必须,字符串,设备的 uuid(MAC 地址)
  uuid       : "{{setNotify.mac}}",
  //必须,字符串,服务 uuid,调用 discoverServices 可获取
  serviceUUID: "{{setNotify.service}}",
  //必须,字符串,特征 uuid,调用 discoverCharacteristics 可获取
  characteristicUUID: "{{setNotify.characteristic}}"
}, function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 对象,请参考 discoverCharacteristics 返回的 data 类型说明
**/

read 从设备上读取数据

• 每有数据回发都会触发回调函数;

uuid:

serviceUUID:

characteristicUUID:

waiting();
jsBridge.ble.read({
  //必须,字符串,设备的 uuid(MAC 地址)
  uuid       : "{{read.mac}}",
  //必须,字符串,服务 uuid,调用 discoverServices 可获取
  serviceUUID: "{{read.service}}",
  //必须,字符串,特征 uuid,调用 discoverCharacteristics 可获取
  characteristicUUID: "{{read.characteristic}}",
  //可选,字符串,可指定描述符 uuid,如果提供了则读取 descriptor,否则读取 characteristic
  descriptorUUID    : ""
}, function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 对象:
  读取 characteristic 请参考 discoverCharacteristics 返回的 data 类型说明
  读取 descriptor 请参考 discoverDescriptors 返回的 data 类型说明
**/

write 向设备写入数据

• 每有数据回发都会触发回调函数;

uuid:

serviceUUID:

characteristicUUID:

waiting();
jsBridge.ble.write({
  //必须,字符串,设备的 uuid(MAC 地址)
  uuid       : "{{write.mac}}",
  //必须,字符串,服务 uuid,调用 discoverServices 可获取
  serviceUUID: "{{write.service}}",
  //必须,字符串,特征 uuid,调用 discoverCharacteristics 可获取
  characteristicUUID: "{{write.characteristic}}",
  //可选,字符串,可指定描述符 uuid,如果提供了则写入 descriptor,否则写入 characteristic
  descriptorUUID    : "",
  //必须,请将需要写入的 byte 流转换为 Hex 16进制串
  value             : "48656C6C6F20776F726C64"
}, function(succ, data) {
  showData({
    succ: succ,
    data: data
  });
});
/**
data 为 json 对象:
  写入 characteristic 请参考 discoverCharacteristics 返回的 data 类型说明
  写入 descriptor 请参考 discoverDescriptors 返回的 data 类型说明
**/

附:蓝牙开发常用 UUID 表

Sample Services
0000180d-0000-1000-8000-00805f9b34fb  Heart Rate Service    
0000180a-0000-1000-8000-00805f9b34fb  Device Information Service    

Sample Characteristics
00002a37-0000-1000-8000-00805f9b34fb  Heart Rate Measurement    
00002a29-0000-1000-8000-00805f9b34fb  Manufacturer Name String    

GATT Services
00001800-0000-1000-8000-00805f9b34fb  GenericAccess    
00001801-0000-1000-8000-00805f9b34fb  GenericAttribute    

GATT Declarations
00002800-0000-1000-8000-00805f9b34fb  Primary Service    
00002801-0000-1000-8000-00805f9b34fb  Secondary Service    
00002802-0000-1000-8000-00805f9b34fb  Include    
00002803-0000-1000-8000-00805f9b34fb  Characteristic    

GATT Descriptors
00002900-0000-1000-8000-00805f9b34fb  Characteristic Extended Properties    
00002901-0000-1000-8000-00805f9b34fb  Characteristic User Description    
00002902-0000-1000-8000-00805f9b34fb  Client Characteristic Configuration    
00002903-0000-1000-8000-00805f9b34fb  Server Characteristic Configuration    
00002904-0000-1000-8000-00805f9b34fb  Characteristic Presentation Format    
00002905-0000-1000-8000-00805f9b34fb  Characteristic Aggregate Format    
00002906-0000-1000-8000-00805f9b34fb  Valid Range    
00002907-0000-1000-8000-00805f9b34fb  External Report Reference Descriptor    
00002908-0000-1000-8000-00805f9b34fb  Report Reference Descriptor    

GATT Characteristics
00002a00-0000-1000-8000-00805f9b34fb  Device Name    
00002a01-0000-1000-8000-00805f9b34fb  Appearance    
00002a02-0000-1000-8000-00805f9b34fb  Peripheral Privacy Flag    
00002a03-0000-1000-8000-00805f9b34fb  Reconnection Address    
00002a04-0000-1000-8000-00805f9b34fb  PPCP    
00002a05-0000-1000-8000-00805f9b34fb  Service Changed    

GATT Service UUIDs
00001802-0000-1000-8000-00805f9b34fb  Immediate Alert    
00001803-0000-1000-8000-00805f9b34fb  Link Loss    
00001804-0000-1000-8000-00805f9b34fb  Tx Power    
00001805-0000-1000-8000-00805f9b34fb  Current Time Service    
00001806-0000-1000-8000-00805f9b34fb  Reference Time Update Service    
00001807-0000-1000-8000-00805f9b34fb  Next DST Change Service    
00001808-0000-1000-8000-00805f9b34fb  Glucose    
00001809-0000-1000-8000-00805f9b34fb  Health Thermometer    
0000180a-0000-1000-8000-00805f9b34fb  Device Information    
0000180b-0000-1000-8000-00805f9b34fb  Network Availability    
0000180d-0000-1000-8000-00805f9b34fb  Heart Rate    
0000180e-0000-1000-8000-00805f9b34fb  Phone Alert Status Service    
0000180f-0000-1000-8000-00805f9b34fb  Battery Service    
00001810-0000-1000-8000-00805f9b34fb  Blood Pressure    
00001811-0000-1000-8000-00805f9b34fb  Alert Notification Service    
00001812-0000-1000-8000-00805f9b34fb  Human Interface Device    
00001813-0000-1000-8000-00805f9b34fb  Scan Parameters    
00001814-0000-1000-8000-00805f9b34fb  Running Speed and Cadence    
00001816-0000-1000-8000-00805f9b34fb  Cycling Speed and Cadence    
00001818-0000-1000-8000-00805f9b34fb  Cycling Power    
00001819-0000-1000-8000-00805f9b34fb  Location and Navigation    

GATT Characteristic UUIDs
00002a06-0000-1000-8000-00805f9b34fb  Alert Level    
00002a07-0000-1000-8000-00805f9b34fb  Tx Power Level    
00002a08-0000-1000-8000-00805f9b34fb  Date Time    
00002a09-0000-1000-8000-00805f9b34fb  Day of Week    
00002a0a-0000-1000-8000-00805f9b34fb  Day Date Time    
00002a0c-0000-1000-8000-00805f9b34fb  Exact Time 256    
00002a0d-0000-1000-8000-00805f9b34fb  DST Offset    
00002a0e-0000-1000-8000-00805f9b34fb  Time Zone    
00002a0f-0000-1000-8000-00805f9b34fb  Local Time Information    
00002a11-0000-1000-8000-00805f9b34fb  Time with DST    
00002a12-0000-1000-8000-00805f9b34fb  Time Accuracy    
00002a13-0000-1000-8000-00805f9b34fb  Time Source    
00002a14-0000-1000-8000-00805f9b34fb  Reference Time Information    
00002a16-0000-1000-8000-00805f9b34fb  Time Update Control Point    
00002a17-0000-1000-8000-00805f9b34fb  Time Update State    
00002a18-0000-1000-8000-00805f9b34fb  Glucose Measurement    
00002a19-0000-1000-8000-00805f9b34fb  Battery Level    
00002a1c-0000-1000-8000-00805f9b34fb  Temperature Measurement    
00002a1d-0000-1000-8000-00805f9b34fb  Temperature Type    
00002a1e-0000-1000-8000-00805f9b34fb  Intermediate Temperature    
00002a21-0000-1000-8000-00805f9b34fb  Measurement Interval    
00002a22-0000-1000-8000-00805f9b34fb  Boot Keyboard Input Report    
00002a23-0000-1000-8000-00805f9b34fb  System ID    
00002a24-0000-1000-8000-00805f9b34fb  Model Number String    
00002a25-0000-1000-8000-00805f9b34fb  Serial Number String    
00002a26-0000-1000-8000-00805f9b34fb  Firmware Revision String    
00002a27-0000-1000-8000-00805f9b34fb  Hardware Revision String    
00002a28-0000-1000-8000-00805f9b34fb  Software Revision String    
00002a29-0000-1000-8000-00805f9b34fb  Manufacturer Name String    
00002a2a-0000-1000-8000-00805f9b34fb  IEEE 11073-20601 Regulatory Certification Data List    
00002a2b-0000-1000-8000-00805f9b34fb  Current Time    
00002a31-0000-1000-8000-00805f9b34fb  Scan Refresh    
00002a32-0000-1000-8000-00805f9b34fb  Boot Keyboard Output Report    
00002a33-0000-1000-8000-00805f9b34fb  Boot Mouse Input Report    
00002a34-0000-1000-8000-00805f9b34fb  Glucose Measurement Context    
00002a35-0000-1000-8000-00805f9b34fb  Blood Pressure Measurement    
00002a36-0000-1000-8000-00805f9b34fb  Intermediate Cuff Pressure    
00002a37-0000-1000-8000-00805f9b34fb  Heart Rate Measurement    
00002a38-0000-1000-8000-00805f9b34fb  Body Sensor Location    
00002a39-0000-1000-8000-00805f9b34fb  Heart Rate Control Point    
00002a3e-0000-1000-8000-00805f9b34fb  Network Availability    
00002a3f-0000-1000-8000-00805f9b34fb  Alert Status    
00002a40-0000-1000-8000-00805f9b34fb  Ringer Control Point    
00002a41-0000-1000-8000-00805f9b34fb  Ringer Setting    
00002a42-0000-1000-8000-00805f9b34fb  Alert Category ID Bit Mask    
00002a43-0000-1000-8000-00805f9b34fb  Alert Category ID    
00002a44-0000-1000-8000-00805f9b34fb  Alert Notification Control Point    
00002a45-0000-1000-8000-00805f9b34fb  Unread Alert Status    
00002a46-0000-1000-8000-00805f9b34fb  New Alert    
00002a47-0000-1000-8000-00805f9b34fb  Supported New Alert Category    
00002a48-0000-1000-8000-00805f9b34fb  Supported Unread Alert Category    
00002a49-0000-1000-8000-00805f9b34fb  Blood Pressure Feature    
00002a4a-0000-1000-8000-00805f9b34fb  HID Information    
00002a4b-0000-1000-8000-00805f9b34fb  Report Map    
00002a4c-0000-1000-8000-00805f9b34fb  HID Control Point    
00002a4d-0000-1000-8000-00805f9b34fb  Report    
00002a4e-0000-1000-8000-00805f9b34fb  Protocol Mode    
00002a4f-0000-1000-8000-00805f9b34fb  Scan Interval Window    
00002a50-0000-1000-8000-00805f9b34fb  PnP ID    
00002a51-0000-1000-8000-00805f9b34fb  Glucose Feature    
00002a52-0000-1000-8000-00805f9b34fb  Record Access Control Point    
00002a53-0000-1000-8000-00805f9b34fb  RSC Measurement    
00002a54-0000-1000-8000-00805f9b34fb  RSC Feature    
00002a55-0000-1000-8000-00805f9b34fb  SC Control Point    
00002a5b-0000-1000-8000-00805f9b34fb  CSC Measurement    
00002a5c-0000-1000-8000-00805f9b34fb  CSC Feature    
00002a5d-0000-1000-8000-00805f9b34fb  Sensor Location    
00002a63-0000-1000-8000-00805f9b34fb  Cycling Power Measurement    
00002a64-0000-1000-8000-00805f9b34fb  Cycling Power Vector    
00002a65-0000-1000-8000-00805f9b34fb  Cycling Power Feature    
00002a66-0000-1000-8000-00805f9b34fb  Cycling Power Control Point    
00002a67-0000-1000-8000-00805f9b34fb  Location and Speed    
00002a68-0000-1000-8000-00805f9b34fb  Navigation    
00002a69-0000-1000-8000-00805f9b34fb  Position Quality    
00002a6a-0000-1000-8000-00805f9b34fb  LN Feature    
00002a6b-0000-1000-8000-00805f9b34fb  LN Control Point

这里只列出了一部分,更多请参考 bluetooth.com

Result: