Kev*_*v K 8 javascript bluetooth bluetooth-lowenergy google-chrome-app
我一直在尝试创建一个Chrome应用程序,该应用程序使用chrome.bluetooth API连接到Texas Instruments CC2541 SensorTag设备并与之通信.
这里的代码检测SensorTag并获取设备信息,但设备上调用的'getProfiles'和'getServices'方法都返回空,而'connect'方法给出错误'Profile not found:invalid uuid'.
我尝试了从示例SensorTag Android应用程序中获取的多种UUID变体(如代码中所示),但都给出了相同的"无效uuid"错误.
即使你无法解决这个特殊的问题,也可以从使用chrome.bluetooth API的任何人那里获得任何乐趣.到目前为止,我的经验是,真正使用它是一个太多的移动目标(是的,我知道它只是'Dev'),但是如果可能的话我真的很想让它工作.
感谢您的关注 - 任何帮助或想法非常感谢!
编辑:其他平台信息
我首先尝试在带有CSR 4.0蓝牙适配器的Windows 7上运行此功能,但事实证明这完全是徒劳的:使用通用Windows 7 BT驱动程序Chrome可以看到适配器并检测BT设备,但驱动程序没有t支持低能耗因此无法检测到我想要的设备.使用CSR驱动程序,它支持LE,我可以在Windows"蓝牙设备"中使用它连接到LE设备,Chrome.bluetooth根本无法检测到蓝牙适配器.
所以现在我正在使用Acer C720 Chromebook,它看起来应该可以工作,但我只是得到"无效的UUID"消息,无论我尝试什么.
(虽然Chrome操作系统和Win/Mac/Linux'Dev'版本的Chrome与更新不一致 - Chrome操作系统暂时落后于其他版本,但现在已经赶上了 - 所以有一段时间需要不同的格式'manifest.json '用于在不同平台上启动应用程序的文件.)
main.js
// I have tried multiple variations of known UUIDS for device... all give "Profile not found: invalid uuid"
var profiles = [
// UUID_IRT_SERV from example Android app
{uuid : 'f000aa00-0451-4000-b000-000000000000', name : 'SensorTag1-lc'},
{uuid : 'F000AA00-0451-4000-B000-000000000000', name : 'SensorTag1-uc'},
{uuid : 'f000aa00', name : 'SensorTag1-shortlc'},
{uuid : 'F000AA00', name : 'SensorTag1-shortuc'},
// UUID_IRT_DATA from example Android app
{uuid : 'f000aa01-0451-4000-b000-000000000000', name : 'SensorTag2-lc'},
{uuid : 'F000AA01-0451-4000-B000-000000000000', name : 'SensorTag2-uc'},
{uuid : 'f000aa01', name : 'SensorTag2-shortlc'},
{uuid : 'F000AA01', name : 'SensorTag2-shortuc'},
// UUID_IRT_CONF from example Android app
{uuid : 'f000aa02-0451-4000-b000-000000000000', name : 'SensorTag3-lc'},
{uuid : 'F000AA02-0451-4000-B000-000000000000', name : 'SensorTag3-uc'},
{uuid : 'f000aa02', name : 'SensorTag3-shortlc'},
{uuid : 'F000AA02', name : 'SensorTag3-shortuc'},
// UUID_IRT_PERI from example Android app
{uuid : 'f000aa03-0451-4000-b000-000000000000', name : 'SensorTag4-lc'},
{uuid : 'F000AA03-0451-4000-B000-000000000000', name : 'SensorTag4-uc'},
{uuid : 'f000aa03', name : 'SensorTag4-shortlc'},
{uuid : 'F000AA03', name : 'SensorTag4-shortuc'},
// UUID_KEY_SERV from example Android app
{uuid : '0000ffe0-0000-1000-8000-00805f9b34fb', name : 'SensorTag5-lc'},
{uuid : '0000FFE0-0000-1000-8000-00805F9B34FB', name : 'SensorTag5-uc'},
{uuid : '0000ffe0', name : 'SensorTag5-shortlc'},
{uuid : '0000FFE0', name : 'SensorTag5-shortuc'},
// UUID_KEY_DATA from example Android app
{uuid : '0000ffe1-0000-1000-8000-00805f9b34fb', name : 'SensorTag6-lc'},
{uuid : '0000FFE1-0000-1000-8000-00805F9B34FB', name : 'SensorTag6-uc'},
{uuid : '0000ffe1', name : 'SensorTag6-shortlc'},
{uuid : '0000FFE1', name : 'SensorTag6-shortuc'},
];
// Listener to deal with initial connection
chrome.bluetooth.onConnection.addListener(onConnected);
// onAdapterStateChanged callback - for debug only
chrome.bluetooth.onAdapterStateChanged.addListener(function(newStatus) {
log('onAdapterStateChanged: ' + JSON.stringify(arguments));
});
// Logs debug messages to app window
function log(msg) {
var msg_str = (typeof(msg) == 'object') ? JSON.stringify(msg) : msg;
console.log(msg_str);
var l = document.getElementById('log');
if (l) {
l.innerText += msg_str + '\n';
}
}
// Function that is called on connection to device
var onConnected = function(socket) {
log("Success - Connected to SensorTag!");
log("Socket: " + JSON.stringify(socket));
}
function recordDevice(device) {
log("Found BT Device: " + JSON.stringify(device));
if (device.name == "SensorTag") {
log("Got SensorTag...");
// Stop discovery and then connect to SensorTag
chrome.bluetooth.stopDiscovery(connectToSensorTag(device));
}
}
function connectToSensorTag(device) {
log("Getting profiles of SensorTag...");
chrome.bluetooth.getProfiles({device: device}, function(profiles) {
log('Got profiles: ' + JSON.stringify(profiles));
});
chrome.bluetooth.getServices({deviceAddress: device.address}, function(services) {
log('Got services: ' + JSON.stringify(services));
});
for (var i = 0; i < profiles.length; i++) {
chrome.bluetooth.connect({ device: device, profile: profiles[i] }, function() {
log('Connect called: ' + JSON.stringify(arguments));
if (chrome.runtime.lastError) {
log("Connection error: " + chrome.runtime.lastError.message);
}
});
}
}
function findDevices() {
log("Finding devices...");
chrome.bluetooth.startDiscovery({deviceCallback: recordDevice});
}
// App execution begins here.
// Add all profiles to try connection later
for (var i = 0; i < profiles.length; i++) {
log("Adding profile: " + profiles[i]);
chrome.bluetooth.addProfile(profiles[i], function() {
log("SensorTag profile added."));
});
}
chrome.bluetooth.getAdapterState( function(result) {
if (result.powered == false || result.available == false ) {
log("Error: No bluetooth adapter available.");
} else {
log("Bluetooth adapter enabled.");
findDevices();
}
});
Run Code Online (Sandbox Code Playgroud)
的index.html
<html>
<head></head>
<body>
<div id="log"></div>
</body>
<script src="main.js"></script>
</html>
Run Code Online (Sandbox Code Playgroud)
的manifest.json
{
"manifest_version": 2,
"name": "Connect to SensorTag",
"description": "Connects to TI SensorTag",
"version": "1.0",
"minimum_chrome_version": "30",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"bluetooth": {}
}
Run Code Online (Sandbox Code Playgroud)
background.js
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
id: "window1",
bounds: {
width: 640,
height: 480
}
});
});
Run Code Online (Sandbox Code Playgroud)
小智 8
我遇到了同样的问题.经过一段时间的挖掘后,我能够用我正在使用的模式确定两个问题.
第一个问题是,据我所知,清单中一个空的"蓝牙"对象实际上并不是有效的,尽管周围的例子使用它并且chrome没有抱怨:
"bluetooth": {}
Run Code Online (Sandbox Code Playgroud)
相反,您的蓝牙清单条目应如下所示:
"bluetooth":{
"profiles":
[
"00001101-0000-1000-8000-00805f9b34fb" // array of uuids
]
},
Run Code Online (Sandbox Code Playgroud)
这个文档可以在两周前的chrome提交中找到:https: //codereview.chromium.org/145663004/patch/760001/770016
我在跟踪此问题时发现的这个API的另一个特性是,不仅可以在"chrome.runtime.lastError"中留下错误的连接调用,这些调用中的任何一个都可能为您留下消息.如果在addProfile调用后检查lastError,您可能会看到chrome抱怨权限:"添加配置文件被拒绝的权限".
回过头来阅读chrome.bluetooth api文档,它实际上在顶部附近的描述中说明了这一点(尽管我像你一样错过了它)
使用chrome.bluetooth API连接到蓝牙设备.所有函数都通过chrome.runtime.lastError报告失败.
(重点补充)http://developer.chrome.com/apps/bluetooth#type-Profile
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
14451 次 |
| 最近记录: |