支持:iOS和Android4.3+

安装:Cordova

PhoneGap

API:

scan 扫描:

蓝牙扫描的方法,第二个参数10指的扫描时间,单位是秒,device是扫描的设备

ble.scan(services, seconds, success, failure);

参数:

services: 发现的服务列表或[ ]中所有设备

seconds: 时间

success: 成功回调

failure: 失败回调

example:

ble.scan([], 5, function(device) {

console.log(JSON.stringify(device));

}, failure);

startScan 开始扫描:

ble.startScan(services, success, failure);

参数:

services: 发现的服务列表或[]中所有设备

success: 成功回调

failure: 失败回调

example:

ble.startScan([], function(device) {

console.log(JSON.stringify(device));

}, failure);

setTimeout(ble.stopScan,

5000,

function() { console.log("Scan complete"); },

function() { console.log("stopScan failed"); }

);

startScanWithOptions 指定扫描:

ble.startScanWithOptions(services, options, success, failure);

参数:

services: 发现的服务列表或[]中所有设备

options: 一个对象指定一组键值对,现在支持的选项如下:

true:如果复制设备应该报道,

false:(默认)如果设备只报告一次。(可选)

success: 成功回调

failure: 失败回调

example:

ble.startScan([],

{ reportDuplicates: true }

function(device) {

console.log(JSON.stringify(device));

},

failure);

setTimeout(ble.stopScan,

5000,

function() { console.log("Scan complete"); },

function() { console.log("stopScan failed"); }

);

stopScan 停止扫描:

ble.stopScan(success, failure);

参数:

success: 成功回调

failure: 失败回调

example:

ble.startScan([], function(device) {

console.log(JSON.stringify(device));

}, failure);

setTimeout(ble.stopScan,

5000,

function() { console.log("Scan complete"); },

function() { console.log("stopScan failed"); }

);

/* Alternate syntax

setTimeout(function() {

ble.stopScan(

function() { console.log("Scan complete"); },

function() { console.log("stopScan failed"); }

);

}, 5000);

*/

connect 连接:

连接蓝牙的方法,第一个参数是你扫描到的设备的id,后面的是成功和失败的回调

ble.connect(device_id, connectSuccess, connectFailure);

参数:

device_id: uuid或者设备的mac地址

connectSuccess: 连接成功回调

connectFailure: 连接失败回调

example:

$scope.connectFun=function(device){

ble.connect(device.id, $scope.onConnected, $scope.onError);

}

disconnect 断开连接:

ble.disconnect(device_id, [success], [failure]);

参数:

device_id: UUID 或者设备 MAC 地址

success: 成功回调

failure: 失败回调

example:

ble.disconnect(device.id, function(){

//do something

}, function(){

//do something

});

read 读:

ble.read(device_id, service_uuid, characteristic_uuid, success, failure);

参数:

device_id: 设备UUID 或者 设备 MAC 地址

service_uuid: 蓝牙设备的uuid

characteristic_uuid: UUID of the BLE characteristic

success: Success callback function that is invoked when the connection is successful. [optional]

failure: Error callback function, invoked when error occurs. [optional]

example:

readCounter = setInterval(function () {

ble.read(device.id, $scope.serviceUUID, $scope.counterCharacteristic,

$scope.onDataReader, $scope.onReadError);}, 1000);

$scope.serviceUUID:蓝牙的UUID,具体不懂,每个蓝牙都有,但是这个值需要注意的地方就是它在android和ios上的写法不一样,比如在android上它的值是xxxxfff0-xxxx-xxxx-xxxx-xxxxxxxxxxx,那么它在ios上就是FFF0,这个可以用ionic.Platform.isAndroid()进行平台判断

$scope.counterCharacteristic:蓝牙的特性值,写法跟UUID类似,在android和ios的差异写法也跟UUID一样,刚开始我在android上写好功能后在iOS上连不上蓝牙问题就出在了这里

$scope.onDataReader:成功的回调,可以进行读取数据

$scope.onReadError:失败的回调

读取数据扩充:

$scope.onDataReader=function(buffer){

//buffer就是蓝牙读取的数据,但是需要转换才能被引用

var data = new Uint8Array(buffer);//Uint8Array对象:8 位无符号整数值的类型化数组。内容将初始化为0。如果无法分配请求数目的字节,则将引发异常。

//这里可以一步步打印data然后按需要转出所需的数据

//将值赋值给页面上绑定的变量时,如果变量没有变化,试着用

$scope.$apply(function(){

//将计算后的数据给变量赋值,要用$apply涉及到了ng的脏值检查机制,有兴趣可以去搜搜相关资料

})

}

write 写:

ble.write(device_id, service_uuid, characteristic_uuid, data, success, failure);

参数:

device_id: 设备蓝牙的UUID或设备的mac地址

service_uuid: 蓝牙服务的UUID

characteristic_uuid: 具有特征值的蓝牙的UUID

data: 利用数组存储的二进制数

success: 成功回调

failure: 失败回调

example:

// send 1 byte to switch a light on

var data = new Uint8Array(1);

data[0] = 1;

ble.write(device_id, "FF10", "FF11", data.buffer, success, failure);

// send a 3 byte value with RGB color

var data = new Uint8Array(3);

data[0] = 0xFF; // red

data[1] = 0x00; // green

data[2] = 0xFF; // blue

ble.write(device_id, "ccc0", "ccc1", data.buffer, success, failure);

// send a 32 bit integer

var data = new Uint32Array(1);

data[0] = counterInput.value;

ble.write(device_id, SERVICE, CHARACTERISTIC, data.buffer, success, failure);

writeWithoutResponse 无响应的写入:

向特征设备写入无返回响应的数据

ble.writeWithoutResponse(device_id, service_uuid, characteristic_uuid, data, success, failure);

参数:

device_id: 设备蓝牙的UUID或设备的mac地址

service_uuid: 蓝牙服务的UUID

characteristic_uuid: 具有特征值的蓝牙的UUID

data: 利用数组存储的二进制数

success: 成功回调

failure: 失败回调

startNotification 开始通知:

ble.startNotification(device_id, service_uuid, characteristic_uuid, success, failure);

参数:

device_id: 设备蓝牙的UUID或设备的mac地址

service_uuid: 蓝牙服务的UUID

characteristic_uuid: 具有特征值的蓝牙的UUID

data: 利用数组存储的二进制数

success: 成功回调

failure: 失败回调

ble.startNotification(device_id, "FFE0", "FFE1", onData, failure);

example:

var onData = function(buffer) {

// Decode the ArrayBuffer into a typed Array based on the data you expect

var data = new Uint8Array(buffer);

alert("Button state changed to " + data[0]);

}

stopNotification 停止通知:

ble.stopNotification(divece_id,servece_uuid,characteristic_uuid,success,failure)

device_id: 设备蓝牙的UUID或设备的mac地址

service_uuid: 蓝牙服务的UUID

characteristic_uuid: 具有特征值的蓝牙的UUID

data: 利用数组存储的二进制数

success: 成功回调

failure: 失败回调

isConnected 是否连接:

ble.isConnected(device_id, success, failure);

参数:

device_id: 设备的uuid或者mac地址

success: 成功回调

failure: 失败回调

example:

ble.isConnected(

'FFCA0B09-CB1D-4DC0-A1EF-31AFD3EDFB53',

function() {

console.log("Peripheral is connected");

},

function() {

console.log("Peripheral is *not* connected");

}

);

isEnabled 是否开启蓝牙:

ble.isEnabled(success, failure);

参数:

success: 成功回调

failure: 失败回调

example:

ble.isEnabled(

function() {

console.log("Bluetooth is enabled");

},

function() {

console.log("Bluetooth is *not* enabled");

}

);

startStateNotification 打开通知状态:

ble.startStateNotifications(success, failure);

状态:

"on"

"off"

"turningOn" (Android Only)

"turningOff" (Android Only)

"unknown" (iOS Only)

"resetting" (iOS Only)

"unsupported" (iOS Only)

"unauthorized" (iOS Only)

参数:

success: 成功回调

failure: 失败回调

example:

ble.startStateNotifications(

function(state) {

console.log("Bluetooth is " + state);

}

);

stopStateNotification 停止状态通知:

ble.startStateNotifications(success, failure);

showBluetoothSettings 显示蓝牙设置:

ble.showBluetoothSettings(success, failure);

enable 打开蓝牙:

enable只能在Android平台使用,iOS无法使用。如果蓝牙已经打开,成功回调函数无法调用

ble.enable(success, failure);

参数:

success: 成功回调

failure: 失败回调

example:

ble.enable(

function() {

console.log("Bluetooth is enabled");

},

function() {

console.log("The user did *not* enable Bluetooth");

}

);

(如需转载请注明)

cordova 调用蓝牙_ionic蓝牙插件(cordova-plugin-ble-central)(个人翻译转载请注明)相关推荐

  1. uniapp:插件Luch_request 修改全局默认配置(转载请标明原创)

    目的是为了方便前端以后修改接口,以下是修改方法. 一.插件市场 导入 luch-request ,导入成功后,根目录有一个文件夹叫 js_sdk ,打开会发现有一个 luch-request 文件夹. ...

  2. Cordova入门系列(三)Cordova插件调用 转发 https://www.cnblogs.com/lishuxue/p/6018416.html...

    Cordova入门系列(三)Cordova插件调用 版权声明:本文为博主原创文章,转载请注明出处 上一章我们介绍了cordova android项目是如何运行的,这一章我们介绍cordova的核心内容 ...

  3. cordova调用java_Cordova入门系列(三)Cordova插件调用

    版权声明:本文为博主原创文章,转载请注明出处 上一章我们介绍了cordova android项目是如何运行的,这一章我们介绍cordova的核心内容,插件的调用.演示一个例子,通过cordova插件, ...

  4. jQuery雪花插件JQuery-Snowfall Plugin

    明天就是圣诞节,分享一个好玩的jQuery插件--JQuery-Snowfall Plugin,该插件可以实现在页面上飘落雪花的特效. JQuery-Snowfall插件的github项目地址:htt ...

  5. b700a怎么连蓝牙_233621蓝牙2.1无线音箱B700A小测

    随着蓝牙技术的发展,使用蓝牙的相关产品也越来越多,各种蓝牙耳机.蓝牙键盘.蓝牙鼠标都让我们的外设摆脱了线材的束缚.小编之前也介绍过一款便携蓝牙音箱,也评测过一款蓝牙耳机,而今天拿到的是一款采用蓝牙2. ...

  6. 蓝牙技术|蓝牙技术联盟发最新蓝牙5.3版本规范

    蓝牙技术成为支持物联网的全球无线标准的关键原因之一是其发展速度.蓝牙核心规范的更新涵盖了广泛的范围,从对现有功能的增强到主要新功能的引入,这些功能代表了技术的重大进步并为无线创新开辟了新的可能性.蓝牙 ...

  7. Cordova入门系列(三)Cordova插件调用

    上一章我们介绍了cordova android项目是如何运行的,这一章我们介绍cordova的核心内容,插件的调用.演示一个例子,通过cordova插件,去调用摄像头. 一.插件的安装以及基本信息: ...

  8. cordova 调用java_Cordova调用原生方法的插件的编写

    上一篇学习了Cordova官方提供插件的安装使用,其实Cordova对本地方法的调用并不是像WebView那样简单的调用,Cordova调用本地方法依赖于插件,今天学习一下插件怎么写. 插件编写通过类 ...

  9. cordova 环境配制和创建插件

    环境配制 英文网站:http://cordova.apache.org/ 中文网站:http://cordova.axuer.com/ 安装Cordova Cordova的命令行运行在Node.js ...

最新文章

  1. 虚拟函数是否应该被声明仅为private/protected?
  2. Python--编码的疑惑
  3. atitit.加入win 系统服务 bat批处理程序服务的法总结instsrv srvany java linux
  4. 用户反馈KB3189866累积更新出现卡在95%进度情况
  5. 第二十一期:老大难的GC原理及调优,这全说清楚了
  6. kafka创建Topic的一道面试题
  7. asp.net中时间差的问题
  8. java银行叫号课程设计_《银行排队叫号系统设计》课程设计.doc
  9. Linux终端界面Screen实现桌面共享
  10. 新浪微博注册页面的用户体验分析报告(转载)
  11. html页面计算圆的周长和面积,计算圆的周长和面积之间的差-JavaScript
  12. python阿拉伯数字转换为英文_python实现将英文单词表示的数字转换成阿拉伯数字的方法...
  13. 190108每日一句
  14. 证券行业的数字化转型:数字新基建 (云、中台、数字化解决方案)
  15. php机房图形资产管理系统,机房资产管理系统(CMDB)
  16. 因子分析python代码_关于「因」的诗词(649首)_诗词名句网
  17. 电容电阻尺寸单位规格-小记
  18. NC 应收应付金额计算逻辑
  19. 工具类commons-io的Tailer用法,用来监控文件内容的变化情况
  20. ESP8266-Arduino编程实例-MS5611气压传感器驱动

热门文章

  1. 阿里云服务器通用算力u1性能测评CPU处理器网络PPS
  2. 第3章 你应该如何运行程序 (可选,Shell、IDE介绍,推荐看一下)
  3. Python学习十四:访问列表元素、遍历列表
  4. 资产、负债及所有者权益类帐户
  5. 我男的,做电话销售,月入6000+,今年25了,感觉做不了几年,要不要转行软件测试,或者换其他工作?
  6. 进入四强的球队Java
  7. android自学流程!Android开发者出路在哪?不吃透都对不起自己
  8. win10 uwp 打开文件管理器选择文件
  9. windows下创建python虚拟环境
  10. 利用ES实现酒店搜索功能