简体   繁体   中英

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. I have created a blank ionic --v2 project and installed the BLE plugin

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. 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.

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:

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.

The docs are pretty clear, if you want more examples you can check the github repo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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