前提・実現したいこと
BLEデバイスにiPhoneからデータを送信するアプリを作っています。ペアリングはできていますが、データ送信がうまくいっていません。データの記述方法と、Charadteristic UUIDの記述方法をご教授いただきたいです。
発生している問題・エラーメッセージ
エラー等は発生していませんが、データ送信ができていません。 下記のソースコードの、 writeValue(data, for: writeChar, type: .withoutResponse) の引数が正しくないと思われます。 writeChar にUUIDを持たせるのは self.writeChar = CBMutableCharacteristic(type: characteristicUUID, properties: properties, value: nil, permissions: permissions) では不可能なのでしょうか?
該当のソースコード
Swift
1import UIKit 2import CoreBluetooth; 3 4class ViewController: UIViewController,CBCentralManagerDelegate, CBPeripheralDelegate { 5 6 var serviceUUID : CBUUID! 7 var characteristicUUID : CBUUID! 8 9 var manager : CBCentralManager! 10 var peripheral : CBPeripheral! 11 var characteristic : CBCharacteristic! 12 var writeChar : CBMutableCharacteristic! 13 14 let properties = (CBCharacteristicProperties.write) 15 let permissions = (CBAttributePermissions.writeable) 16 17 18 @IBOutlet var ConnectButton: UIButton! 19 @IBOutlet var DisconnectButton: UIButton! 20 @IBOutlet var OnButton: UIButton! 21 @IBOutlet var OffButton: UIButton! 22 23 override func viewDidLoad() { 24 super.viewDidLoad() 25 self.serviceUUID = CBUUID(string: Constants.SERVICE_UUID) 26 self.characteristicUUID = CBUUID(string: Constants.CHARACTERISTIC_UUID) 27 self.manager = CBCentralManager(delegate : self, queue : nil) 28 self.ConnectButton.isEnabled = true 29 self.DisconnectButton.isEnabled = true 30 self.writeChar = CBMutableCharacteristic(type: characteristicUUID, properties: properties, value: nil, permissions: permissions) 31 32 33 } 34 35 override func didReceiveMemoryWarning() { 36 super.didReceiveMemoryWarning() 37 // Dispose of any resources that can be recreated. 38 } 39 @IBAction func ConnectClicked(_ sender: Any) { 40 self.manager.scanForPeripherals(withServices: [self.serviceUUID], options: nil) 41 } 42 @IBAction func DisconnectClicked(_ sender: Any) { 43 self.manager.cancelPeripheralConnection(peripheral) 44 } 45 46 @IBAction func OnClicked(_ sender: Any) { 47 let ondata: [UInt8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]; 48 let data = Data(ondata) 49 peripheral.writeValue(data, for: writeChar, type: .withoutResponse) 50 51 } 52 53 @IBAction func OffClicked(_ sender: Any) { 54 let offdata: [UInt8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] 55 let data = Data(offdata) 56 peripheral.writeValue(data, for: writeChar, type: .withoutResponse) 57 58 } 59 60 61 func centralManagerDidUpdateState(_ central: CBCentralManager) { 62 } 63 64 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 65 66 self.peripheral = peripheral 67 self.peripheral.delegate = self 68 self.manager.connect(peripheral, options: nil) 69 self.manager.stopScan() 70 } 71 72 func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 73 self.peripheral.discoverServices([self.serviceUUID]) 74 } 75 76 func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 77 if (error != nil) { 78 return 79 } 80 let services : [CBService]? = peripheral.services 81 for service in services! { 82 if service.uuid.isEqual(self.serviceUUID) { 83 self.peripheral.discoverCharacteristics(nil, for:service) 84 } 85 } 86 } 87 88 func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 89 if error != nil { 90 return 91 } 92 let characteristics : [CBCharacteristic]? = service.characteristics 93 for c in characteristics! { 94 if c.uuid.isEqual(self.characteristicUUID) { 95 self.peripheral.readValue(for: c) 96 } 97 } 98 } 99 100} 101

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/29 03:03