Skip to content

Instantly share code, notes, and snippets.

@andikan
Forked from revolunet/bluetooth.js
Created May 17, 2014 17:46
Show Gist options
  • Save andikan/a2990bbd77d7dc91ecaf to your computer and use it in GitHub Desktop.
Save andikan/a2990bbd77d7dc91ecaf to your computer and use it in GitHub Desktop.
/*
Chrome Packaged app Bluetooth API test
Before interacting with a BT device, you need to :
1) get the device MAC and service UUID with startDiscovery and getServices methods
2) request permission with chrome.permissions.request
3) add the service profile with chrome.bluetooth.addProfile (a profile is only {uuid:'xxxxxxx...'})
*/
// onConnection callback
chrome.bluetooth.onConnection.addListener(
function(socket) {
console.log('onConnection:', socket);
chrome.bluetooth.read({
socket: socket
}, function() {
console.log('BT read:', arguments);
});
// chrome.bluetooth.write({
// socket: socket,
// data: arrayBuffer
// }, function() {
// console.log('write BT', arguments);
// })
});
// onAdapterStateChanged callback (wifi card)
chrome.bluetooth.onAdapterStateChanged.addListener(function(newStatus) {
console.log('onAdapterStateChanged:', arguments);
});
// discover devices
chrome.bluetooth.startDiscovery({
deviceCallback: function(device) {
console.log('deviceCallback:', device);
// discover services
chrome.bluetooth.getServices({
deviceAddress: device.address
}, function(services) {
console.log('getServices:', device.name, arguments);
var service = services[1];
// try to connect to service
// Wii service name is "Nintendo RVL-CNT-01"
chrome.bluetooth.connect({
deviceAddress: device.address,
serviceUuid: service.uuid
}, function() {
console.log('connect:', arguments);
})
})
}
}, function() {
console.log('callback', arguments);
});
// ask permission to access a device
chrome.permissions.request({
permissions: [{
'bluetoothDevices': [{
'deviceAddress': address
}]
}]
},
function(granted) {
console.log('device granted:', granted);
}
);
// add profile to local profiles (needed)
chrome.bluetooth.addProfile(profile, function() {
console.log('profile added', arguments);
// now you can connect
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment