Guest User

Untitled

a guest
May 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. if central.state == CBManagerState.poweredOn {
  2. debugPrint("poweredOn")
  3.  
  4. let serviceUUIDs:[AnyObject] = [serviceCBUUID_READ]
  5. let lastPeripherals = centralManager.retrieveConnectedPeripherals(withServices: serviceUUIDs as! [CBUUID])
  6.  
  7. if lastPeripherals.count > 0{
  8. let device = lastPeripherals.last! as CBPeripheral;
  9. deviceX = device;
  10. centralManager.connect(deviceX, options: nil)
  11. if(device.state == .disconnected){
  12. self.alertShowing(msg: "Device is disconnected restart the device and connect again.")
  13. }
  14. }
  15. else {
  16. centralManager.scanForPeripherals(withServices: nil, options: nil)
  17. }
  18.  
  19. debugPrint(lastPeripherals)
  20. } else if(central.state == CBManagerState.poweredOff) {
  21. self.alertShowing(msg: "Make sure that your bluetooth is turned on.")
  22. }
  23. else if(central.state == CBManagerState.unsupported) {
  24. self.alertShowing(msg: "This device is unsupported.")
  25. }else{
  26. self.alertShowing(msg: "Try again after restarting the device.")
  27. }
  28. }
  29.  
  30. func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
  31. if(peripheral.name == "Peripheral Observer") || (peripheral.name == "BLE Device"){
  32. if let services = peripheral.services{
  33. for service in services{
  34. debugPrint(service)
  35. }
  36. }
  37. debugPrint("advertisementData :(advertisementData)")
  38. deviceX = peripheral
  39. peripheral.delegate = self
  40. centralManager.stopScan()
  41. centralManager.connect(peripheral)
  42. }
  43. }
  44.  
  45. func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
  46. debugPrint("Connected")
  47. var i = 0
  48. timerForService = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
  49. if(self.service_Read == nil) && (self.service_Write == nil){
  50. i += 1
  51.  
  52. if(i%2 == 1){
  53. debugPrint("loop1")
  54. peripheral.discoverServices(nil)
  55. self.deviceX!.discoverServices(nil)
  56. }else if( i&2 == 0){
  57. debugPrint("loop0")
  58. self.deviceX!.discoverServices([self.serviceCBUUID_READ, self.serviceCBUUID_Write])
  59. }
  60.  
  61. }else{
  62. self.timerForService.invalidate()
  63. }
  64. })
  65. }
  66.  
  67. @interface CentralManager()
  68. property (nonatomic, strong) CBPeripheral *golablePeripheral;
  69. @end
  70.  
  71. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
  72. self.golablePeripheral = peripheral;
  73. self.golablePeripheral.delegate = self;
  74. }
  75.  
  76. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
  77. [self.golablePeripheral discoverServices: _serviceUUID];
  78. }
  79.  
  80. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
  81. for (CBService *service in peripheral.services) {
  82. if ([_serviceUUID containsObject: service.UUID]) {
  83. [self.golablePeripheral discoverCharacteristics:_serviceCharacteristics forService:service];
  84. [self.golablePeripheral discoverCharacteristics:_serviceNotifyCharacteristics forService:service];
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment