From fefbfb5aae5ae714a9b1e21bf7de16d38d0833c7 Mon Sep 17 00:00:00 2001 From: "taekeun.kang" Date: Fri, 26 Jun 2015 15:59:21 +0900 Subject: [PATCH] [Web Device API] Remove a category for BLE and Merge a BLE tutorial into a bluetooth tutorial. Change-Id: I2cc91e5bae9529e34905b26a986353f6762c4c77 Signed-off-by: taekeun.kang --- .../html/web/tizen/communication/bluetooth_w.htm | 17 +- org.tizen.tutorials/html/index.htm | 1 - .../communication/bluetooth_le_tutorial_w.htm | 554 --------------------- .../tizen/communication/bluetooth_tutorial_w.htm | 395 +++++++++++++++ org.tizen.tutorials/index.xml | 1 - 5 files changed, 403 insertions(+), 565 deletions(-) mode change 100644 => 100755 org.tizen.guides/html/web/tizen/communication/bluetooth_w.htm delete mode 100644 org.tizen.tutorials/html/web/tizen/communication/bluetooth_le_tutorial_w.htm diff --git a/org.tizen.guides/html/web/tizen/communication/bluetooth_w.htm b/org.tizen.guides/html/web/tizen/communication/bluetooth_w.htm old mode 100644 new mode 100755 index fb8b891..4fa69f4 --- a/org.tizen.guides/html/web/tizen/communication/bluetooth_w.htm +++ b/org.tizen.guides/html/web/tizen/communication/bluetooth_w.htm @@ -23,7 +23,6 @@

Related Info

@@ -60,24 +59,24 @@
  • Managing the local Bluetooth adapter

    The Bluetooth adapter management is performed the same way as in regular Bluetooth API.

  • Discovering Bluetooth Low Energy Devices -

    You can discover Bluetooth Low Energy devices in range. +

    You can discover Bluetooth Low Energy devices in range. By this discovery process you will obtain basic information about available remote devices: their names, provided services etc.

  • Managing the Advertising Options -

    You can managing the advertising to control how your device announces itself to other Bluetooth Low Energy devices to be discovered.

  • +

    You can managing the advertising to control how your device announces itself to other Bluetooth Low Energy devices to be discovered.

  • Connecting to a Bluetooth Low Energy Device -

    You can connecting to a remote Bluetooth Low Energy device. When connected you can access services and characteristics of the remote device.

  • +

    You can connecting to a remote Bluetooth Low Energy device. When connected you can access services and characteristics of the remote device.

  • Receiving Notifications on Connection State Change -

    You can monitor the state of the connection to detect when the connection to the remote device is lost.

  • +

    You can monitor the state of the connection to detect when the connection to the remote device is lost.

  • Retrieving Bluetooth GATT Services -

    You can retrieve information about Bluetooth GATT Services provided by the remote device.

    +

    You can retrieve information about Bluetooth GATT Services provided by the remote device.

    Every GATT service defines characteristics it includes. By knowing the service you know what features the Bluetooth device exposes.

  • Accessing Bluetooth GATT Characteristic Value -

    You can read and write Bluetooth GATT characteristic value.

    +

    You can read and write Bluetooth GATT characteristic value.

    Characteristics allows you to monitor and sometimes control remote Bluetooth Low Energy devices. For example a sensor reading can be exposed by the sensor device as a Bluetooth GATT characteristic.

  • Receiving Notification on Characterictic Value Change -

    You can monitor changes of a characterictic value to detect change for example in sensor reading, battery level and so on.

  • +

    You can monitor changes of a characterictic value to detect change for example in sensor reading, battery level and so on.

  • Accessing Bluetooth GATT Descriptor Value -

    You can read and write Bluetooth GATT descriptor value.

  • +

    You can read and write Bluetooth GATT descriptor value.

    diff --git a/org.tizen.tutorials/html/index.htm b/org.tizen.tutorials/html/index.htm index dfd4c0f..a86fcc7 100644 --- a/org.tizen.tutorials/html/index.htm +++ b/org.tizen.tutorials/html/index.htm @@ -42,7 +42,6 @@
  • Task: Bluetooth Chat
  • -
  • Bluetooth Low Energy
  • NFC
    • Task: Contacts Exchanger
    • diff --git a/org.tizen.tutorials/html/web/tizen/communication/bluetooth_le_tutorial_w.htm b/org.tizen.tutorials/html/web/tizen/communication/bluetooth_le_tutorial_w.htm deleted file mode 100644 index 2959543..0000000 --- a/org.tizen.tutorials/html/web/tizen/communication/bluetooth_le_tutorial_w.htm +++ /dev/null @@ -1,554 +0,0 @@ - - - - - - - - - - - - - - Bluetooth: Managing a Bluetooth Low Evergy Device - - - - - - -
      -

      Bluetooth: Managing a Bluetooth Low Energy Device

      -
      - - -

      This tutorial demonstrates how you can manage Bluetooth Low Energy and exchange data with a peer device.

      - - - - - - - - - - - - - -
      Note
      The Bluetooth API is optional for both Tizen mobile and wearable profiles, which means that it may not be supported in all mobile and wearable devices.
      The Bluetooth API is not supported on any Tizen Emulators.
      - -

      Warm-up

      -

      Become familiar with the Bluetooth API basics by learning about:

      - - -
      - -
      - -
        -
      • -
        -

        Managing the Local Bluetooth Adapter

        - Hide -
        -
        - -

        To enable the Bluetooth device, request a built-in Settings application to present the relevant switch to the user so that they can enable or disable the Bluetooth. For details see the Managing the Local Bluetooth Adapter section of the Bluetooth Tutorial.

        - -
        -
      • -
      • -
        -

        Discovering Bluetooth Low Energy Devices

        - Hide -
        -
        - -

        Learning how to search for remote devices is a basic Bluetooth management skill:

        -
          -
        1. Define a scan events handler by implementing the BluetoothLEScanCallback (in mobile and wearable applications). - A callback is invoked to notify that the scan has started, a remote device has been detected, and when the scan is finished.

          -
          -var callbacks = {
          -   onstarted: function() {
          -      console.log("Scan started");
          -   },
          -   ondevicefound: function(device) {
          -      console.log("Found device: " + device.name + " [" + device.address + "]");
          -   },
          -   onfinished: function(devices) {
          -      console.log("Scan finished. Found " + devices.length + " devices.");
          -   }
          -};
          -
          - - - - - - - - - -
          Note
          To allow other Bluetooth devices to find your device, you must set the device to be visible through the system settings.
        2. - -
        3. Retrieve a BluetoothLEAdapter object with the getLEAdapter() method (in mobile and wearable applications) method:

          -
          var adapter = tizen.bluetooth.getLEAdapter();
          -
        4. - -
        5. To search for remote devices, use the startScan() method (in mobile and wearable applications) of the BluetoothLEAdapter interface.

          -
          adapter.startScan(callbacks);
          -
        6. -
        7. Finally, when you find the right remote device or the user cancels the scanning, disable the scan using the stopScan() method (in mobile and wearable applications) of the BluetoothLEAdapter interface.

          -
          adapter.stopScan();
          -
        8. - -
        - -
        -
      • - -
      • -
        -

        Managing the Advertising Options

        - Hide -
        -
        - -

        Learning how to control what information are advertised by the device is a usefull Bluetooth Low Energy skill:

        -

        Bluetooth Low Energy technology allows a device to broadcast some information without any need of connection between devices. Bluetooth Low Energy API provides methods to control this advertising/broadcasting.

        -
          -
        1. Retrieve a BluetoothLEAdapter object with the getLEAdapter() method (in mobile and wearable applications):

          -
          var adapter = tizen.bluetooth.getLEAdapter();
          -
        2. - -
        3. Setup options and start advertising with the startAdvertise() method (in mobile and wearable applications) of the BluetoothLEAdapter interface

          -
          -var advertiseData = new tizen.BluetoothLEAdvertiseData({
          -    includeName: true,
          -    serviceuuids: ["180f"]  // 180F is 16bit Battery Service UUID
          -});
          -var connectable = true;
          -
          -adapter.startAdvertise(
          -   advertiseData,
          -   "ADVERTISE",
          -   function onstate(state)
          -   {
          -      console.log("Advertising configured: " + state);
          -   },
          -   function(error)
          -   {
          -      console.log("startAdvertise() failed: " + error.message);
          -   },
          -   "LOW_LATENCY",
          -   connectable
          -);
          -
          - - - - - - - - - -
          Note
          To learn how to make your mobile device visible to other Bluetooth devices see the Managing the Local Bluetooth Adapter section of the Bluetooth Tutorial.
          -
        4. -
        5. To disable the advertising stopAdvertise() method (in mobile and wearable applications) of the BluetoothLEAdapter interface.

          -
          adapter.stopAdvertise();
          -
        6. -
        -
        -
      • - -
      • -
        -

        Connecting to a Bluetooth Low Energy Device

        - Hide -
        -
        - -

        Learning how to connect to other devices is a basic Bluetooth Low Energy management skill:

        -
          -
        1. Retrieve a BluetoothLEAdapter object with the getLEAdapter() method (in mobile and wearable applications): -

          -
          var adapter = tizen.bluetooth.getLEAdapter();
          -
        2. -
        3. Define a success and error callbacks for a connect operation:

          -
          -function connectFail(error) {
          -     console.log("Failed to connect to device: " + e.message);
          -}
          -
          -function connectSuccess() {
          -    console.log("Connected to device");
          -}
          -
          -
        4. - -
        5. Define a callback for scan operation which will connect to any device found and stop the scan.

          -

          Within the callback request establishing a connection with the found device using the connect() method (in mobile and wearable applications) of the BluetoothLEDevice interface (in mobile and wearable applications): -

          -var remoteDevice = null;
          -
          -function onDeviceFound(device)
          -{
          -   if (remoteDevice === null) {
          -      remoteDevice = device;
          -      console.log("Found device " + device.name + ". Connecting...");
          -
          -      device.connect(connectSuccess, connectFail);
          -   }
          -
          -   adapter.stopScan();
          -}
          -
          -
        6. - -
        7. Having all callbacks ready, initiate the Bluetooth Low Energy scan using the startScan() method (in mobile and wearable applications) of the BluetoothLEAdapter adapter:

          -
          adapter.startScan({
          -   ondevicefound: onDeviceFound
          -});
        8. - -
        9. When the connection to the remote device is no longer required, disconnect from the device by calling the disconnect() method (in mobile and wearable applications) of the BluetoothLEDevice interface:

          -
          remoteDevice.disconnect();
        10. - -
        -
        -
      • -
      • -
        -

        Receiving Notifications on Connection State Change

        -Hide -
        -
        - -

        Learning how to receive notifications whenever the device connection is established or lost is a useful Bluetooth management skill:

        -
          - -
        1. Retrieve a BluetoothLEAdapter object with the getLEAdapter() method (in mobile and wearable applications): -

          -
          var adapter = tizen.bluetooth.getLEAdapter();
          -
        2. -
        3. Define a connection state change listener by implementing the BluetoothLEConnectChangeCallback (in mobile and wearable applications):

          -
          -var connectionListener = {
          -    onconnected: function(device) {
          -        console.log("Connected to the device: " + device.name + " [" + device.address + "]");
          -    },
          -    ondisconnected: function(device) {
          -        console.log("Disconnected from the device " + device.name + " [" + device.address + "]");
          -    }
          -};
          -
          -
        4. - -
        5. Define a callback for scan operation which will connect to any device found and stop the scan.

          -

          Within the callback register a connection state change listener using the addConnectStateChangeListener() method (in mobile and wearable applications) of the BluetoothLEDevice interface: -

          -var remoteDevice = null;
          -var watchId;
          -
          -function onDeviceFound(device)
          -{
          -   if (remoteDevice === null) {
          -      remoteDevice = device;
          -      console.log("Found device " + device.name + ". Connecting...");
          -
          -      watchId = remoteDevice.addConnectStateChangeListener(connectionListener);
          -
          -      remoteDevice.connect();
          -   }
          -
          -   adapter.stopScan();
          -}
          -
          -
        6. - -
        7. Having all callbacks ready, initiate the Bluetooth Low Energy scan:

          -
          adapter.startScan({
          -   ondevicefound: onDeviceFound
          -});
        8. - -
        9. When the notifications about the connection are no longer required, unregister the listener from the device by calling the removeConnectStateChangeListener() method (in mobile and wearable applications) of the BluetoothLEDevice interface:

          -
          remoteDevice.removeConnectStateChangeListener(watchId);
        10. -
        -
        -
      • -
      • -
        -

        Retrieving Bluetooth GATT Services

        -Hide -
        -
        - -

        Learning how to retrieve a list of GATT services (Generic Attribute) provided by a device is basic Bluetooth Low Energy management skill:

        -
          - -
        1. Connect to any Bluetooth Low Energy device as described in the Connecting to a Bluetooth Low Energy Device section.
        2. -
        3. Define a connection state change listener by implementing the BluetoothLEConnectChangeCallback (in mobile and wearable applications):

          -
          -function showGATTService(service, indent)
          -{
          -   if (indent === undefined) {
          -      indent = "";
          -   }
          -
          -   console.log(indent + "Service " + service.uuid + ". Has " + service.characteristics.length + " characteristics and " + service.services.length + " sub-services.");
          -
          -   for (var i = 0; i < service.services.length; i++) {
          -      showGATTService(service.services[i], indent + "   ");
          -   }
          -}
          -
          -
        4. -
        5. Retrieve a list of GATT service uuids from the uuids attribute (in mobile and wearable applications) of the BluetoothLEDevice interface:

          -
          var serviceUUIDs = remoteDevice.uuids;
          -
        6. -
        7. Retrieve GATT service information using the getService() method (in mobile and wearable applications) of the BluetoothLEDevice interface for every service uuid:

          -
          -var i = 0, service = null;
          -
          -for (i; i < serviceUUIDs.length; i++) {
          -
          -   service = remoteDevice.getService(serviceUUIDs[i]);
          -
          -   showGATTService(service);
          -}
          -
        8. - -
        -
        -
      • -
      • -
        -

        Accessing Bluetooth GATT Characteristic Value

        -Hide -
        -
        - -

        Learning how to read and write a value of Bluetooth Characteristic is a useful Bluetooth Low Energy management skill:

        -
          - -
        1. Connect to any Bluetooth Low Energy device as described in the Connecting to a Bluetooth Low Energy Device section.

        2. -
        3. Retrieve a list of GATT service uuids from the uuids attribute (in mobile and wearable applications) of the BluetoothLEDevice interface:

          -
          var serviceUUIDs = remoteDevice.uuids;
          -
        4. -
        5. Select one of the GATT services and use the getService() method (in mobile and wearable applications) of the BluetoothLEDevice interface to retrieve an object representing the service. In this example we use the first service:

          -
          var gattService = remoteDevice.getService(serviceUUIDs[0]);
          -
        6. -
        7. Select the interesting characteristic from the characteristics attribute (in mobile and wearable applications) of the BluetoothGATTService interface (in mobile and wearable applications). In this example we use the first of them:

          -
          var property = gattService.characteristics[0];
          -
        8. -
        9. Define a callback implementing the ReadValueSuccessCallback which (in mobile and wearable applications) will receive the value of the characteristic:

          -
          -function readSuccess(value)
          -{
          -   console.log("Characteristic value: " + value);
          -}
          -
          -function readFail(error)
          -{
          -   console.log("readValue() failed: " + error);
          -}
          -
          -
        10. -
        11. To retrieve GATT Characteristic value, use the readValue() method (in mobile and wearable applications) of the BluetoothGATTCharacteristic interface:

          -
          -if (!property.isReadable) {
          -   console.log("Property seems not to be readable. Attempting to read...");
          -}
          -property.readValue(readSuccess, readFail);
          -
        12. -
        13. To change the characteristic value, define callbacks and use the writeValue() method (in mobile and wearable applications) of the BluetoothGATTCharacteristic interface:

          -
          -function writeSuccess(value)
          -{
          -   console.log("Written");
          -}
          -
          -function writeFail(error)
          -{
          -   console.log("writeValue() failed: " + error);
          -}
          -
          -if (!property.isWritable) {
          -   console.log("Property seems not to be writable. Attempting to write...");
          -}
          -var newValue = [82];
          -
          -property.writeValue(newValue, writeSuccess, writeFail);
          -
          -
        14. -
        -
        -
      • -
      • -
        -

        Receiving Notification on Characterictic Value Change

        - Hide -
        -
        - -

        Learning how to monitor a changes of a Bluetooth characteristic is a useful Bluetooth Low Energy management skill:

        -
          - -
        1. Connect to any Bluetooth Low Energy device as described in the Connecting to a Bluetooth Low Energy Device section.

        2. -
        3. Retrieve a list of GATT service uuids from the uuids attribute (in mobile and wearable applications) of the BluetoothLEDevice interface:

          -
          var serviceUUIDs = remoteDevice.uuids;
          -
        4. -
        5. Select one of the GATT services and use the getService() method (in mobile and wearable applications) of the BluetoothLEDevice interface to retrieve an object representing the service. In this example we use the first service:

          -
          var gattService = remoteDevice.getService(serviceUUIDs[0]);
          -
        6. -
        7. Select the interesting characteristic from the characteristics attribute (in mobile and wearable applications) of the BluetoothGATTService interface (in mobile and wearable applications). In this example we use the first of them:

          -
          var property = gattService.characteristics[0];
          -
        8. -
        9. Define a callback implementing the ReadValueSuccessCallback which (in mobile and wearable applications) will receive the value of the characteristic every time the value changes:

          -
          -function onValueChange(value)
          -{
          -   console.log("Characteristic value is now: " + value);
          -}
          -
          -
        10. - -
        11. Register a value change listener using the addValueChangeListener() method (in mobile and wearable applications) of the BluetoothGATTCharacteristic interface:

          -
          var watchId = property.addValueChangeListener(onValueChange);
        12. - -
        13. When the notifications about the connection are no longer required, unregister the listener from the device by calling the removeValueChangeListener() method (in mobile and wearable applications) of the BluetoothGATTCharacteristic interface:

          -
          property.removeValueChangeListener(watchId);
        14. -
        - -
        -
      • - -
      • -
        -

        Accessing Bluetooth GATT Descriptor Value

        - Hide -
        -
        - -

        Learning how to read and write a value of Bluetooth Descriptor is a useful Bluetooth Low Energy management skill:

        -
          - -
        1. Connect to any Bluetooth Low Energy device as described in the Connecting to a Bluetooth Low Energy Device section.

        2. -
        3. Retrieve a list of GATT service uuids from the uuids attribute (in mobile and wearable applications) of the BluetoothLEDevice interface:

          -
          var serviceUUIDs = remoteDevice.uuids;
          -
        4. -
        5. Select one of the GATT services and use the getService() method (in mobile and wearable applications) of the BluetoothLEDevice interface to retrieve an object representing the service. In this example we use the first service:

          -
          var gattService = remoteDevice.getService(serviceUUIDs[0]);
          -
        6. -
        7. Select the interesting characteristic from the characteristics attribute (in mobile and wearable applications) of the BluetoothGATTService interface (in mobile and wearable applications). In this example we use the first of them:

          -
          var characteristic = gattService.characteristics[0];
          -
        8. Select the interesting descriptor from the descriptors attribute (in mobile and wearable applications) of the BluetoothGATTCharacteristic interface (in mobile and wearable applications). In this example we use the first of them:

          -
          var descriptor = characteristic.descriptors[0];
          -
        9. -
        10. Define a callback implementing the ReadValueSuccessCallback (in mobile and wearable applications) which will receive the value of the descriptor:

          -
          -function readSuccess(value)
          -{
          -   console.log("Descriptor value: " + value);
          -}
          -
          -function readFail(error)
          -{
          -   console.log("readValue() failed: " + error);
          -}
          -
          -
        11. -
        12. To retrieve GATT Descriptor value, use the readValue() method (in mobile and wearable applications) of the BluetoothGATTDescriptor interface:

          -
          -descriptor.readValue(readSuccess, readFail);
          -
        13. -
        14. To change the descriptor value, define callbacks and use the writeValue() method (in mobile and wearable applications) of the BluetoothGATTDescriptor interface:

          -
          -function writeSuccess(value)
          -{
          -   console.log("Written");
          -}
          -
          -function writeFail(error)
          -{
          -   console.log("writeValue() failed: " + error);
          -}
          -
          -var newValue = [3];
          -
          -descriptor.writeValue(newValue, writeSuccess, writeFail);
          -
          -
        15. -
        - -
        -
      • - -
      -
      -
      - - - - -
      - -Go to top - - - - - - - diff --git a/org.tizen.tutorials/html/web/tizen/communication/bluetooth_tutorial_w.htm b/org.tizen.tutorials/html/web/tizen/communication/bluetooth_tutorial_w.htm index d0841c5..60352e3 100644 --- a/org.tizen.tutorials/html/web/tizen/communication/bluetooth_tutorial_w.htm +++ b/org.tizen.tutorials/html/web/tizen/communication/bluetooth_tutorial_w.htm @@ -28,6 +28,17 @@
    • Creating a Bonding with a Bluetooth Device
    • Connecting to and Exchanging Data with a Bluetooth Device
    • Communicating with a Health Source Device
    • +
    • Bluetooth Low Energy
    • +

    Related Info

    Task

    In the Bluetooth Chat task, we will walk through how to use the device as a Bluetooth server or client in a chat application.

    @@ -319,6 +341,379 @@ healthChannel.close();

    When the channel is disconnected, the onclose event handler in the BluetoothHealthChannelChangeCallback interface is called.

  • + +

    Discovering Bluetooth Low Energy Devices

    + +

    Learning how to search for remote devices is a basic Bluetooth management skill:

    +
      +
    1. Define a scan event handler by implementing the BluetoothLEScanCallback callback (in mobile and wearable applications). +

      The callback is invoked when a remote device has been detected.

      +
      +var callbacks =
      +{
      +   onsuccess: function(device)
      +   {
      +      console.log("Found device: " + device.name + " [" + device.address + "]");
      +   }
      +};
      +
      + + + + + + + + + +
      Note
      To allow other Bluetooth devices to find your device, you must set the device to be visible through the system settings.
    2. + +
    3. Retrieve a BluetoothLEAdapter object (in mobile and wearable applications) with the getLEAdapter() method of the BluetoothManager interface (in mobile and wearable applications):

      +
      var adapter = tizen.bluetooth.getLEAdapter();
      +
    4. + +
    5. To search for remote devices, use the startScan() method of the BluetoothLEAdapter interface:

      +
      adapter.startScan(callbacks);
      +
    6. +
    7. When you find the right remote device or the user cancels the scanning, disable the scan using the stopScan() method of the BluetoothLEAdapter interface:

      +
      adapter.stopScan();
      +
    8. + +
    + +

    Managing the Advertising Options

    +

    The Bluetooth Low Energy technology allows a device to broadcast some information without a connection between devices. The Bluetooth Low Energy API provides methods to control this advertising (broadcasting).

    +

    Learning how to control what information is advertised by the device is a useful Bluetooth Low Energy skill:

    + +
      +
    1. Retrieve a BluetoothLEAdapter object (in mobile and wearable applications) with the getLEAdapter() method of the BluetoothManager interface (in mobile and wearable applications):

      +
      var adapter = tizen.bluetooth.getLEAdapter();
      +
    2. + +
    3. Set up options and start advertising with the startAdvertise() method of the BluetoothLEAdapter interface:

      +
      +var advertiseData = new tizen.BluetoothLEAdvertiseData(
      +{
      +   includeName: true,
      +   serviceuuids: ["180f"]  /* 180F is 16bit Battery Service UUID */
      +});
      +var connectable = true;
      +
      +adapter.startAdvertise(advertiseData, "ADVERTISE",
      +                       function onstate(state)
      +                       {
      +                          console.log("Advertising configured: " + state);
      +                       },
      +                       function(error)
      +                       {
      +                          console.log("startAdvertise() failed: " + error.message);
      +                       },
      +                       "LOW_LATENCY", connectable);
      +
      + + + + + + + + + +
      Note
      To learn how to make your mobile device visible to other Bluetooth devices, see Managing the Local Bluetooth Adapter of the Bluetooth Tutorial.
      +
    4. +
    5. To disable the advertising, use the stopAdvertise() method of the BluetoothLEAdapter interface:

      +
      adapter.stopAdvertise();
      +
    6. +
    + +

    Connecting to a Bluetooth Low Energy Device

    + +

    Learning how to connect to other devices is a basic Bluetooth Low Energy management skill:

    +
      +
    1. Retrieve a BluetoothLEAdapter object (in mobile and wearable applications) with the getLEAdapter() method of the BluetoothManager interface (in mobile and wearable applications):

      +
      var adapter = tizen.bluetooth.getLEAdapter();
      +
    2. +
    3. Define success and error callbacks for the connect operation:

      +
      +function connectFail(error)
      +{
      +   console.log("Failed to connect to device: " + e.message);
      +}
      +
      +function connectSuccess()
      +{
      +   console.log("Connected to device");
      +}
      +
      +
    4. + +
    5. Define a callback for the scan operation that connects to a found device and stops the scan.

      +

      Within the callback request, establish a connection with the found device using the connect() method of the BluetoothLEDevice interface (in mobile and wearable applications):

      +
      +var remoteDevice = null;
      +
      +function onDeviceFound(device)
      +{
      +   if (remoteDevice === null)
      +   {
      +      remoteDevice = device;
      +      console.log("Found device " + device.name + ". Connecting...");
      +
      +      device.connect(connectSuccess, connectFail);
      +   }
      +
      +   adapter.stopScan();
      +}
      +
      +
    6. + +
    7. When the callbacks are completed, initiate the Bluetooth Low Energy scan using the startScan() method of the BluetoothLEAdapter adapter:

      +
      adapter.startScan({onsuccess: onDeviceFound});
    8. + +
    9. When the connection to the remote device is no longer required, disconnect from the device by calling the disconnect() method of the BluetoothLEDevice interface:

      +
      remoteDevice.disconnect();
    10. + +
    + +

    Receiving Notifications on Connection State Changes

    + +

    Learning how to receive notifications whenever the device connection is established or lost is a useful Bluetooth management skill:

    +
      + +
    1. Retrieve a BluetoothLEAdapter object (in mobile and wearable applications) with the getLEAdapter() method of the BluetoothManager interface (in mobile and wearable applications):

      +
      var adapter = tizen.bluetooth.getLEAdapter();
      +
    2. +
    3. Define a connection state change listener by implementing the BluetoothLEConnectChangeCallback callback (in mobile and wearable applications):

      +
      +var connectionListener =
      +{
      +   onconnected: function(device)
      +   {
      +      console.log("Connected to the device: " + device.name + " [" + device.address + "]");
      +   },
      +   ondisconnected: function(device)
      +   {
      +      console.log("Disconnected from the device " + device.name + " [" + device.address + "]");
      +   }
      +};
      +
      +
    4. + +
    5. Define a callback for the scan operation that connects to a found device and stops the scan.

      +

      Within the callback, register a connection state change listener using the addConnectStateChangeListener() method of the BluetoothLEDevice interface (in mobile and wearable applications):

      +
      +var remoteDevice = null;
      +var watchId;
      +
      +function onDeviceFound(device)
      +{
      +   if (remoteDevice === null)
      +   {
      +      remoteDevice = device;
      +      console.log("Found device " + device.name + ". Connecting...");
      +
      +      watchId = remoteDevice.addConnectStateChangeListener(connectionListener);
      +
      +      remoteDevice.connect();
      +   }
      +
      +   adapter.stopScan();
      +}
      +
      +
    6. + +
    7. When the callbacks are completed, initiate the Bluetooth Low Energy scan:

      +
      adapter.startScan({onsuccess: onDeviceFound});
    8. + +
    9. When the notifications about the connection are no longer required, unregister the listener from the device by calling the removeConnectStateChangeListener() method of the BluetoothLEDevice interface:

      +
      remoteDevice.removeConnectStateChangeListener(watchId);
    10. +
    + +

    Retrieving Bluetooth GATT Services

    + +

    Learning how to retrieve a list of GATT services (Generic Attribute) provided by a remote device is basic Bluetooth Low Energy management skill:

    +
      + +
    1. Connect to a Bluetooth Low Energy device.
    2. +
    3. Define a connection state change listener by implementing the BluetoothLEConnectChangeCallback (in mobile and wearable applications):

      +
      +function showGATTService(service, indent)
      +{
      +   if (indent === undefined)
      +   {
      +      indent = "";
      +   }
      +
      +   console.log(indent + "Service " + service.uuid + ". Has " + service.characteristics.length
      +               + " characteristics and " + service.services.length + " sub-services.");
      +
      +   for (var i = 0; i < service.services.length; i++)
      +   {
      +      showGATTService(service.services[i], indent + "   ");
      +   }
      +}
      +
      +
    4. +
    5. Retrieve a list of GATT service UUIDs from the uuids attribute of the BluetoothLEDevice interface (in mobile and wearable applications):

      +
      var serviceUUIDs = remoteDevice.uuids;
      +
    6. +
    7. Retrieve GATT service information using the getService() method of the BluetoothLEDevice interface for every service UUID:

      +
      +var i = 0, service = null;
      +
      +for (i; i < serviceUUIDs.length; i++)
      +{
      +
      +   service = remoteDevice.getService(serviceUUIDs[i]);
      +
      +   showGATTService(service);
      +}
      +
    8. + +
    + +

    Accessing the Bluetooth GATT Characteristic Value

    + +

    Learning how to read and write a value of the Bluetooth characteristic is a useful Bluetooth Low Energy management skill:

    +
      + +
    1. Connect to a Bluetooth Low Energy device.
    2. +
    3. Retrieve a list of GATT service UUIDs from the uuids attribute of the BluetoothLEDevice interface (in mobile and wearable applications):

      +
      var serviceUUIDs = remoteDevice.uuids;
      +
    4. +
    5. Select a GATT service and use the getService() method of the BluetoothLEDevice interface to retrieve an object representing the service. In this example, the first service is used:

      +
      var gattService = remoteDevice.getService(serviceUUIDs[0]);
      +
    6. +
    7. Select an interesting characteristic from the characteristics attribute of the BluetoothGATTService interface (in mobile and wearable applications). In this example, the first characteristic is used:

      +
      var property = gattService.characteristics[0];
      +
    8. +
    9. Define a callback implementing the ReadValueSuccessCallback callback (in mobile and wearable applications), which receives the value of the characteristic:

      +
      +function readSuccess(value)
      +{
      +   console.log("Characteristic value: " + value);
      +}
      +
      +function readFail(error)
      +{
      +   console.log("readValue() failed: " + error);
      +}
      +
      +
    10. +
    11. To retrieve the GATT characteristic value, use the readValue() method of the BluetoothGATTCharacteristic interface (in mobile and wearable applications):

      +
      +if (!property.isReadable)
      +{
      +   console.log("Property seems not to be readable. Attempting to read...");
      +}
      +property.readValue(readSuccess, readFail);
      +
    12. +
    13. To change the characteristic value, define callbacks and use the writeValue() method of the BluetoothGATTCharacteristic interface:

      +
      +function writeSuccess(value)
      +{
      +   console.log("Written");
      +}
      +
      +function writeFail(error)
      +{
      +   console.log("writeValue() failed: " + error);
      +}
      +
      +if (!property.isWritable)
      +{
      +   console.log("Property seems not to be writable. Attempting to write...");
      +}
      +var newValue = [82];
      +
      +property.writeValue(newValue, writeSuccess, writeFail);
      +
      +
    14. +
    + +

    Receiving Notifications on Characteristic Value Changes

    + +

    Learning how to monitor a changes in a Bluetooth characteristic is a useful Bluetooth Low Energy management skill:

    +
      + +
    1. Connect to a Bluetooth Low Energy device.
    2. +
    3. Retrieve a list of GATT service UUIDs from the uuids attribute of the BluetoothLEDevice interface (in mobile and wearable applications):

      +
      var serviceUUIDs = remoteDevice.uuids;
      +
    4. +
    5. Select a GATT service and use the getService() method of the BluetoothLEDevice interface to retrieve an object representing the service. In this example, the first service is used:

      +
      var gattService = remoteDevice.getService(serviceUUIDs[0]);
      +
    6. +
    7. Select an interesting characteristic from the characteristics attribute of the BluetoothGATTService interface (in mobile and wearable applications). In this example, the first characteristic is used:

      +
      var property = gattService.characteristics[0];
      +
    8. +
    9. Define a callback implementing the ReadValueSuccessCallback callback (in mobile and wearable applications), which receives the value of the characteristic every time the value changes:

      +
      +function onValueChange(value)
      +{
      +   console.log("Characteristic value is now: " + value);
      +}
      +
      +
    10. + +
    11. Register a value change listener using the addValueChangeListener() method of the BluetoothGATTCharacteristic interface (in mobile and wearable applications):

      +
      var watchId = property.addValueChangeListener(onValueChange);
    12. + +
    13. When the notifications about the connection are no longer required, unregister the listener from the device by calling the removeValueChangeListener() method of the BluetoothGATTCharacteristic interface:

      +
      property.removeValueChangeListener(watchId);
    14. +
    + +

    Accessing the Bluetooth GATT Descriptor Value

    + +

    Learning how to read and write a value of the Bluetooth descriptor is a useful Bluetooth Low Energy management skill:

    +
      + +
    1. Connect to a Bluetooth Low Energy device.
    2. +
    3. Retrieve a list of GATT service UUIDs from the uuids attribute of the BluetoothLEDevice interface (in mobile and wearable applications):

      +
      var serviceUUIDs = remoteDevice.uuids;
      +
    4. +
    5. Select a GATT service and use the getService() method of the BluetoothLEDevice interface to retrieve an object representing the service. In this example, the first service is used:

      +
      var gattService = remoteDevice.getService(serviceUUIDs[0]);
      +
    6. +
    7. Select an interesting characteristic from the characteristics attribute of the BluetoothGATTService interface (in mobile and wearable applications). In this example, the first characteristic is used:

      +
      var characteristic = gattService.characteristics[0];
    8. +
    9. Select an interesting descriptor from the descriptors attribute of the BluetoothGATTCharacteristic interface (in mobile and wearable applications). In this example, the first descriptor is used:

      +
      var descriptor = characteristic.descriptors[0];
      +
    10. +
    11. Define a callback implementing the ReadValueSuccessCallback callback (in mobile and wearable applications), which receives the value of the descriptor:

      +
      +function readSuccess(value)
      +{
      +   console.log("Descriptor value: " + value);
      +}
      +
      +function readFail(error)
      +{
      +   console.log("readValue() failed: " + error);
      +}
      +
      +
    12. +
    13. To retrieve the GATT descriptor value, use the readValue() method of the BluetoothGATTDescriptor interface (in mobile and wearable applications):

      +
      +descriptor.readValue(readSuccess, readFail);
      +
    14. +
    15. To change the descriptor value, define callbacks and use the writeValue() method of the BluetoothGATTDescriptor interface:

      +
      +function writeSuccess(value)
      +{
      +   console.log("Written");
      +}
      +
      +function writeFail(error)
      +{
      +   console.log("writeValue() failed: " + error);
      +}
      +
      +var newValue = [3];
      +
      +descriptor.writeValue(newValue, writeSuccess, writeFail);
      +
      +
    16. +
    diff --git a/org.tizen.tutorials/index.xml b/org.tizen.tutorials/index.xml index 50c6ee9..907036a 100644 --- a/org.tizen.tutorials/index.xml +++ b/org.tizen.tutorials/index.xml @@ -16,7 +16,6 @@ - -- 2.7.4