2016-11-24 11 views
4

マイクロチップ社では、MLDPプロファイルを使用してRN4020 BT LEチップをリリースしてから数年経ちました。しかし、Apple App StoreにiOSアプリケーションを持っているにもかかわらず、まだ利用可能なiOSサンプルソースコードはまだありません。誰も作業コードを持っていて、それを共有/投稿したいと思っていますか?マイクロチップRN4020→MLDPモードiOSサンプルコード

ありがとうございます!

ティム

答えて

4

私はいくつかの作業コードを持っています。私はここでいくつかのスニペットを与えるでしょう。我々が持っているCBCentralManagerDelegateに準拠して最初のViewControllerには:ボタンをタッチし

var cbc : CBCentralManager? = nil 

override func viewDidLoad() { 
    super.viewDidLoad() 
    cbc = CBCentralManager(delegate: self, queue: nil) 
} 

は、各ペリフェラルの

@IBAction func scan(_ sender: Any) { 
    cbc?.scanForPeripherals(withServices: nil, options: nil) 
} 

周辺機器のためのスキャンは、次のデリゲートメンバーが

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
    // store peripherals here to let select user one 
    NSLog("name=%@", peripheral.name ?? "unnamed") 
} 
と呼ばれる発見を開始します

周辺機器を辞書に保存し、テーブルビューを使用してその名前をユーザに提示します。

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
    performSegue(withIdentifier: "ConnectPeriph", sender: self) 
} 

第二のViewControllerに接続された状態に責任つながる:ユーザーが周辺を選択した場合、私たちは、次のデリゲートメソッドの呼び出しにつながること

@IBAction func connect(_ sender: Any) { 
    // selectedPeripheral set by selection from the table view 
    cbc?.connect(selectedPeripheral!, options: nil) 
} 

成功し、接続に接続しよう。これのViewControllerはCBPeripheralDelegateプロトコルに準拠し、次の変数を宣言します。接続後の

var periph : CBPeripheral! // selected peripheral 
var dataChar : CBCharacteristic? // characteristic for data transfer 

let mchpPrivateService : CBUUID = CBUUID(string: "00035B03-58E6-07DD-021A-08123A000300") 
let mchpDataPrivateChar : CBUUID = CBUUID(string: "00035B03-58E6-07DD-021A-08123A000301") 

最初のアクションは、周辺機器が提供されるサービスを発見することである。

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    periph.delegate = self 
    periph.discoverServices(nil) 
} 

これは、このデリゲートメソッドの呼び出しになり:

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    if let e = error { 
     NSLog("Error %@", e.localizedDescription) 
    } 
    else if let services = peripheral.services { 
     for s in services { 
      NSLog("Service=%@", s.uuid.uuidString) 
      if s.uuid.isEqual(mchpPrivateService) { 
       peripheral.discoverCharacteristics(nil, for: s) 
      } 
     } 
    } 
} 

発見特性のターン結果に:

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
    NSLog("characteristics for service %@", service.uuid.uuidString) 
    if let characteristics = service.characteristics { 
     for c in characteristics { 
      if c.uuid.isEqual(mchpDataPrivateChar) { 
       peripheral.setNotifyValue(true, for: c) 
       dataChar = c 
      } 
     } 
    } 
} 

私たちが興味を持っている唯一の特徴は、uuid mchpDataPrivateCharです。通知のリクエストにより、iOS側の受信者が完了し、

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 
    NSLog("update value for %@", characteristic.uuid) 
    if let d = characteristic.value { 
     var s : String = String() 
     for b in d { 
      s.append(Character(UnicodeScalar(b))) 
     } 
     NSLog("received \(d.count) bytes: \(s)") 
    } 
} 

に電話がかかります。送信バイト数は、

@IBAction func sendClicked(_ sender: Any) { 
    if let d = dataChar, let s=sendEdit.text { 
     let buffer : [UInt8] = Array(s.utf8) 
     let data : Data = Data(buffer) 
     periph.writeValue(data, for: d, type: .withResponse) 
    } 
} 
関連する問題