使用chrome.bluetooth API连接到设备

时间:2014-02-21 16:45:48

标签: javascript bluetooth bluetooth-lowenergy google-chrome-app

我一直在尝试创建一个使用chrome.bluetooth API连接到德州仪器CC2541 SensorTag设备并与之通信的Chrome应用。

这里的代码检测到SensorTag并获取设备信息,但设备上调用的'getProfiles'和'getServices'方法都返回空,'connect'方法给出错误'Profile not found:invalid uuid' 。

我尝试过从example SensorTag Android app中获取的UUID的多种变体(可以在代码中看到),但都会给出相同的“无效的uuid”错误。

即使你无法解决这个特殊的问题,也可以从使用chrome.bluetooth API的任何人那里获得任何乐趣。到目前为止,我的经验是,真正使用它的目标太多了(是的,我确实知道它只是'Dev'),但是如果可能的话,我真的很想让它工作。

感谢您的关注 - 任何帮助或想法非常感谢!

编辑:其他平台信息
我首先尝试在带有CSR 4.0蓝牙适配器的Windows 7上运行此功能,但事实证明这是徒劳的:使用通用Windows 7 BT驱动程序Chrome可以看到适配器并检测BT设备,但驱动程序不支持低功耗无法检测到我想要的设备。使用CSR驱动程序,它支持LE,我可以在Windows“蓝牙设备”中使用它连接到LE设备,Chrome.bluetooth根本无法检测到蓝牙适配器。

所以现在我正在使用Acer C720 Chromebook,它看起来应该可以工作,但我只是得到“无效的UUID”消息,无论我尝试什么。

(虽然Chrome操作系统和Win / Mac / Linux'开发'版本的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();
      }   
});

的index.html

<html>
<head></head>
<body>
    <div id="log"></div>
</body>
<script src="main.js"></script>
</html>

的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": {}
}

background.js

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('index.html', {
    id: "window1",
    bounds: {
      width: 640,
      height: 480
    }
  });
});

4 个答案:

答案 0 :(得分:8)

我遇到了同样的问题。经过一段时间的挖掘后,我能够用我正在使用的模式确定两个问题。

第一个问题是,据我所知,清单中空的“蓝牙”对象实际上并不是有效的,尽管周围的例子使用它并且chrome没有抱怨:

"bluetooth": {}

相反,您的蓝牙清单条目应该如下所示:

"bluetooth":{
"profiles":
    [
        "00001101-0000-1000-8000-00805f9b34fb" // array of uuids
    ]
},

这个文档可以在两周前的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

希望这有帮助。

答案 1 :(得分:2)

您使用的CSR标签是低能耗设备?在这种情况下,您需要使用chrome.bluetoothLowEnergy API:https://developer.chrome.com/apps/bluetoothLowEnergy

有关示例,请参阅:https://github.com/armansito/ble-battery-service-demo/blob/master/script.js

答案 2 :(得分:0)

某些更新,似乎清单格式会根据以下内容略有变化:https://developer.chrome.com/apps/app_bluetooth

"bluetooth": {
    "uuids": [ "1105", "1106" ]
}

答案 3 :(得分:0)

发现另一个解决方案似乎也很好:www.github.com/bcsphere/bluetooth

它支持iOS / Android蓝牙JS API。它不仅支持BLE / GATT,还支持BT2.1 classic(例如支持串口/ rfcomm配置文件。)

他们甚至实现了一些服务/配置文件库。     https://github.com/bcsphere/bluetooth/tree/master/www

各种开源示例:     https://github.com/bcsphere/apps