CoreBluetooth For Central (3)

Discover BLE Device

BLE跟一般Bluetooth一样,它需要经过扫描周边方式取得可接受BLE连线的周边,再经过指定的方式与周边连线,但CB提供功能,这些动作需要经过程式的控制来启动扫描与停止扫描的动作,这章我们要完成扫描周边的动作,一方面也可以确认BLE运作是否正常。

建立Xcode专案与配置UI

  • 首先建立名为BluetoothLE-Scan的专案,再将视图转至Storyboard上面,我们要进行UI的配置工作。

  • Button元件配置2个按键分别是ScanStop

  • Label元件配置1个,内容可以写上你可以识别的或是Discover BLE Device

  • 最后整个配置如下图所示,可以试着操作看看

开始写功能

CB在使用时,有些过程及程式码内容是必要的,每章节下来部分内容会沿用之前程式码的内容,在这只针对新增的部分进行说明,详细内容请自行下载程式码对照使用。

Scan

UI与程式码的配置
  • 我们使用拖、拉的方式建立Button的Action,如果您对于Xcode有基本操作的话,可以自行先行建立。

  • Connection 选择Action、Name 填上 buttonScan ,完成后按下Connect,UI Button与程式的连结,

可以在标头档案(.h)与程式码(.m)看到已自动建立程式码,

之后只要在UI上按下Scan按键就会对应到

//-----------start-----------
    - (IBAction)buttonScan:(id)sender {
    :
    }
//------------end------------

Scan按键的行为功能
  • 无论如何,在使用CB功能时请务必要引入
//-----------start-----------
    #import <CoreBluetooth/CoreBluetooth.h>
//------------end------------

  • 加入Scan程式码:
//-----------start-----------
    [CM scanForPeripheralsWithServices:nil options:nil];
//------------end------------

所以内容:

//-----------start-----------
    - (IBAction)buttonScan:(id)sender {
        [CM scanForPeripheralsWithServices:nil options:nil];
        NSLog(@"Scan");
    }
//------------end------------

  • 加入Delegate,当抓到BLE周边时,CB会将资讯传至
    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

所以我们要增加程式码将资料印出来

    NSLog(@"%@\n",peripheral);
    NSLog(@"%@\n",advertisementData);
    NSLog(@"%@\n",RSSI);

Stop

UI与程式码的配置

整个配置的方法与Scan的方式相同,在Name上填入buttonStop

Stop按键的行为功能

CB中要停止扫描的Method为stopScan,这是属于CBCentralManager物件所提供的。

  • 加入Stop程式码:
    [CM stopScan];

所以内容:

    - (IBAction)buttonStop:(id)sender {

        [CM stopScan];
        NSLog(@"stopScan");
    }

测试功能

整个程式完成后我们可以来看一下最后的结果,这里以iPad做为Device来展示程式,执行之前请记得要开启蓝牙功能,

程式的执行画面:

Scan

按下按键后会看一下Log上的显示

    2014-01-21 16:25:56.685 BluetoothLE-Scan[6123:60b] Scan
    2014-01-21 16:25:56.924 BluetoothLE-Scan[6123:60b] <CBPeripheral: 0x14d3f810 identifier = 9D027D39-5A77-3B6B-   BC45-5A1E00115269, Name = "TI BLE Keyfob", state = disconnected>
    2014-01-21 16:25:56.926 BluetoothLE-Scan[6123:60b] {
        kCBAdvDataChannel = 37;
        kCBAdvDataIsConnectable = 0;
    }
    2014-01-21 16:25:56.929 BluetoothLE-Scan[6123:60b] -60
    2014-01-21 16:25:56.960 BluetoothLE-Scan[6123:60b] <CBPeripheral: 0x14d44600 identifier = B10F4C1D-A316-18AA-9E1D-  FE64D26B1FA0, Name = "(null)", state = disconnected>
    2014-01-21 16:25:56.962 BluetoothLE-Scan[6123:60b] {
        kCBAdvDataChannel = 38;
        kCBAdvDataIsConnectable = 1;
    }
    2014-01-21 16:25:56.963 BluetoothLE-Scan[6123:60b] -94

Log上显示:

  • identifier:周边的UUID,其ID为设备的唯一值,每个设备都会有不同的UUID,连线时也是利用此UUID进行连线

  • Name:周边的名称,这里看到的是”TI BLE Keyfob”名称。

  • state:连线状态,如果有连线就会得到 connected 范例中得到的讯息是 disconnected (断线),代表目前周边未进行任何连线。

  • kCBAdvDataChannel:周边的广播通道号码,Bluetooth是使用跳频的方式连线,所以每一次得到的号码也不同。

  • kCBAdvDataIsConnectable:周边是否能被连线。

Log之中的 -60、-94这值为RSSI值,这值代表iDevice得到BLE周边讯号的强度,

另一个例子:

    2014-01-21 16:51:40.195 BluetoothLE-Scan[6145:60b] Scan
    2014-01-21 16:51:40.220 BluetoothLE-Scan[6145:60b] <CBPeripheral: 0x16569be0 identifier = 419D6B15-1F6C-    EE7B-7751-2748ACA0D7C3, Name = "DannySerialApp", state = disconnected>
    2014-01-21 16:51:40.224 BluetoothLE-Scan[6145:60b] {
        kCBAdvDataChannel = 37;
        kCBAdvDataIsConnectable = 1;
        kCBAdvDataLocalName = DannySimpleBLE;
        kCBAdvDataServiceUUIDs =     (
            "Unknown (<fff0>)"
        );
        kCBAdvDataTxPowerLevel = 0;
    }
    2014-01-21 16:51:40.225 BluetoothLE-Scan[6145:60b] -43
    2014-01-21 16:51:40.331 BluetoothLE-Scan[6145:60b] <CBPeripheral: 0x16679130 identifier = D9BFA6BC-DAD3-B29B-1031-  AA1CCC09B910, Name = "(null)", state = disconnected>
    2014-01-21 16:51:40.333 BluetoothLE-Scan[6145:60b] {
        kCBAdvDataChannel = 37;
        kCBAdvDataIsConnectable = 1;
    }
    2014-01-21 16:51:40.335 BluetoothLE-Scan[6145:60b] -93

如果有提供其他的连线服务的话,会看到kCBAdvDataServiceUUIDs会有内容。

Stop

按键立即停止扫描功能,会在Log上看到stopScan。

范例程式