在MicroBit开发板服务添加成功后,我们要修改电脑端源码进行读写操作,从而控制开发板LED灯的亮灭。 一,修改源码 Step1:阅读特性的相关代码 function readModelNumber() { console.log(“readModelNumber”); // state validation if (!connected) { alert(“Error: Discover and connect to a device before using this function”); return; } if (!services_discovered) { alert(“Error: Service discovery has not yet completed”); return; } if (!has_device_information_service) { alert(“Error: The connected device does not contain the device information service”); return; } if (!has_model_name_string) { alert(“Error: The connected device does not contain the model name string characteristic”); return; } model_number_string.readValue() .then(value => { data = new Uint8Array(value.buffer); model_number_string = new TextDecoder(“utf8”).decode(data); console.log(model_number_string); document.getElementById(“model_number”).innerHTML = model_number_string; }) .catch(error => { console.log(‘Error: ‘ + error); alert(‘Error: ‘ + error); return; }); } Step2:写特性的相关代码 function randomLEDs() { console.log(“randomLEDs”); //state validation if (!connected) { alert(“Error: Discover and connect to a device before using this function”); return } if (!services_discovered)//有没有发现所有服务 { alert(“Error: Service discovery has not yet completed”); return } if (!has_led_service) { alert(“Error: The connected device does not contain the LED service”); return } if (!has_led_matrix_state)//led灯的特性有没有被发现的一个标记变量 { alert(“Error: The connected device does not contain the LED matrix state characteristic”); return; } var led_array = [0, 0, 0, 0, 0]; //math.random():0~1 math.floor(x):返回小于等于x的最大整数 led_array[0] = Math.floor(Math.random() * 32); led_array[1] = 1; led_array[2] = 2; led_array[3] = 3; led_array[4] = 4; var led_matrix_data = new Uint8Array(led_array); led_matrix_state.writeValue(led_matrix_data.buffer)//控制板子上灯亮灭 .then(_ => { console.log(‘LED matrix state changed’); }) .catch(error => { console.log(‘Error: ‘ + error); alert(‘Error: ‘ +error); return; }); } Step3:HTML相关代码的修改

Status

Connected Service Discovery Completed
false false

Device Discovery Or Disconnect

Start scan


Reading and Writing

Write Characteristic - Randomise Lights

Randomise LEDs

Read Characteristic - Model Number

Read Model Number


二,代码解析 1.你的蓝牙设备应该包含GATT设备信息服务,该服务至少应该包含模型名称字符串特征。我们将读取它的值并在web页面中显示结果。函数readModelNumber() 就是读特性所调用的函数。只有当我们成功连接到一个设备时,才能执行读取和写入特征值。在进行读或写操作之前,我们还需要知道所连接的设备包含所需的服务和特性。因此,服务发现也必须完成。因此,在readModelNumberl()和randomLEDs()函数开始时,我们将在开始时执行一些简单的防御性验证,以检查我们已经达到了所需的状态,以下代码就是防御性验证: if (!connected) { alert(“Error: Discover and connect to a device before using this function”); return; } if (!services_discovered) { alert(“Error: Service discovery has not yet completed”); return; } …… 2.完成防御性验证,我们就开始读值,然后将其打印出来。Uint8Array是8 位无符号整数值的类型化数组,将读到的值赋给data。TextDecoder接口表示用于特定文本编码的解码器,例如 UTF-8,ISO-8859-2,KOI8-R等。 data = new Uint8Array(value.buffer); model_number_string = new TextDecoder(“utf8”).decode(data); console.log(model_number_string); 3.如果前面的函数有出错的,.catch的作用是捕捉前一个函数的错误,并将错误打印出来,供编码者观看。 .catch(error => { console.log(‘Error: ‘ + error); alert(‘Error: ‘ + error); 4.对于写特性,我们将看到写入特征值与读取特征值是很相似的。我们将编写一个值到LED矩阵中,在BBC开发板上控制5 x5 LED矩阵LED开启和关闭。规则如下: 为了简单起见,我们将让代码生成一个范围为0到31的随机值就是通过函数math.random():0~31,math.floor(x):返回小于等于x的最大整数。当我们将它写入BBC micro:bit时,每当我们点击网页上的按钮时,LED矩阵就会显示一个随机的图案。led_array[1] = 1; led_array[2] = 2;…..这些值是写入的特定值,您可以控制这些值,选择权在你。 led_array[0] = Math.floor(Math.random() * 32); led_array[1] = 1; led_array[2] = 2; led_array[3] = 3; led_array[4] = 4; 5.在使用这些函数时,我们需要点击网页上Randomise LEDs,Read Model Number按钮,来调用这些函数。 Randomise LEDsRead Model Number 6.程序编辑好后打开,在浏览器中按F12,观察。 实验现象: 1.表示连接成功。 2.通过点击Randomise LEDs,来改变LED灯的亮灭。


一个好奇的人