Saturday 15 February 2014

swift - For communication between iOS device and hardware using core bluetooth, how to write data in chunks and read the same data -



swift - For communication between iOS device and hardware using core bluetooth, how to write data in chunks and read the same data -

i want write info hardware in chunks , read same info find solution. here code communication using core bluetooth correct? going right way? why on creating chunks in did write method getting response of lastly chunk , in read method lastly chunk getting read.

func centralmanagerdidupdatestate(central: cbcentralmanager!) { if central.state == cbcentralmanagerstate.poweredoff { println("bluetooth off") } else if central.state == cbcentralmanagerstate.poweredon { ibtblscanmenu!.reloaddata() central.scanforperipheralswithservices(nil, options: [cbcentralmanagerscanoptionallowduplicateskey : false]) println("bluetooth on") } else if central.state == cbcentralmanagerstate.unknown { println("bluetooth unknown") } else if central.state == cbcentralmanagerstate.unsupported { println("bluetooth unsupported") } } func centralmanager(central: cbcentralmanager!, diddiscoverperipheral peripheral: cbperipheral!, advertisementdata: [nsobject : anyobject]!, rssi: nsnumber!) { if peripheral != nil{ arrperipherals.addobject(peripheral) self.peripheral = peripheral self.ibtblscanmenu!.reloaddata() ibtblscanmenu!.hidden = false println(peripheral.name) } else { println("no device found") } peripheral.delegate = self } func centralmanager(central: cbcentralmanager!, didconnectperipheral peripheral: cbperipheral!) { println(flag) if flag == false{ peripheral.discoverservices(nil) var state = peripheral.state == cbperipheralstate.connected ? "yes" : "no" connected = "connected \(state)" println("connected \(connected)") flag = true } else { var alert:uialertview = uialertview(title: "message", message: "already connected", delegate: self, cancelbuttontitle: "ok") alert.show() println("already connected") }} func peripheral(peripheral: cbperipheral!, diddiscoverservices error: nserror!) { if error == nil { println(peripheral.services) service in peripheral.services [cbservice] { var alert:uialertview? println("service id \(service.uuid)") arrserviceid.addobject(service.uuid) println(arrserviceid) if service.uuid.isequal(cbuuid.uuidwithstring("f4f-bc76-d6341a")) { peripheral.discovercharacteristics(nil, forservice: service cbservice) } }} else { println("error!!!!!! ") } func peripheral(peripheral: cbperipheral!, diddiscovercharacteristicsforservice service: cbservice!, error: nserror!) { if (service.uuid.isequal(cbuuid.uuidwithstring("f4f-bc76-d6341a"))) { arrcharacteristics = service.characteristics var writeid:cbuuid = arrcharacteristics!.objectatindex(1).uuid var readid:cbuuid = arrcharacteristics!.objectatindex(0).uuid /* write data*/ if writeid.isequal(cbuuid.uuidwithstring("d0f0c1-9dd7-0b07047")) { var str:nsstring = "hii..!! hello..!!" info = str.datausingencoding(nsasciistringencoding)! if (datalen > 0) { (var count:int = 0 ; count < datalen ; count++ ) { peripheral.writevalue(self.data!.subdatawithrange(nsmakerange(count, 1)), forcharacteristic: self.arrcharacteristics!.objectatindex(1) cbcharacteristic , type: cbcharacteristicwritetype.withresponse) println("write performed") }}} func peripheral(peripheral: cbperipheral!, didwritevalueforcharacteristic characteristic: cbcharacteristic!, error: nserror!) { if characteristic.uuid.isequal(cbuuid.uuidwithstring("d0f0c1-9dd7-0b07047")) { var datastring = nsstring(data: data!, encoding: nsasciistringencoding) println(datastring) message = uialertview(title: "response", message: datastring , delegate: self, cancelbuttontitle: "ok") peripheral.setnotifyvalue(true, forcharacteristic: arrcharacteristics!.objectatindex(0) cbcharacteristic) } } func peripheral(peripheral: cbperipheral!, didupdatenotificationstateforcharacteristic characteristic: cbcharacteristic!, error: nserror!) { if characteristic.uuid.isequal(cbuuid.uuidwithstring("c8853-c506-4835-a2ab1")) { if (characteristic.isnotifying) { println("notification began\(characteristic)") println(characteristic.value) peripheral.readvalueforcharacteristic(arrcharacteristics!.objectatindex(0) cbcharacteristic) } else { println("notification stopped") } } } func peripheral(peripheral: cbperipheral!, didupdatevalueforcharacteristic characteristic: cbcharacteristic!, error: nserror!) { if characteristic.uuid.isequal(cbuuid.uuidwithstring("c8853-c506-4835-a2ab1")) { println(characteristic.value) println("read performed") var str:nsstring = nsstring(data: characteristic.value, encoding: nsasciistringencoding) println("bbbbb--- \(str)") var alert:uialertview = uialertview(title: "message", message: str, delegate: self, cancelbuttontitle: "ok") alert.show() }

ios swift core-bluetooth

No comments:

Post a Comment