Bluetooth通信の勉強がしたい為、Qiitaであったサンプルアプリを試してみたのですが、textfieldに入力した文字をボタンを押したらもう一つの端末のTextViewに表示させたいのですがうまくいきません。
どうすれば表示できるようになるのでしょうか?
初心者なもので、何卒力添えを...
参考にしたQiitaのリンク
https://qiita.com/nave421m/items/639a7a043844acda4eff
swift
1import UIKit 2 import CoreBluetooth 3 4 class BluetoothViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralManagerDelegate, UITextFieldDelegate, CBPeripheralDelegate { 5 6 var centralManager: CBCentralManager! 7 var peripheral: CBPeripheral! 8 var peripheralManager: CBPeripheralManager! 9 var textFieldString = "" 10 11 @IBOutlet weak var messageField: UITextField! 12 @IBOutlet weak var messageView: UITextView! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view. 17 18 centralManager = CBCentralManager(delegate: self, queue: nil) 19 peripheralManager = CBPeripheralManager(delegate: self, queue: nil) 20 } 21 22 override func didReceiveMemoryWarning() { 23 super.didReceiveMemoryWarning() 24 // Dispose of any resources that can be recreated. 25 } 26 27 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 28 view.endEditing(true) 29 } 30 31 // 接続状況が変わるたびに呼ばれる 32 func centralManagerDidUpdateState(_ central: CBCentralManager) { 33 print ("state: (central.state)") 34 } 35 36 // スキャン開始 37 @IBAction func startScan(_ sender: Any) { 38 // 2.3 39 // centralManager.scanForPeripheralsWithServices(nil, options: nil) 40 centralManager.scanForPeripherals(withServices: nil, options: nil) 41 print("スキャン開始") 42 } 43 44 // スキャン結果を取得 45 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 46 self.peripheral = peripheral 47 self.centralManager.connect(self.peripheral, options: nil) 48 } 49 50 // スキャン終了 51 @IBAction func stopScan(_ sender: Any) { 52 centralManager.stopScan() 53 print("スキャン終了") 54 } 55 56 // 接続成功時に呼ばれる 57 func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 58 print("Connect success!") 59 } 60 61 // 接続失敗時に呼ばれる 62 func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { 63 print("Connect failed...") 64 } 65 66 @IBAction func sendMessage(_ sender: Any) { 67 68 textFieldString = messageField.text! 69 messageView.text = textFieldString 70 71 } 72 73 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 74 messageField.resignFirstResponder() 75 return true 76 } 77 78 // ペリフェラルのStatusが変化した時に呼ばれる 79 func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { 80 print("periState(peripheral.state)") 81 } 82 83 @IBAction func startAdvertite(_ sender: Any) { 84 let advertisementData = [CBAdvertisementDataLocalNameKey: "Test Device"] 85 let serviceUUID = CBUUID(string: "0000") 86 let service = CBMutableService(type: serviceUUID, primary: true) 87 let charactericUUID = CBUUID(string: "0001") 88 let characteristic = CBMutableCharacteristic(type: charactericUUID, properties: CBCharacteristicProperties.read, value: nil, permissions: CBAttributePermissions.readable) 89 service.characteristics = [characteristic] 90 self.peripheralManager.add(service) 91 peripheralManager.startAdvertising(advertisementData) 92 } 93 94 // サービス追加結果の取得 95 func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) { 96 if error != nil { 97 print("Service Add Failed...") 98 return 99 } 100 print("Service Add Sucsess!") 101 } 102 103 // アドバタイズ開始処理の結果を取得 104 func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) { 105 if let error = error { 106 print("***Advertising ERROR") 107 return 108 } 109 print("Advertising success") 110 } 111 112 // アドバタイズ終了 113 @IBAction func topArvertisement(_ sender: Any) { 114 peripheralManager.stopAdvertising() 115 print("アドバタイズ終了") 116 } 117 118 // service検索開始 119 @IBAction func getService(_ sender: Any) { 120 peripheral.delegate = self 121 peripheral.discoverServices(nil) 122 } 123 124 // service検索結果取得 125 func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 126 guard let services = peripheral.services else{ 127 print("error") 128 return 129 } 130 print("(services.count)個のサービスを発見。(services)") 131 132 // サービスを見つけたらすぐにキャラクタリスティックを取得 133 for obj in services { 134 peripheral.discoverCharacteristics(nil, for: obj) 135 } 136 } 137 138 // キャラクタリスティック検索結果取得 139 func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 140 if let characteristics = service.characteristics { 141 print("(characteristics.count)個のキャラクタリスティックを発見。(characteristics)") 142 } 143 } 144 145 /* 146 // MARK: - Navigation 147 148 // In a storyboard-based application, you will often want to do a little preparation before navigation 149 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 150 // Get the new view controller using segue.destinationViewController. 151 // Pass the selected object to the new view controller. 152 } 153 */ 154 155 }
あなたの回答
tips
プレビュー