ios - connecting Central to peipheral for change the colour of lights or on/off light -
i want use ble
device ios has on/off lights & coloured lights. connecting bluetooth using core bluetooth framework.
now want on/off or change colour of lights. how central app peripheral bluetooth device, methods required function ?
1) scan nil service id (if using in background need service id)
2) scan
[self.centralmanager scanforperipheralswithservices:nil options:nil];
3) devices in delegate method
- (void) centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { // check advertisementdata service uuid [self.centralmanager connectperipheral:peripheral options:nil]; }
4) on successful connection
- (void) centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral { [peripheral discoverservices:nil]; }
5) on services discoverd
- (void)peripheral:(cbperipheral *)peripheral diddiscoverservices:(nserror *)error { if (error) { nslog(@"error discovering services: %@", [error localizeddescription]); return; } // discover characteristic want... // loop through newly filled peripheral.services array, in case there's more one. (cbservice *service in peripheral.services) { [peripheral discovercharacteristics:nil forservice:service]; } }
6) on retrieving charactesicts
- (void)peripheral:(cbperipheral *)peripheral diddiscovercharacteristicsforservice:(cbservice *)service error:(nserror *)error { // deal errors (if any) if (error) { nslog(@"error discovering characteristics: %@", [error localizeddescription]); return; } // again, loop through array, in case. (cbcharacteristic *characteristic in service.characteristics) { // , check if it's right 1 if ([characteristic.uuid isequal:[cbuuid uuidwithstring:@"fff1"]]) { scannedcharacteristic = characteristic; nsstring *str = writevalue.length == 0 ? @"z" : writevalue; ddlogverbose(@"writing value %@", str); nsdata *expecteddata = [str datausingencoding:nsutf8stringencoding]; [peripheral writevalue:expecteddata forcharacteristic:characteristic type:cbcharacteristicwritewithresponse]; } // [peripheral setnotifyvalue:yes forcharacteristic:characteristic]; } }
Comments
Post a Comment