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

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

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

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

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

0回答

1017閲覧

<coreBluetooth>peripheralとしてアドバタイズするために実装したが、エラーが出てしまい進みません。

youki0199

総合スコア0

Bluetooth

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

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2021/08/09 14:12

編集2021/08/09 14:15

<coreBluetooth>peripheralとしてアドバタイズする

swift、coreBluetoothともに初心者です。
coreBluetoothの理解のために、まずperipheralでサービスUUIDを指定してそれをcentralで検索するというシステムを作っています。
色々調べながら作成してcentralの方は完成したのですが、peripheralの方でエラーが出てしまい、詰まってしまっています。
central、peripheralはそれぞれ別のプロジェクトで作成して、2端末で実験しています。

発生している問題・エラーメッセージ

*** Terminating app due to uncaught exception 'Invalid Object', reason: 'Object <CBMutableService: xxxx Primary = YES, UUID = xxxx, Included Services = (null), Characteristics = ( "<CBMutableCharacteristic: xxxx UUID = xxxx, Value = (null), Properties = xxxx, Permissions = xxxx, Descriptors = (null), SubscribedCentrals = (\n)>" )> is invalid' *** First throw call stack: (xxxx) libc++abi: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'Invalid Object', reason: 'Object <CBMutableService: xxxx Primary = YES, UUID = xxxx, Included Services = (null), Characteristics = ( "<CBMutableCharacteristic: xxxx UUID = xxxx, Value = (null), Properties = xxxx, Permissions = xxxx, Descriptors = (null), SubscribedCentrals = (\n)>" )> is invalid' terminating with uncaught exception of type NSException

該当のソースコード

class ViewController: UIViewController, CBPeripheralManagerDelegate{ var manager: CBPeripheralManager! var service: CBMutableService! var characteristic: CBMutableCharacteristic! override func viewDidLoad() { super.viewDidLoad() self.manager = CBPeripheralManager(delegate : self, queue : nil, options: nil) } func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { switch peripheral.state { case .poweredOn: service = CBMutableService(type: CBUUID(string: "xxxx"), primary: true) let properties: CBCharacteristicProperties = [.notify, .read, .write] let permissions: CBAttributePermissions = [.readable, .writeable] characteristic = CBMutableCharacteristic(type: CBUUID(string: "xxxx"),properties: properties, value: nil, permissions: permissions) self.service.characteristics = [self.characteristic] self.manager.add(self.service) case .unknown: print("bluetooth error") case .resetting: print("bluetooth error") case .unsupported: print("bluetooth error") case .unauthorized: print("bluetooth error") case .poweredOff: print("bluetooth error") @unknown default: print("bluetooth error") } } func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) { if error != nil { print("Service Add Failed...") return } print("Service Add Success!") if (self.manager.isAdvertising == false) { //アドバタイズ開始 print("start") manager.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [service], CBAdvertisementDataLocalNameKey: "TestPeripheralDevice"]) } } func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) { if let error = error { print("Failed... error: (error)") return } print("Succeeded!") }

開発環境

・Xcode Version 12.5.1
・swift
・実装端末:iPhone12、iPhone6s

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問