すべてのプロダクト
Search
ドキュメントセンター

Mobile Platform as a Service:Bluetooth API の概要

最終更新日:Jan 17, 2025

バージョン要件

Bluetooth

mPaaS バージョン

Android/iOS

Bluetooth Low Energy (BLE)

mPaaS 10.1.60 以降

  • Android: 5.0 以降

  • iOS: 要件なし

Traditional Bluetooth

mPaaS 10.1.60 以降

-

処理フロー

BLE

BLE

Traditional Bluetooth

traditional

Bluetooth API

Bluetooth Low Energy

API

説明

my.connectBLEDevice

Bluetooth Low Energy デバイスに接続します。

my.disconnectBLEDevice

Bluetooth Low Energy デバイスとの接続を切断します。

my.getBLEDeviceCharacteristics

Bluetooth Low Energy デバイスの特性を取得します。

my.getBLEDeviceServices

検出されたすべての Bluetooth Low Energy デバイス (接続されているデバイスを含む) を取得します。

my.notifyBLECharacteristicValueChange

特性値の変更を通知する機能を有効にします。

my.offBLECharacteristicValueChange

特性値の変更を通知する機能を無効にします。

my.offBLEConnectionStateChanged

接続状態変更イベントのリスナーを無効にします。

my.onBLECharacteristicValueChange

特性値変更イベントのリスナーを有効にします。

my.onBLEConnectionStateChanged

デバイスの紛失やデバイスの切断など、接続状態変更イベントのリスナーを有効にします。

my.readBLECharacteristicValue

特性値を読み取ります。

my.writeBLECharacteristicValue

特性値にデータを書き込みます。

Traditional Bluetooth

API

説明

my.closeBluetoothAdapter

ミニプログラムの Bluetooth モジュールを閉じます。

my.getBluetoothAdapterState

ミニプログラムの Bluetooth アダプタの状態を取得します。

my.getBluetoothDevices

検出されたすべての Bluetooth Low Energy デバイス (接続されているデバイスを含む) を取得します。

my.getConnectedBluetoothDevices

接続されている Bluetooth デバイスを取得します。

my.offBluetoothAdapterStateChange

接続状態変更イベントのリスナーを削除します。

my.offBluetoothDeviceFound

新しい Bluetooth デバイス検出イベントのリスナーを削除します。

my.onBluetoothDeviceFound

新しい Bluetooth デバイスが検出されたときにイベントをトリガーします。

my.onBluetoothAdapterStateChange

Bluetooth 状態変更イベントを監視します。

my.openBluetoothAdapter

ミニプログラムの Bluetooth モジュールを初期化します。

my.startBluetoothDevicesDiscovery

近くの Bluetooth デバイスの検出を開始します。

my.stopBluetoothDevicesDiscovery

近くの Bluetooth デバイスの検出を停止します。

サンプルコード

// 初期化
my.openBluetoothAdapter({
  success: (res) => {
      console.log(res);
  }
});
// 検出イベントの登録
my.onBluetoothDeviceFound({
  success: (res) => {
    let device = res.devices[0];
    // 検出されたデバイスに接続する
    my.connectBLEDevice({
      deviceId: deviceId,
      success: (res) => {
        console.log(res)
      },
      fail:(res) => {
      },
      complete: (res)=>{
      }
    });
    // 検出を停止する
    my.stopBluetoothDevicesDiscovery({
      success: (res) => {
        console.log(res)
      },
      fail:(res) => {
      },
      complete: (res)=>{
      }
    });
  }
});
// 接続イベントの登録
my.onBLEConnectionStateChanged({
  success: (res) => {
      console.log(res);
    if (res.connected) {
        // 読み取り、書き込み、通知などの操作を開始する
        my.notifyBLECharacteristicValueChange({
          deviceId: deviceId,
          serviceId: serviceId,
          characteristicId: characteristicId,
          success: (res) => {
              console.log(res)
          },
          fail:(res) => {
          },
          complete: (res)=>{
          }
        });
    }
  }
});
// 読み取りまたは通知データ受信イベントの登録
my.onBLECharacteristicValueChange({
  success: (res) => {
      console.log(res);
  }
});
// 検出を開始する
my.startBluetoothDevicesDiscovery({
  services: ['fff0'],
  success: (res) => {
      console.log(res)
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});

// 切断する
my.disconnectBLEDevice({
  deviceId: deviceId,
  success: (res) => {
      console.log(res)
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});

// イベントの登録解除
my.offBluetoothDeviceFound();
my.offBLEConnectionStateChanged();
my.offBLECharacteristicValueChange();

// Bluetooth モジュールを終了する
my.closeBluetoothAdapter({
  success: (res) => {
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});