Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created May 28, 2013 19:38
Show Gist options
  • Save revolunet/5665484 to your computer and use it in GitHub Desktop.
Save revolunet/5665484 to your computer and use it in GitHub Desktop.
Chrome Packaged app Bluetooth API example
/*
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
});
@revolunet
Copy link
Author

@adcmo
Copy link

adcmo commented Apr 17, 2015

Does this still work?

@lamarant
Copy link

lamarant commented Apr 5, 2016

nope. "chrome.bluetooth.connect is not a function"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment