|
| 1 | +/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/* Copyright (C) 2015 Sandeep Mistry [email protected] |
| 17 | + * |
| 18 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 19 | + * of this software and associated documentation files (the "Software"), to deal |
| 20 | + * in the Software without restriction, including without limitation the rights |
| 21 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 22 | + * copies of the Software, and to permit persons to whom the Software is |
| 23 | + * furnished to do so, subject to the following conditions: |
| 24 | + * |
| 25 | + * The above copyright notice and this permission notice shall be included in |
| 26 | + * all copies or substantial portions of the Software. |
| 27 | + * |
| 28 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 30 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 31 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 32 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 33 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 | + * SOFTWARE. |
| 35 | + */ |
| 36 | + |
| 37 | +var noble = require('../..'); |
| 38 | + |
| 39 | +var echoServiceUuid = 'ec00'; |
| 40 | +var echoCharacteristicUuid = 'ec0e'; |
| 41 | + |
| 42 | +noble.on('stateChange', function(state) { |
| 43 | + if (state === 'poweredOn') { |
| 44 | + // |
| 45 | + // Once the BLE radio has been powered on, it is possible |
| 46 | + // to begin scanning for services. Pass an empty array to |
| 47 | + // scan for all services (uses more time and power). |
| 48 | + // |
| 49 | + console.log('scanning...'); |
| 50 | + noble.startScanning([echoServiceUuid], false); |
| 51 | + } |
| 52 | + else { |
| 53 | + noble.stopScanning(); |
| 54 | + } |
| 55 | +}) |
| 56 | + |
| 57 | +var echoService = null; |
| 58 | +var echoCharacteristic = null; |
| 59 | + |
| 60 | +noble.on('discover', function(peripheral) { |
| 61 | + // we found a peripheral, stop scanning |
| 62 | + noble.stopScanning(); |
| 63 | + |
| 64 | + // |
| 65 | + // The advertisment data contains a name, power level (if available), |
| 66 | + // certain advertised service uuids, as well as manufacturer data, |
| 67 | + // which could be formatted as an iBeacon. |
| 68 | + // |
| 69 | + console.log('found peripheral:', peripheral.advertisement); |
| 70 | + // |
| 71 | + // Once the peripheral has been discovered, then connect to it. |
| 72 | + // It can also be constructed if the uuid is already known. |
| 73 | + /// |
| 74 | + peripheral.connect(function(err) { |
| 75 | + // |
| 76 | + // Once the peripheral has been connected, then discover the |
| 77 | + // services and characteristics of interest. |
| 78 | + // |
| 79 | + peripheral.discoverServices([echoServiceUuid], function(err, services) { |
| 80 | + services.forEach(function(service) { |
| 81 | + // |
| 82 | + // This must be the service we were looking for. |
| 83 | + // |
| 84 | + console.log('found service:', service.uuid); |
| 85 | + |
| 86 | + // |
| 87 | + // So, discover its characteristics. |
| 88 | + // |
| 89 | + service.discoverCharacteristics([], function(err, characteristics) { |
| 90 | + |
| 91 | + characteristics.forEach(function(characteristic) { |
| 92 | + // |
| 93 | + // Loop through each characteristic and match them to the |
| 94 | + // UUIDs that we know about. |
| 95 | + // |
| 96 | + console.log('found characteristic:', characteristic.uuid); |
| 97 | + |
| 98 | + if (echoCharacteristicUuid == characteristic.uuid) { |
| 99 | + echoCharacteristic = characteristic; |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + // |
| 104 | + // Check to see if we found all of our characteristics. |
| 105 | + // |
| 106 | + if (echoCharacteristic) { |
| 107 | + bakePizza(); |
| 108 | + } |
| 109 | + else { |
| 110 | + console.log('missing characteristics'); |
| 111 | + } |
| 112 | + }) |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | +}) |
| 117 | + |
| 118 | +function bakePizza() { |
| 119 | + var crust = new Buffer("Hello BLE Service."); |
| 120 | + echoCharacteristic.write(crust, false, function(err) { |
| 121 | + if (!err) { |
| 122 | + echoCharacteristic.read(function (err, buffer) { |
| 123 | + if (!err) { |
| 124 | + console.log('BLE says' + buffer.toString()); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + else { |
| 129 | + console.log('crust error'); |
| 130 | + } |
| 131 | + }) |
| 132 | +} |
0 commit comments