Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if central.state == CBManagerState.poweredOn {
- debugPrint("poweredOn")
- let serviceUUIDs:[AnyObject] = [serviceCBUUID_READ]
- let lastPeripherals = centralManager.retrieveConnectedPeripherals(withServices: serviceUUIDs as! [CBUUID])
- if lastPeripherals.count > 0{
- let device = lastPeripherals.last! as CBPeripheral;
- deviceX = device;
- centralManager.connect(deviceX, options: nil)
- if(device.state == .disconnected){
- self.alertShowing(msg: "Device is disconnected restart the device and connect again.")
- }
- }
- else {
- centralManager.scanForPeripherals(withServices: nil, options: nil)
- }
- debugPrint(lastPeripherals)
- } else if(central.state == CBManagerState.poweredOff) {
- self.alertShowing(msg: "Make sure that your bluetooth is turned on.")
- }
- else if(central.state == CBManagerState.unsupported) {
- self.alertShowing(msg: "This device is unsupported.")
- }else{
- self.alertShowing(msg: "Try again after restarting the device.")
- }
- }
- func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
- if(peripheral.name == "Peripheral Observer") || (peripheral.name == "BLE Device"){
- if let services = peripheral.services{
- for service in services{
- debugPrint(service)
- }
- }
- debugPrint("advertisementData :(advertisementData)")
- deviceX = peripheral
- peripheral.delegate = self
- centralManager.stopScan()
- centralManager.connect(peripheral)
- }
- }
- func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
- debugPrint("Connected")
- var i = 0
- timerForService = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
- if(self.service_Read == nil) && (self.service_Write == nil){
- i += 1
- if(i%2 == 1){
- debugPrint("loop1")
- peripheral.discoverServices(nil)
- self.deviceX!.discoverServices(nil)
- }else if( i&2 == 0){
- debugPrint("loop0")
- self.deviceX!.discoverServices([self.serviceCBUUID_READ, self.serviceCBUUID_Write])
- }
- }else{
- self.timerForService.invalidate()
- }
- })
- }
- @interface CentralManager()
- property (nonatomic, strong) CBPeripheral *golablePeripheral;
- @end
- - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
- self.golablePeripheral = peripheral;
- self.golablePeripheral.delegate = self;
- }
- - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
- [self.golablePeripheral discoverServices: _serviceUUID];
- }
- - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
- for (CBService *service in peripheral.services) {
- if ([_serviceUUID containsObject: service.UUID]) {
- [self.golablePeripheral discoverCharacteristics:_serviceCharacteristics forService:service];
- [self.golablePeripheral discoverCharacteristics:_serviceNotifyCharacteristics forService:service];
- }
- }
- }
Add Comment
Please, Sign In to add comment