2

I'm writing a Chrome App using the chrome.bluetooth Javascript API and PNACL. I can turn on Bluetooth discovery, find devices, connect, and communicate successfully. But I cannot figure out how to pair a new device programmatically from my app.

There are Windows and Mac system APIs for this; is there an equivalent on ChromeOS?

mekin
  • 99
  • 4

1 Answers1

0

Use the chrome.bluetooth API to connect to a Bluetooth device which works only on OS X, Windows and Chrome OS. All functions report failures via chrome.runtime.lastError.

You may make your chrome app connect to any device that supports RFCOMM or L2CAP services and that includes the majority of classic Bluetooth devices on the market.

As detailed in Chrome - Bluetooth, there are three things you need to make a connection to a device:

  • A socket to make the connection with, created using bluetoothSocket.create
  • the address of the device you wish to connect to
  • the UUID of the service itself.

Sample code implementation:

var uuid = '1105';
var onConnectedCallback = function() {
  if (chrome.runtime.lastError) {
    console.log("Connection failed: " + chrome.runtime.lastError.message);
  } else {
    // Profile implementation here.
  }
};

chrome.bluetoothSocket.create(function(createInfo) {
  chrome.bluetoothSocket.connect(createInfo.socketId,
    device.address, uuid, onConnectedCallback);
});

Please note too that before making the connection, you should verify that the adapter is aware of the device by using bluetooth.getDevice or the device discovery APIs.

More information and sample code implementations can be found in the documentation.

Teyam
  • 7,686
  • 3
  • 15
  • 22
  • 1
    Thanks, but that only works with already paired devices. This question is about pairing a new device programmatically. I don't understand why you'd be able to discover devices but not pair them. – mekin May 07 '16 at 05:51