一、Chrome扩展蓝牙开发
1.1 main.js解析(接上篇)
1.1.1 以下几行的作用是初始化tooltips
/**
*Initialize tooltips
*/
$scope.initTooltips = function() {
$('[data-toggle="tooltip"]').tooltip();
}
1.1.2 以下几句是的作用是设置当前蓝牙适配器
/**
*Set the current bluetooth adapter
*
*@param {Object} //参数是对象型的
*@return {undefined} //没有返回参数
*/
$scope.setAdapter = function(adapter) {
$scope.$apply(function() {
$scope.adapter = adapter;
});
};
1.1.3 以下几句的作用是:找到近期连接的蓝牙设备
/**
*Start device discovery for a optional specified amount of time
*
*@param {int} //整型参数
*@return {undefined} //没有返回值
*/
$scope.startDiscovery = function(timeout) {
$scope.deviceList = []; //设备清单
$scope.showDeviceList = true; //展示设备清单
$scope.deviceScan = true; //扫描设备可见
$scope.percentDone = 0; //扫描设备时完成的百分比,初始值为0
chrome.bluetooth.startDiscovery(); //调用Chrome接口,开始搜索蓝牙设备
chrome.bluetooth.getDevices(function(devices) {
for(var i=0, len=devices.length; i<len; i++) {//经测len的值为1,就是说发现了一个设备
devices[i].old = true;
console.log(devices[i].old);
$scope.addDevice(devices[i]);
}
});
1.1.4
var tm = parseInt(timeout)*1000 || 0; //timeout最小值为1,最大值为999,parseInt()函数可解析一个字符串,并返回一个整数。
if( tm > 0 ) {
$scope.scanProgress = 0;
window.setTimeout($scope.stopDiscovery, tm);
$scope.progressInterval = window.setInterval(function() {
var progressBar = document.getElementById('scanProgress');
引入:console.log(‘1008—>’progressBar);时截图如下:
$scope.$apply(function() {
$scope.scanProgress++; //输出1到20
});
$scope.percentDone = parseInt(($scope.scanProgress / timeout) * 100);
//$scope.percentDon表示扫描完成百分比的整数,比方20%,就显示20,上式中看出乘以100了,最后。
progressBar.setAttribute('aria-valuenow', $scope.scanProgress);
progressBar.style.width = $scope.percentDone + '%'
}, 1000);
1.1.5 以下几行的作用是:停止发现设备。主要是停止定时搜索函数setInterval
/**
*Stop device discovery
*
*@return {undefined}
*/
$scope.stopDiscovery= function() {
chrome.bluetooth.stopDiscovery();
$scope.deviceScan = false;
if( $scope.progressInterval ) {
console.log('qhz4$scope.progressInterval::',$scope.progressInterval);
window.clearInterval($scope.progressInterval);
//clearInterval() 方法可取消由setInterval() 设置的 timeout。clearInterval() 方法的参数必须是由 setInterval() 返回的 ID 值。
}
}
1.1.6 以下几行的作用是在列表中增加设备:
/**
*Add a device to the device list (newly discovered and old ones):在列表中增加一个设备
*
*@param {Object} //参数需要对象型
*@return {undefined} //没有返回值
*/
$scope.addDevice = function(device) {
console.log('qhz5->device:',device);//返回对象型的设备信息,截图如下:
if( !device.old ) device.old = false;
$scope.$apply(function() {
$scope.deviceList.push(device);
// console.log('qhz6$scope.deviceList.push(device::',$scope.deviceList.push(device));
});
};
1.1.7 获取所有的已知设备
/**
*Fetch all already known devices
*
*@param {function} //参数是函数
*@return {undefined} //没有返回值
*/
chrome.bluetooth.getDevices(function(devices) {
for(var i=0, len=devices.length; i<len; i++) {
devices[i].old = true;
$scope.addDevice(devices[i]);
}
});
1.1.8 获取适配器状态
/**
*Get the adapter state //获取适配器状态
*
*@param {function} //传入的参数是函数
*@return {undefined} //没有返回值
*/
chrome.bluetooth.getAdapterState(function(adapter) {
console.log("qhz6Adapter的address-->" + adapter.address + " adapter的name-->" +adapter.name);//console.log里可以有中文:
$scope.setAdapter(adapter);
});
上述的adapter的状态中,除了adapter.address,adapter.name还有三个adapter.powered、adapter.available、adapter.discovering. 修改上述程序,让其全部输出:
这三个状态都是布尔值,powerd:true表示有权限,avaviable:true表示适配器可用,discovering:false,表示这个适配器不是当前发现的。详细的说明如下官方截图:
小技巧:每次修改代码时,不必重新选择文件夹,直接点重新加载即可。
1.1.9 如果适配器状态改变了,则进行监控。
/**
*Callback if adapter state changed
*
*@param {function} //输入参数是函数
*@return {undefined} //没有返回值
*/
chrome.bluetooth.onAdapterStateChanged.addListener(function(adapter) {
$scope.setAdapter(adapter);
});
1.1.10 如果一个设备被添加,则进行监控
/**
*Callback if a device is added
*
*@param {function}
*@return {undefined}
*/
chrome.bluetooth.onDeviceAdded.addListener($scope.addDevice);
}]);
2.1 main.js所用chrome.bluetooth接口总结:
一共7个,有两个重复的,合起来是6个,截图如下:
1)chrome.bluetooth.startDiscovery()----->开始发现设备 ---------方法
2)chrome.bluetooth.stopDiscovery()----->停止发现设备 ---------方法
3)chrome.bluetooth.getDevices()----->获得设备信息 ---------方法
4)chrome.bluetooth.getAdapterState()----->获得适配器状态---------方法
5)chrome.bluetooth.onAdapterStateChanged.addListener()----->适配器状态改变时进行监控
-----------事件
6)chrome.bluetooth.onDeviceAdded.addListener()----->有新设备添加时进行监控
-------------事件
官网上:(https://developer.chrome.com/apps/bluetooth#type-AdapterState)
chrome.bluetooth接口,方法有五个,本项目用了四个:
事件有四个,本项目用了两个:
2016年10月8日星期六