Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit dd2f506

Browse files
author
Ke, Mingze
committed
Added bluetooth API support
1 parent ca187a2 commit dd2f506

File tree

5 files changed

+136
-11
lines changed

5 files changed

+136
-11
lines changed

‎chrome-app/manifest.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "API Proxy for Google Chrome™",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"manifest_version": 2,
55
"minimum_chrome_version": "23",
66
"app": {
@@ -14,6 +14,10 @@
1414
"permissions": [
1515
"serial"
1616
],
17+
"bluetooth": {
18+
"uuids": ["1101"],
19+
"socket": true
20+
},
1721
"icons": {
1822
"128": "assets/icon_128.png"
1923
}

‎chrome-app/serial.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<head>
55
<meta charset="utf-8">
6-
<title>Serial Port Status</title>
6+
<title>Status</title>
77
<script src="json-human/src/json.human.js"></script>
88
<script src="serial.js"></script>
99
<link rel="stylesheet" href="json-human/css/json.human.css"></link>
@@ -24,13 +24,20 @@
2424
</head>
2525

2626
<body>
27-
<h1>Serial Port Status <button class='button' id='refresh'>Refresh</button></h1>
28-
<div id='serial-port-status'></div>
27+
<h1>Serial Port <button class='button' id='refresh'>Refresh</button></h1>
28+
<div id='status'></div>
2929
<div>
3030
<input id='disconnId' type='text' size='5' />
3131
<button id='disconnect'>Disconnect</button>
3232
<span id='disconnStatus'></span>
3333
</div>
34+
<h1>Bluetooth <button class='button' id='refresh-bt'>Refresh</button></h1>
35+
<div id='status-bt'></div>
36+
<div>
37+
<input id='disconnId-bt' type='text' size='5' />
38+
<button id='disconnect-bt'>Disconnect</button>
39+
<span id='disconnStatus-bt'></span>
40+
</div>
3441
</body>
3542

3643
</html>

‎chrome-app/serial.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,48 @@
22

33
'use strict';
44

5-
var node;
5+
var node, nodeBT;
66

77
document.addEventListener('DOMContentLoaded', function () {
8-
node = document.querySelector('#serial-port-status');
8+
node = document.querySelector('#status');
9+
nodeBT = document.querySelector('#status-bt');
910

1011
getNode('refresh').addEventListener('click', function () {
11-
refreshStatus(node);
12+
refreshStatus();
1213
}, false);
1314

1415
getNode('disconnect').addEventListener('click', function () {
1516
chrome.serial.disconnect(parseInt(getNode('disconnId').value), function (e) {
16-
refreshStatus(node);
17+
refreshStatus();
1718
if (e !== true) {
1819
alert(e + '');
1920
}
2021
});
2122
});
2223

23-
refreshStatus(node);
24+
getNode('refresh-bt').addEventListener('click', function () {
25+
refreshStatusBT();
26+
}, false);
27+
28+
getNode('disconnect-bt').addEventListener('click', function () {
29+
chrome.bluetoothSocket.close(parseInt(getNode('disconnId-bt').value), function (e) {
30+
refreshStatusBT();
31+
if (e !== true) {
32+
alert(e + '');
33+
}
34+
});
35+
});
36+
37+
refreshStatus();
38+
refreshStatusBT();
2439

2540
window.addEventListener('focus', function () {
26-
refreshStatus(node);
41+
refreshStatus();
42+
refreshStatusBT();
2743
}, false);
2844
}, false);
2945

30-
function refreshStatus(node) {
46+
function refreshStatus() {
3147
var status = {};
3248

3349
getNode('disconnId').value = '';
@@ -43,6 +59,22 @@
4359
});
4460
}
4561

62+
function refreshStatusBT() {
63+
var status = {};
64+
65+
getNode('disconnId-bt').value = '';
66+
getNode('disconnStatus-bt').innerHTML = '';
67+
68+
chrome.bluetooth.getDevices(function (devs) {
69+
status.devices = devs;
70+
chrome.bluetoothSocket.getSockets(function (sks) {
71+
status.sockets = sks;
72+
nodeBT.innerHTML = '';
73+
nodeBT.appendChild(JsonHuman.format(status));
74+
});
75+
});
76+
}
77+
4678
function getNode(id) {
4779
return document.getElementById(id);
4880
}

‎chrome-app/transforms.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@
2020
}];
2121
};
2222

23+
scope.transforms.request['chrome.bluetoothSocket.send'] = function (socketId, data) {
24+
return [socketId, new Uint8Array(data).buffer];
25+
};
26+
27+
scope.transforms.response['chrome.bluetoothSocket.onReceive.addListener'] = function (event) {
28+
return [{
29+
socketId: event.socketId,
30+
data: slice.call(new Uint8Array(event.data))
31+
}];
32+
};
33+
2334
}(window));

‎lib/chrome.bluetooth.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
chrome.bluetooth = chrome.bluetooth || (function (_api) {
2+
3+
'use strict';
4+
5+
var proxyRequest = _api.proxyRequest;
6+
7+
return {
8+
getDevice: proxyRequest('chrome.bluetooth.getDevice'),
9+
getDevices: proxyRequest('chrome.bluetooth.getDevices')
10+
};
11+
12+
}(chrome._api));
13+
14+
chrome.bluetoothSocket = chrome.bluetoothSocket || (function (_api) {
15+
16+
'use strict';
17+
18+
var slice = Array.prototype.slice,
19+
undef = undefined,
20+
proxyRequest = _api.proxyRequest,
21+
proxyAddListener = _api.proxyAddListener,
22+
proxyRemoveListener = _api.proxyRemoveListener,
23+
proxiedSend = proxyRequest('chrome.bluetoothSocket.send');
24+
25+
return {
26+
create: proxyRequest('chrome.bluetoothSocket.create'),
27+
28+
connect: proxyRequest('chrome.bluetoothSocket.connect'),
29+
30+
update: proxyRequest('chrome.bluetoothSocket.update'),
31+
32+
disconnect: proxyRequest('chrome.bluetoothSocket.disconnect'),
33+
34+
close: proxyRequest('chrome.bluetoothSocket.close'),
35+
36+
setPaused: proxyRequest('chrome.bluetoothSocket.setPaused'),
37+
38+
getInfo: proxyRequest('chrome.bluetoothSocket.getInfo'),
39+
40+
getSockets: proxyRequest('chrome.bluetoothSocket.getSockets'),
41+
42+
send: function (socketId, data, callback) {
43+
proxiedSend.apply(undef, [socketId, slice.call(new Uint8Array(data)), callback]);
44+
},
45+
46+
listenUsingRfcomm: proxyRequest('chrome.bluetoothSocket.listenUsingRfcomm'),
47+
48+
listenUsingL2cap: proxyRequest('chrome.bluetoothSocket.listenUsingL2cap'),
49+
50+
onAccept: {
51+
addListener: proxyAddListener('chrome.bluetoothSocket.onAccept.addListener'),
52+
removeListener: proxyRemoveListener('chrome.bluetoothSocket.onAccept.removeListener')
53+
},
54+
55+
onAcceptError: {
56+
addListener: proxyAddListener('chrome.bluetoothSocket.onAcceptError.addListener'),
57+
removeListener: proxyRemoveListener('chrome.bluetoothSocket.onAcceptError.removeListener')
58+
},
59+
60+
onReceive: {
61+
addListener: proxyAddListener('chrome.bluetoothSocket.onReceive.addListener'),
62+
removeListener: proxyRemoveListener('chrome.bluetoothSocket.onReceive.removeListener')
63+
},
64+
65+
onReceiveError: {
66+
addListener: proxyAddListener('chrome.bluetoothSocket.onReceiveError.addListener'),
67+
removeListener: proxyRemoveListener('chrome.bluetoothSocket.onReceiveError.removeListener')
68+
}
69+
};
70+
71+
}(chrome._api));

0 commit comments

Comments
 (0)