

## スキャン

BLEのペリフェラルはadvertisement(直訳で広告)という信号を送り続けます。  
これは「私はXXXっていうデバイスです。つなぐことができます」という信号で、これを使ってセントラルは近くにいるBLEペリフェラルを探すことができます。

advertisementを取得し周りにあるデバイスを全部探してみましょう。[ble.scan](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html)にある[ble.scan.startWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#start)によりスキャンを開始できます。発見されたデバイスは[ble.scan.onfind](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#onfind)に設定した関数で[bleRemotePeripheral](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.bleremoteperipheral.html)オブジェクトとして受け取ることができます。

```javascript
// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();

  obniz.ble.scan.onfind = async function(peripheral){
    console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait();
}

```

スキャンは一定時間（デフォルト 30 秒）で完了し、完了時には[ble.scan.onfinish](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#onfinish)に設定した関数が呼ばれます。
一定時間経過後、繰り返しスキャンさせたい場合は onfinish 内で [ble.scan.startWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#startwait) を呼ぶことで繰り返しスキャンが可能です。
スキャン時間に `duration: null` を指定すると一定時間で停止せずに常時スキャンする状態になります。

ただし、**スキャン時間の指定に関わらず**期待した時間より早くスキャンが停止されることがあります。
その場合は onfinish 内で [ble.scan.startWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#startwait) を再度呼んでスキャンを再開させる必要があります。

```javascript
// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();

  obniz.ble.scan.onfind = async function(peripheral){
    console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    console.log("scan again")
    await obniz.ble.scan.startWait();
  };

  await obniz.ble.scan.startWait();
}

```

## 条件付き検索

検索時にパラメーターを指定することで特定のデバイスだけに絞り込むことができます。
複数設定した場合はOR検索となり、どれかに一致したもののみ検索結果として検出されます。
binaryを用いることで特殊なビーコンなどの絞り込みも可能です。

[ScanTarget](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescantarget.html)

- [uuids](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescantarget.html#uuids): UUIDを指定して絞り込み
- [localName](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescantarget.html#localname): デバイスの名前を指定して絞り込み(完全一致)
- [localNamePrefix](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescantarget.html#localnameprefix): デバイス名を指定して絞り込み(前方一致)(obniz.js 3.4.0以降)
- [deviceAddress](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescantarget.html#deviceaddress): デバイスアドレスを指定して絞り込み(obniz.js 3.4.0以降)
- [binary](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescantarget.html#binary): advertisementに含まれるバイナリ列の部分一致で絞り込み(obniz.js 3.4.0以降)

また、スキャンの設定を変更することができます。時間の長さやActive/Passiveの切り替えなどです。特に通信費を抑える場合はデバイス側でのfilterを行うfilterOnDeviceプロパティ(obnizOS 3.2.0以上)もあります。

[ScanSetting](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescansetting.html)

- [duration](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescansetting.html#duration): スキャンの時間を変更(標準30秒)
- [duplicate](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescansetting.html#duplicate): 同一のデバイスでも新しいadvertisementを受け取ったら通知。以下は名前を指定する例です。
- [activeScan](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescansetting.html#activescan): アクティブスキャンかどうかを指定します(標準アクティブ)
- [filterOnDevice](https://obniz.github.io/obniz/obnizjs/interfaces/obnizcore.components.ble.hci.blescansetting.html#filterondevice): フィルター処理をデバイス上で行うかどうかを指定します。(obnizOS 3.2.0以上・obniz.js 3.4.0以上)

filterOnDeviceについて  
デバイス側で指定された条件に合わせたfilterが実施されるため通信費の削減を行うことができます。  
また、filterOnDevice実行時には受け取るadvertisementのscanResponseが足りないまたはその逆のadvertisementを受け取る可能性があります。

```javascript
// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    localName: "Blank"
  };

  obniz.ble.scan.onfind = async function(peripheral){
    console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target);
}

```

検索時間を長くし、かつ同じデバイスでもadvertisementを再度受け取ったら通知するようにします。これによりadvertisement内に含まれるデータが変わった場合や、RSSIの変化を知ることができます。

```javascript
// Javascript Example

var target = {
  localName: "Blank"
};
var setting = {
  duration: 60, // 60 sec
  duplicate: true // allow duplicate
}
await obniz.ble.scan.startWait(target, setting);

```

検索時の条件としてlocalNameの他にadvertisementに入っているUUIDを指定することもできます。

```javascript
// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    uuids: ["1111"]
  };

  obniz.ble.scan.onfind = async function(peripheral){
   console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = function(){
   console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target);
}

```


より複雑な検索方法については、[より高度な条件付きスキャン](./central-scan-filter)を参照ください

## １つのみ検索

検索はcallback形式でなく待機することも可能です。特に[startOneWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#startonewait)を利用すれば、ある１つのデバイスだけ探すことも可能です。また、条件を指定せずすべてのデバイスのスキャンが終わるまで待機する[startAllWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#startallwait)も利用可能です。

```javascript
// Javascript Example

await obniz.ble.initWait();
var target = {
  localName: "Blank"
};
var peripheral = await obniz.ble.scan.startOneWait(target);

```

## スキャンの中断

開始中のスキャンを中断するには[ble.scan.endWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.ble.hci.blescan.html#endwait)を利用します。

```javascript
// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    uuids: ["1111"]
  };

  obniz.ble.scan.onfind = async function(peripheral){
    console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target);
  await obniz.wait(1000);
  await obniz.ble.scan.endWait();
}

```
