CBMutableCharacteristicで初期化後にvalueの値を変更したいと考えています。
初期化を行っている部分は以下のようにCBMutableCharacteristicの初期値に0x00という値を設定して初期化を行っています。
Swift
1 2 func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { 3 4 if peripheral.state == .poweredOn { 5 service = CBMutableService(type: CBUUID(string: "1AB20166-1B2B-4B71-9538-CF3ADCD7ABAE"), primary: true) 6 7 characteristic = CBMutableCharacteristic(type: CharacteristicUUID.batteryInformation,properties: .read, value: Data(bytes: [0x00]), permissions: .readable) 8 9 self.service.characteristics = [characteristic] 10 self.manager.add(self.service) 11 12 } 13 }
画面にUISegmentedControlを設置して以下のように値が変更された際にcharacteristicの値を書き換えているのですが、Central側のアプリからreadを行っても初期値で設定した値を取得してしまい書き換わっていないようです。
Swift
1 2 @IBAction func changeBattery(_ sender: UISegmentedControl) { 3 switch sender.selectedSegmentIndex { 4 case 0: 5 characteristic.value = Data(bytes: [0x00]) 6 case 1: 7 characteristic.value = Data(bytes: [0x01]) 8 case 2: 9 characteristic.value = Data(bytes: [0x02]) 10 default: 11 break 12 } 13 } 14
値を書き換える際に必要なことが別途あるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。