蓝牙 ble -九游会国际

webapp快捷打包
蓝牙 ble
转到模块插件

敬告:此 demo 演示为开放测试页面,仅用于开发者快速测试体验应用功能,请严格遵守开发者协议,九游会国际-九游会真人第一品牌

js-sdk 引用方式:

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

♦ js module 方式引用:npm install ym-jsbridge

♦ 要求 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 判断与指定设备是否处于已连接状态

jsbridge.ble.isconnected({
  uuid: "e4:19:c1:bc:0b:60"  //必须,字符串类型,目标设备的 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 连接指定设备

jsbridge.ble.connect({
  uuid: "e4:19:c1:bc:0b:60"  //必须,字符串,目标设备的 uuid(mac 地址)
}, function(succ, err) {
  if (succ) {
    alert("连接成功");
  } else {
    alert(err);
  }
});

disconnect 断开与指定设备的连接

jsbridge.ble.disconnect({
  uuid: "e4:19:c1:bc:0b:60"  //必须,字符串,目标设备的 uuid(mac 地址)
}, function(succ, err) {
  if (succ) {
    alert("断开成功");
  } else {
    alert(err);
  }
});

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

waiting();
jsbridge.ble.discoverservices({
  //必须,字符串,设备的 uuid(mac 地址)
  uuid: "e4:19:c1:bc:0b:60"
}, function(succ, data) {
  showdata({
    succ: succ,
    data: data
  });
});
/**
data 为 json 字符串数组类型,返回此设备所有服务的 uuid (serviceuuid) 集
[
  "",  //字符串,服务 uuid (serviceuuid)
  ...
]
**/

discovercharacteristics 获取指定设备服务的所有特征(characteristic)

waiting();
jsbridge.ble.discovercharacteristics({
  //必须,字符串,设备的 uuid(mac 地址)
  uuid       : "e4:19:c1:bc:0b:60",
  //必须,字符串,服务 uuid,调用 discoverservices 可获取
  serviceuuid: "00001800-0000-1000-8000-00805f9b34fb"
}, 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)

waiting();
jsbridge.ble.discoverdescriptors({
  //必须,字符串,设备的 uuid(mac 地址)
  uuid       : "e4:19:c1:bc:0b:60",
  //必须,字符串,服务 uuid,调用 discoverservices 可获取
  serviceuuid: "00001800-0000-1000-8000-00805f9b34fb",
  //必须,字符串,特征 uuid,调用 discovercharacteristics 可获取
  characteristicuuid: "00002a00-0000-1000-8000-00805f9b34fb"
}, 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 断开连接则自动停止监听;

waiting();
jsbridge.ble.setnotify({
  //必须,字符串,设备的 uuid(mac 地址)
  uuid       : "e4:19:c1:bc:0b:60",
  //必须,字符串,服务 uuid,调用 discoverservices 可获取
  serviceuuid: "00001800-0000-1000-8000-00805f9b34fb",
  //必须,字符串,特征 uuid,调用 discovercharacteristics 可获取
  characteristicuuid: "00002a00-0000-1000-8000-00805f9b34fb"
}, function(succ, data) {
  showdata({
    succ: succ,
    data: data
  });
});
/**
data 为 json 对象,请参考 discovercharacteristics 返回的 data 类型说明
**/

read 从设备上读取数据

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

waiting();
jsbridge.ble.read({
  //必须,字符串,设备的 uuid(mac 地址)
  uuid       : "e4:19:c1:bc:0b:60",
  //必须,字符串,服务 uuid,调用 discoverservices 可获取
  serviceuuid: "00001800-0000-1000-8000-00805f9b34fb",
  //必须,字符串,特征 uuid,调用 discovercharacteristics 可获取
  characteristicuuid: "00002a00-0000-1000-8000-00805f9b34fb",
  //可选,字符串,可指定描述符 uuid,如果提供了则读取 descriptor,否则读取 characteristic
  descriptoruuid    : ""
}, function(succ, data) {
  showdata({
    succ: succ,
    data: data
  });
});
/**
data 为 json 对象:
  读取 characteristic 请参考 discovercharacteristics 返回的 data 类型说明
  读取 descriptor 请参考 discoverdescriptors 返回的 data 类型说明
**/

write 向设备写入数据

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

waiting();
jsbridge.ble.write({
  //必须,字符串,设备的 uuid(mac 地址)
  uuid       : "e4:19:c1:bc:0b:60",
  //必须,字符串,服务 uuid,调用 discoverservices 可获取
  serviceuuid: "00001800-0000-1000-8000-00805f9b34fb",
  //必须,字符串,特征 uuid,调用 discovercharacteristics 可获取
  characteristicuuid: "00002a00-0000-1000-8000-00805f9b34fb",
  //可选,字符串,可指定描述符 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

这里只列出了一部分,更多请参考 。

result:

网站地图