The company that manufactures bluetooth dice were kind enough to send me a browser application to test out the dice to make sure they are working properly. The application consists of an html, css, and two javascript files. Now I am attempting to make sense of what needs to be done to incorporate this into B4X. The javascript code is a main and a class.
The code in the main, main.js, to request a connection
The code in the class, godice.js, to connect to a die
Putting Chrome into debug mode and adding some logging I see this.
GlobalDeviceId: aOHCIYtwaFac7kbrO7ac2Q== godice.js:275
bluetoothDevice: godice.js:277
BluetoothDevice {id: 'aOHCIYtwaFac7kbrO7ac2Q==', name: 'GoDice_CE03C9_B_v04',
gatt: BluetoothRemoteGATTServer, ongattserverdisconnected: null}
gatt: BluetoothRemoteGATTServer {device: BluetoothDevice, connected: true}
id: "aOHCIYtwaFac7kbrO7ac2Q=="
name: "GoDice_CE03C9_B_v04"
ongattserverdisconnected: null
[[Prototype]]: BluetoothDevice
Dice connected: aOHCIYtwaFac7kbrO7ac2Q== main.js:24
The Bluetooth.requestDevice() method of the Bluetooth interface returns a Promise to a BluetoothDevice object with the specified options. If there is no chooser UI, this method returns the first device matching the criteria.
The BluetoothRemoteGATTServer interface of the Web Bluetooth API represents a GATT Server on a remote device. Does this mean that the die is the GATT server or that the web application needed the interface to use bluetooth functionality, or is something else going on?
Either way does B4X have the capability of doing this request. I would think probably so but I am getting nowhere testing with the various bluetooth examples that exists in this forum.
In the unfiltered B4X log I am seeing this information.
Found: GoDice_CE03C9_B_v04, D9:E9:F4:C9:03:CE, RSSI = -54, (MyMap) {1=[B@f832ea0, 9=[B@2cfca59, 7=[B@bf6761e, 0=[B@9219eff}
stopLeScan()
isLeEnabled(): ON
D9:E9:F4:C9:03:CE
connecting to GoDice_CE03C9_B_v04
connect() - device: D9:E9:F4:C9:03:CE, auto: true
registerApp()
registerApp() - UUID=cfbfad8e-4090-4113-abef-ccff536c2d2c
onClientRegistered() - status=0 clientIf=12
onClientConnectionState() - status=133 clientIf=12 device=D9:E9:F4:C9:03:CE
close()
unregisterApp() - mClientIf=12
Disconnected
I read that the status code of 133 on the OnClientConnection state is that it timed out which makes sense.
The code in the main, main.js, to request a connection
B4X:
// Open the Bluetooth connection dialog for choosing a GoDice to connect
function openConnectionDialog() {
const newDice = new GoDice();
newDice.requestDevice();
}
The code in the class, godice.js, to connect to a die
B4X:
/**
* Open a connection dialog to connect a single GoDice, after successfull connection it will follow by corresponding "DiceConnected" event (response).
*/
requestDevice() {
return navigator.bluetooth.requestDevice({
filters: [{ namePrefix: 'GoDice_' }],
optionalServices: ['6e400001-b5a3-f393-e0a9-e50e24dcca9e']
})
.then(async device => {
this.GlobalDeviceId = device.id.toString();
console.log("GlobalDeviceId: ", this.GlobalDeviceId);
this.bluetoothDevice = device;
console.log("bluetoothDevice: ", this.bluetoothDevice);
var _self = this
this.bluetoothDevice.addEventListener('gattserverdisconnected', function() {
_self.onDiceDisconnected(_self.GlobalDeviceId, _self)
})
await this.connectDeviceAndCacheCharacteristics();
});
}
Putting Chrome into debug mode and adding some logging I see this.
GlobalDeviceId: aOHCIYtwaFac7kbrO7ac2Q== godice.js:275
bluetoothDevice: godice.js:277
BluetoothDevice {id: 'aOHCIYtwaFac7kbrO7ac2Q==', name: 'GoDice_CE03C9_B_v04',
gatt: BluetoothRemoteGATTServer, ongattserverdisconnected: null}
gatt: BluetoothRemoteGATTServer {device: BluetoothDevice, connected: true}
id: "aOHCIYtwaFac7kbrO7ac2Q=="
name: "GoDice_CE03C9_B_v04"
ongattserverdisconnected: null
[[Prototype]]: BluetoothDevice
Dice connected: aOHCIYtwaFac7kbrO7ac2Q== main.js:24
The Bluetooth.requestDevice() method of the Bluetooth interface returns a Promise to a BluetoothDevice object with the specified options. If there is no chooser UI, this method returns the first device matching the criteria.
The BluetoothRemoteGATTServer interface of the Web Bluetooth API represents a GATT Server on a remote device. Does this mean that the die is the GATT server or that the web application needed the interface to use bluetooth functionality, or is something else going on?
Either way does B4X have the capability of doing this request. I would think probably so but I am getting nowhere testing with the various bluetooth examples that exists in this forum.
In the unfiltered B4X log I am seeing this information.
Found: GoDice_CE03C9_B_v04, D9:E9:F4:C9:03:CE, RSSI = -54, (MyMap) {1=[B@f832ea0, 9=[B@2cfca59, 7=[B@bf6761e, 0=[B@9219eff}
stopLeScan()
isLeEnabled(): ON
D9:E9:F4:C9:03:CE
connecting to GoDice_CE03C9_B_v04
connect() - device: D9:E9:F4:C9:03:CE, auto: true
registerApp()
registerApp() - UUID=cfbfad8e-4090-4113-abef-ccff536c2d2c
onClientRegistered() - status=0 clientIf=12
onClientConnectionState() - status=133 clientIf=12 device=D9:E9:F4:C9:03:CE
close()
unregisterApp() - mClientIf=12
Disconnected
I read that the status code of 133 on the OnClientConnection state is that it timed out which makes sense.