基于NativeScript实现与蓝牙LE外设的连接与交互

NativeScript 应用程序可以通过 @nativescript-community/ble 插件与蓝牙LE外设进行连接和交互。

安装插件

在命令提示符中,进入应用程序的根目录,执行以下命令安装插件:

tns plugin add @nativescript-community/ble

添加 TypeScript 支持

为了便于开发,建议在NativeScript应用程序中添加 TypeScript 支持:

tns install typescript

API 使用

以下是一些常用的 API 功能:

  • isBluetoothEnabled: 检查蓝牙是否已启用。
import { Bluetooth } from '@nativescript-community/ble';

const bluetooth = new Bluetooth();

bluetooth.isBluetoothEnabled().then(enabled => {
    console.log(`蓝牙已启用: ${enabled}`);
});
  • 发现外设: 扫描并发现附近的蓝牙LE外设。

  • 连接: 连接到指定的外设。

  • 交互: 与连接的外设进行数据交互,例如读取和写入数据。

总结

通过 @nativescript-community/ble 插件,开发者可以轻松地使用 NativeScript 构建与蓝牙LE外设进行通信的应用程序。

folder
ble:连接到蓝牙LE外设并与之交互 预估大小:229个文件
file
main.css 106KB
file
main.js 46KB
file
index.html 183KB
file
globals.html 182KB
file
bluetooth.html 270KB
file
app.css 979B
file
app.gradle 486B
file
svcList.component.html 1KB
file
peripheralList.component.html 974B
file
scan.component.html 2KB
file
actionBar.component.html 554B
file
characteristicList.component.html 5KB
file
bluetooth.gif 4KB
file
nativescript_bluetooth.aar 1KB
file
.gitignore 230B
file
app.css 927B
file
app.gradle 578B
file
services-page.css 165B
file
characteristics-page.css 702B
file
.eslintignore 34B
zip 文件大小:5.11MB