forked from BLE-MIDI/WebMIDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblerx.js
116 lines (107 loc) · 3.81 KB
/
blerx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** Global Variables */
var connectDevice = null;
var synth;
var bleDevice;
var bluetoothDevice;
/** Function to run upon window load. */
window.onload=function(){
let norurl;
if (getUrlVars()["demo"] !== undefined) {
norurl = "./no/multirxno.html?demo=" + getUrlVars()["demo"] + "&id=" + getUrlVars()["id"];
console.log(norurl);
document.getElementById("link").href = norurl;
bleDevice = getUrlVars()["demo"] + " " + getUrlVars()["id"];
}else{
console.log("BLE Device: " + bleDevice);
}
/** Synth for Audio feedback */
synth = new WebAudioTinySynth();
synth.setTsMode(1);
synth.setQuality(0);
console.log('WebAudio Tinysynth is running...');
}
/** Connecting to given device. */
function rxConnect() {
printToConsole('Searching for bluetooth devices...');
console.log('Requesting Bluetooth Device with MIDI UUID...');
console.log('Searching for ' + getUrlVars()["demo"] + getUrlVars()["id"] + "...");
navigator.bluetooth.requestDevice({
filters: [{
services: [MIDI_SERVICE_UID],
name: bleDevice
}]
})
.then(device => {
// Set up event listener for when device gets disconnected.
console.log('Connecting to GATT server of ' + device.name);
printToConsole('Connecting to bluetooth device '+ device.name + '...');
device.addEventListener('gattserverdisconnected', onDisconnected);
bleConnected();
bluetoothDevice = device;
// Attempts to connect to remote GATT Server.
return device.gatt.connect();
})
.then(server => {
console.log('Getting Service...');
return server.getPrimaryService(MIDI_SERVICE_UID);
})
.then(service => {
console.log('Getting Characteristic...');
return service.getCharacteristic(MIDI_IO_CHARACTERISTIC_UID);
})
.then(characteristic => {
console.log('Found Characteristic...');
return characteristic.startNotifications();
})
.then(characteristic => {
// Set up event listener for when characteristic value changes.
characteristic.oncharacteristicvaluechanged = handleMidiMessageReceived;
console.log('Notifications have been started.')
printToConsole('Ready to test!');
})
.catch(error => { console.log('ERRORCODE: ' + error); });
}
function rxDisconnect() {
if (!bluetoothDevice) {
return;
}
console.log('Disconnecting from Bluetooth Device...');
if (bluetoothDevice.gatt.connected) {
bluetoothDevice.gatt.disconnect();
bleDisconnect();
printToConsole('Disconnecting from Bluetooth Device...');
} else {
console.log('> Bluetooth Device is already disconnected');
printToConsole('Bluetooth Device is already disconnected.');
}
}
/** Incoming BLE MIDI */
function handleMidiMessageReceived(event) {
console.log('Received value: ' + event.target.value);
const {buffer} = event.target.value;
const eventData = new Uint8Array(buffer);
bleMIDIrx(eventData);
}
function onDisconnected(event) {
if (!connectDevice || !connectDevice.gatt.connected) return;
connectDevice.gatt.disconnect();
let device = event.target;
printToConsole('Connection lost...');
console.log('Device ' + device.name + ' is disconnected.');
bleDisconnect();
}
function bleConnected() {
document.getElementById('hide').style.display = 'none';
document.getElementById('ibutton').innerHTML = 'Disconnect';
document.getElementById('midi-data').style.height = '45vh';
document.getElementById("ibutton").onclick = rxDisconnect;
document.getElementById("info").style.fontSize = '1.5em';
document.getElementById('midi-data').style.background = '#ECEFF1'
}
function bleDisconnect() {
document.getElementById('hide').style.display = 'block';
document.getElementById('ibutton').innerHTML = 'Connect';
document.getElementById('midi-data').style.height = '10vh';
document.getElementById("ibutton").onclick = rxConnect;
document.getElementById("info").style.fontSize = '2em';
}