質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

375閲覧

swift Bluetooth通信について

nyupos

総合スコア6

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/02/13 03:28

編集2018/02/14 10:30

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 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/02/13 03:54

「勉強がしたい」のに、いきなり丸投げですか?
nyupos

2018/02/13 04:15

すみません。どのような感じで動作するのか見たかったので...
fuzzball

2018/02/13 04:53

せめて、ビルド出来ないとか、すぐに落ちるとか、○○から先に進まないとか、通信は出来ているようだけどTextViewに表示されないとか、具体的に書いて下さい。「うまくいきません」じゃ何も分からないです。
nyupos

2018/02/13 06:14

全然具体的に書いていませんでした。すみません。具体的に書くと、textfieldに入力した文字をボタンを押したらもう一つの端末のTextViewに通信は出来ているのですが表示されません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問