简体   繁体   English

具有BLE功能的ionic 2应用程序(蓝牙)

[英]ionic 2 app with BLE functionality (Bluetooth)

I'm trying to make an ionic 2 app that lists all available bluetooth devices and simply just connects with them. 我正在尝试制作一个ionic 2应用程序,该应用程序列出所有可用的蓝牙设备,并仅与它们连接。 I have created a blank ionic --v2 project and installed the BLE plugin 我创建了一个空白的ionic --v2项目并安装了BLE插件

Please assist, thanks! 请协助,谢谢!

You want to list all available bluetooth devices and connect with them. 您要列出所有可用的蓝牙设备并与其连接。

This is an example of how you can call the bluetooth api. 这是如何调用蓝牙api的示例。 Or you can also take a look at the examples given by the author. 或者,您也可以查看作者提供的示例。

var deviceToConnect = "12:34:56:78";
ble.scan([], 10, onSuccess, onFailure);

function onSuccess(device){
  console.log(device);
  if (device.id == deviceToConnect){
    ble.connect(device.id, connectSuccess, connectFailure);
  }
}

function onFailure(error){
  console.log(error);
}

function connectSuccess(){...}
function connectFailure(){...}

If you are still unsure, maybe try out a simpler api call such as ble.isEnabled() which will return you success or failure callback. 如果仍然不确定,请尝试使用更简单的api调用,例如ble.isEnabled() ,它将返回成功或失败回调。

I'm doing the same thing, but I'm guessing that you're not very familiar with the language. 我在做同样的事情,但是我猜你对这种语言不是很熟悉。

First of all you have to import the Plugin in your page (ie home.ts or any other page you're working in) by typing this: 首先,您必须通过键入以下命令将插件导入页面(即home.ts或您正在使用的任何其他页面)中:

import { BLE } from '@ionic-native/ble';

As written in the docs there are many functions you can call from the plugin, for example BLE.isEnabled() that returns a promise resolved if the Bluetooth is enabled on the device. 文档中所述,您可以从插件调用许多函数,例如BLE.isEnabled() ,如果设备上启用了蓝牙,则返回已解决的promise

The docs are pretty clear, if you want more examples you can check the github repo . 该文档非常清楚,如果您需要更多示例,可以查看github repo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM