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

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

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

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

解決済

1回答

1148閲覧

didReceiveWrite時のデータの取り出し方

saku_panda

総合スコア20

Bluetooth

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2019/11/27 02:45

編集2019/11/27 02:59

##前提・実現したいこと
iOS同士のBLE通信で、Centralが固有IDをPeripheralへwriteし、
Peripheralがそれを保持するプログラムが組みたいです。

##発生している問題
PeripheralがIDを書き込まれ、didReceiveWrite起動しますが、書き込まれたデータを取り出せません。
後述のプログラムにおいて、requestsがポインタで渡されているようで、その扱いがわかりません。
requests内では2byteのデータが_value内にあるのがXcodeデバック時に確認できています。

##該当のソースコード

Swift

1/* Central */ 2private var my_peripheral: CBPeripheral? = nil 3private var ID_characteristic: CBCharacteristic? = nil 4private let ID_UUID: CBUUID = CBUUID(string: "ID_UUID") 5private var peripheral_ID: UInt = 1111 // writeするID 6 7...... 8 9func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 10 print("didDiscoverCharacteristicsFor") 11 12 guard ( (peripheral == my_peripheral! ) 13 && (service.characteristics != nil) ) else{ 14 return 15 } 16 17 for characteristic in service.characteristics!{ 18 /* characteristicの識別 */ 19 correspond_characteristic(characteristic: characteristic) 20 } 21 } 22 23 24func correspond_characteristic(characteristic: CBCharacteristic){ 25 if (characteristic.uuid == ID_UUID) { 26 /* peripheralへIDを書き込みにいく */ 27 if (ID_characteristic == nil){ 28 ID_characteristic = characteristic 29 write_ID() 30 } else { 31 ...... 32 } 33 } else { 34 ...... 35 } 36} 37 38 39func write_ID(){ 40 guard my_peripheral != nil else { 41 print("no peripheral") 42 return 43 } 44 guard ID_characteristic != nil else{ 45 Log.d("no characteristic") 46 return 47 } 48 var data: [UInt8] = [] 49 var write_ID: UInt16 = UInt16(peripheral_ID) 50 51 data.append(contentsOf: withUnsafeBytes(of: &write_ID){ $0.map{ UInt8($0) } }) 52 53 let write_data = Data(data) 54 55 print("write_data = (String(describing: write_data))") 56 // write_data = 2byte 57 58 my_peripheral!.writeValue(write_data, for: ID_characteristic!, type: CBCharacteristicWriteType.withResponse) 59}

Swift

1/* Peripheral */ 2private var ID_characteristic: CBMutableCharacteristic? = nil 3private let ID_UUID: CBUUID = CBUUID(string: "ID_UUID") 4private let ID_property: CBCharacteristicProperties = [.notify,.write,.read] 5private let ID_permission: CBAttributePermissions = [.readable,.writeable] 6 7...... 8 9func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]){ 10 guard ID_characteristic != nil else{ 11 print("no chracteristic") 12 return 13 } 14 for request in requests { 15 if ( request.characteristic.uuid == ID_UUID ){ 16 /* 正常にIDをwriteできなかった時 */ 17 if ( (request.characteristic.value == nil) || (request.characteristic.value!.isEmpty) ) { 18 print("ID not recv") 19 print("request ID = (request.characteristic.value)") 20 // request ID = nil 21 peripheralManager.respond(to: request, withResult: CBATTError.invalidHandle) 22 return 23 } 24 25 let value = NSData(data: request.characteristic.value!) 26 let elements_num = request.value!.count 27 var recv_ID: UInt16 = 0 28 29 value.getBytes(&recv_ID ,range: NSRange(location: 0, length: elements_num)) 30 print("new ID = (recv_ID)") 31 } else { 32 ...... 33 } 34 } 35}

##使っているツールのバージョンなど補足情報
Swift 5.0
Xcode 11.0
iOS 12.3
MacOS Mojave 10.14.6

都合上、アップデートができません。
ご助力いただけますと幸いです。

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

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

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

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

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

guest

回答1

0

自己解決

###解決内容
自己解決しました。
CBATTRequestの理解が間違っていました。
request.characteristic.valueに書き込みデータがあると考えていましたが、
データはrequest.valueに格納されており、
該当部分を訂正したところうまく動作しました。

request.characteristicは、あくまでも書き込み先を指定するためだけのオブジェクトだと理解を改めました。
###参考
https://blog.reinforce-lab.com/2013/09/19/2013-09-19-blebook-ch3-corebluetooth/
https://developer.apple.com/documentation/corebluetooth/cbattrequest

投稿2019/11/27 06:52

saku_panda

総合スコア20

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問