Random posts about coding

Mostly blogging about dart.

How to Communicate With Chrome.* API in Dart

| Comments

I had the privilege to attend Chrome Apps Accelerated MTV hackathon. The goal set out for my self was to create a bluetooth scanner via chrome.bluetooth api.

First I started off by reviewing the Chrome app developer guide. From a previous post, a patch for 7228 was needed at the time.

Second was too communicate with the js defined context which Chrome Apps execute in. Some setup ceremony is needed such as a manifest file and background window. Creating the manifest file was easy, mostly choosing permissions and adding the entry Javascript background entry.

For dart we will need to create the background javascript that loads our dart application.

I stumbled with the communications between dart generated javascript and js-interop for some time. The first part of the stumbling blocks was CSP (Content Security Policy) and how dart2js apps are loaded. Thanks to Vijay Menon and Renato Mangini, I was able to create successful call and callback from dart to javascript. This was non trivial since it required knowledge of dart.js and loading of dart_interop.js/js.dart.

The diff file below removes the reference to localStorage and replaces it with a map.

dart_interop.js and js.dart needed to be copied into the root folder of the project. Not sure exactly why this is, very possible that only dart_interop.js is needed since were compiling to js.

The first sanity check of being able to communicate with dart in a Chrome App was printing out the permissions from chrome.permissions.getAll

Google searches lead me to the wrong api docs for accessing the bluetooth device. Managed to land on chrome.experimental.bluetooth from extensions api, which is different from the Chrome Apps chrome.* api. Bug was filed about how easy it was to land on the wrong api pages.

Now that I’ve got the right context too loaded, calling the chrome.bluetooth api produced errors with bluetooth support on MacOSX. doh! Bluetooth is currently only supported on ChromeOS and Windows. This was not mentioned in any of the docs and bug was filed on that.

1
2
Error during bluetooth.isAvailable: This operation is not supported on your platform 
chromeHidden.handleResponse

At this point I was glad to be able to make calls on chrome.* api, in a following post I will go over a more complete sample from start to finish. Feel free to browse code and project structure bluetooth_example_chrome_app. Please note this is not a fully working sample and has bugs!

By the end of the hackathon I decided my best bet was to package something that did not rely on the chrome.* api so heavily. An ASCII camera capture app was created and demoed. The application accesses the client’s video input device and converts images capture to ascii images based on a ascii art formula. “videoCapture” permissions are required for accessing video input, this was set in the manifest file. The ASCII camera capture app code is available on the following branch chrome_app_example.