Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RNBluetoothManager.m 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. //
  2. // RNBluethManager.m
  3. // RNBluetoothEscposPrinter
  4. //
  5. // Created by januslo on 2018/9/28.
  6. // Copyright © 2018年 Facebook. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RNBluetoothManager.h"
  10. #import <CoreBluetooth/CoreBluetooth.h>
  11. @implementation RNBluetoothManager
  12. NSString *EVENT_DEVICE_ALREADY_PAIRED = @"EVENT_DEVICE_ALREADY_PAIRED";
  13. NSString *EVENT_DEVICE_DISCOVER_DONE = @"EVENT_DEVICE_DISCOVER_DONE";
  14. NSString *EVENT_DEVICE_FOUND = @"EVENT_DEVICE_FOUND";
  15. NSString *EVENT_CONNECTION_LOST = @"EVENT_CONNECTION_LOST";
  16. NSString *EVENT_UNABLE_CONNECT=@"EVENT_UNABLE_CONNECT";
  17. NSString *EVENT_CONNECTED=@"EVENT_CONNECTED";
  18. static NSArray<CBUUID *> *supportServices = nil;
  19. static NSDictionary *writeableCharactiscs = nil;
  20. bool hasListeners;
  21. static CBPeripheral *connected;
  22. static RNBluetoothManager *instance;
  23. static NSObject<WriteDataToBleDelegate> *writeDataDelegate;// delegate of write data resule;
  24. static NSData *toWrite;
  25. static NSTimer *timer;
  26. +(Boolean)isConnected{
  27. return !(connected==nil);
  28. }
  29. +(void)writeValue:(NSData *) data withDelegate:(NSObject<WriteDataToBleDelegate> *) delegate
  30. {
  31. @try{
  32. writeDataDelegate = delegate;
  33. toWrite = data;
  34. connected.delegate = instance;
  35. [connected discoverServices:supportServices];
  36. // [connected writeValue:data forCharacteristic:[writeableCharactiscs objectForKey:supportServices[0]] type:CBCharacteristicWriteWithoutResponse];
  37. }
  38. @catch(NSException *e){
  39. NSLog(@"error in writing data to %@,issue:%@",connected,e);
  40. [writeDataDelegate didWriteDataToBle:false];
  41. }
  42. }
  43. // Will be called when this module's first listener is added.
  44. -(void)startObserving {
  45. hasListeners = YES;
  46. // Set up any upstream listeners or background tasks as necessary
  47. }
  48. // Will be called when this module's last listener is removed, or on dealloc.
  49. -(void)stopObserving {
  50. hasListeners = NO;
  51. // Remove upstream listeners, stop unnecessary background tasks
  52. }
  53. /**
  54. * Exports the constants to javascritp.
  55. **/
  56. - (NSDictionary *)constantsToExport
  57. {
  58. /*
  59. EVENT_DEVICE_ALREADY_PAIRED Emits the devices array already paired
  60. EVENT_DEVICE_DISCOVER_DONE Emits when the scan done
  61. EVENT_DEVICE_FOUND Emits when device found during scan
  62. EVENT_CONNECTION_LOST Emits when device connection lost
  63. EVENT_UNABLE_CONNECT Emits when error occurs while trying to connect device
  64. EVENT_CONNECTED Emits when device connected
  65. */
  66. return @{ EVENT_DEVICE_ALREADY_PAIRED: EVENT_DEVICE_ALREADY_PAIRED,
  67. EVENT_DEVICE_DISCOVER_DONE:EVENT_DEVICE_DISCOVER_DONE,
  68. EVENT_DEVICE_FOUND:EVENT_DEVICE_FOUND,
  69. EVENT_CONNECTION_LOST:EVENT_CONNECTION_LOST,
  70. EVENT_UNABLE_CONNECT:EVENT_UNABLE_CONNECT,
  71. EVENT_CONNECTED:EVENT_CONNECTED
  72. };
  73. }
  74. - (dispatch_queue_t)methodQueue
  75. {
  76. return dispatch_get_main_queue();
  77. }
  78. + (BOOL)requiresMainQueueSetup
  79. {
  80. return YES;
  81. }
  82. /**
  83. * Defines the event would be emited.
  84. **/
  85. - (NSArray<NSString *> *)supportedEvents
  86. {
  87. return @[EVENT_DEVICE_DISCOVER_DONE,
  88. EVENT_DEVICE_FOUND,
  89. EVENT_UNABLE_CONNECT,
  90. EVENT_CONNECTION_LOST,
  91. EVENT_CONNECTED,
  92. EVENT_DEVICE_ALREADY_PAIRED];
  93. }
  94. RCT_EXPORT_MODULE(BluetoothManager);
  95. //isBluetoothEnabled
  96. RCT_EXPORT_METHOD(isBluetoothEnabled:(RCTPromiseResolveBlock)resolve
  97. rejecter:(RCTPromiseRejectBlock)reject)
  98. {
  99. CBManagerState state = [self.centralManager state];
  100. resolve(state == CBManagerStatePoweredOn?@"true":@"false");//canot pass boolean or int value to resolve directly.
  101. }
  102. //enableBluetooth
  103. RCT_EXPORT_METHOD(enableBluetooth:(RCTPromiseResolveBlock)resolve
  104. rejecter:(RCTPromiseRejectBlock)reject)
  105. {
  106. resolve(nil);
  107. }
  108. //disableBluetooth
  109. RCT_EXPORT_METHOD(disableBluetooth:(RCTPromiseResolveBlock)resolve
  110. rejecter:(RCTPromiseRejectBlock)reject)
  111. {
  112. resolve(nil);
  113. }
  114. //scanDevices
  115. RCT_EXPORT_METHOD(scanDevices:(RCTPromiseResolveBlock)resolve
  116. rejecter:(RCTPromiseRejectBlock)reject)
  117. {
  118. @try{
  119. if(!self.centralManager || self.centralManager.state!=CBManagerStatePoweredOn){
  120. reject(@"BLUETOOTCH_INVALID_STATE",@"BLUETOOTCH_INVALID_STATE",nil);
  121. return;
  122. }
  123. if(self.centralManager.isScanning){
  124. [self.centralManager stopScan];
  125. }
  126. self.scanResolveBlock = resolve;
  127. self.scanRejectBlock = reject;
  128. if(connected && connected.identifier){
  129. NSDictionary *idAndName =@{@"address":connected.identifier.UUIDString,@"name":connected.name?connected.name:@""};
  130. NSDictionary *peripheralStored = @{connected.identifier.UUIDString:connected};
  131. if(!self.foundDevices){
  132. self.foundDevices = [[NSMutableDictionary alloc] init];
  133. }
  134. [self.foundDevices addEntriesFromDictionary:peripheralStored];
  135. if(hasListeners){
  136. [self sendEventWithName:EVENT_DEVICE_FOUND body:@{@"device":idAndName}];
  137. }
  138. }
  139. [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@NO}];
  140. //Callbacks:
  141. //centralManager:didDiscoverPeripheral:advertisementData:RSSI:
  142. NSLog(@"Scanning started with services.");
  143. if(timer && timer.isValid){
  144. [timer invalidate];
  145. timer = nil;
  146. }
  147. timer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(callStop) userInfo:nil repeats:NO];
  148. }
  149. @catch(NSException *exception){
  150. NSLog(@"ERROR IN STARTING SCANE %@",exception);
  151. reject([exception name],[exception name],nil);
  152. }
  153. }
  154. //stop scan
  155. RCT_EXPORT_METHOD(stopScan:(RCTPromiseResolveBlock)resolve
  156. rejecter:(RCTPromiseRejectBlock)reject)
  157. {
  158. [self callStop];
  159. resolve(nil);
  160. }
  161. //connect(address)
  162. RCT_EXPORT_METHOD(connect:(NSString *)address
  163. findEventsWithResolver:(RCTPromiseResolveBlock)resolve
  164. rejecter:(RCTPromiseRejectBlock)reject)
  165. {
  166. NSLog(@"Trying to connect....%@",address);
  167. [self callStop];
  168. if(connected){
  169. NSString *connectedAddress =connected.identifier.UUIDString;
  170. if([address isEqualToString:connectedAddress]){
  171. resolve(nil);
  172. return;
  173. }else{
  174. [self.centralManager cancelPeripheralConnection:connected];
  175. //Callbacks:
  176. //entralManager:didDisconnectPeripheral:error:
  177. }
  178. }
  179. CBPeripheral *peripheral = [self.foundDevices objectForKey:address];
  180. self.connectResolveBlock = resolve;
  181. self.connectRejectBlock = reject;
  182. if(peripheral){
  183. _waitingConnect = address;
  184. NSLog(@"Trying to connectPeripheral....%@",address);
  185. [self.centralManager connectPeripheral:peripheral options:nil];
  186. // Callbacks:
  187. // centralManager:didConnectPeripheral:
  188. // centralManager:didFailToConnectPeripheral:error:
  189. }else{
  190. //starts the scan.
  191. _waitingConnect = address;
  192. NSLog(@"Scan to find ....%@",address);
  193. [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@NO}];
  194. //Callbacks:
  195. //centralManager:didDiscoverPeripheral:advertisementData:RSSI:
  196. }
  197. }
  198. //unpaire(address)
  199. -(void)callStop{
  200. if(self.centralManager.isScanning){
  201. [self.centralManager stopScan];
  202. NSMutableArray *devices = [[NSMutableArray alloc] init];
  203. for(NSString *key in self.foundDevices){
  204. NSLog(@"insert found devies:%@ =>%@",key,[self.foundDevices objectForKey:key]);
  205. NSString *name = [self.foundDevices objectForKey:key].name;
  206. if(!name){
  207. name = @"";
  208. }
  209. [devices addObject:@{@"address":key,@"name":name}];
  210. }
  211. NSError *error = nil;
  212. NSData* jsonData = [NSJSONSerialization dataWithJSONObject:devices options:NSJSONWritingPrettyPrinted error:&error];
  213. NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  214. if(hasListeners){
  215. [self sendEventWithName:EVENT_DEVICE_DISCOVER_DONE body:@{@"found":jsonStr,@"paired":@"[]"}];
  216. }
  217. if(self.scanResolveBlock){
  218. RCTPromiseResolveBlock rsBlock = self.scanResolveBlock;
  219. rsBlock(@{@"found":jsonStr,@"paired":@"[]"});
  220. self.scanResolveBlock = nil;
  221. }
  222. }
  223. if(timer && timer.isValid){
  224. [timer invalidate];
  225. timer = nil;
  226. }
  227. self.scanRejectBlock = nil;
  228. self.scanResolveBlock = nil;
  229. }
  230. - (void) initSupportServices
  231. {
  232. if(!supportServices){
  233. CBUUID *issc = [CBUUID UUIDWithString: @"49535343-FE7D-4AE5-8FA9-9FAFD205E455"];
  234. supportServices = [NSArray arrayWithObject:issc];/*ISSC*/
  235. writeableCharactiscs = @{issc:@"49535343-8841-43F4-A8D4-ECBE34729BB3"};
  236. }
  237. }
  238. - (CBCentralManager *) centralManager
  239. {
  240. @synchronized(_centralManager)
  241. {
  242. if (!_centralManager)
  243. {
  244. if (![CBCentralManager instancesRespondToSelector:@selector(initWithDelegate:queue:options:)])
  245. {
  246. //for ios version lowser than 7.0
  247. self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
  248. }else
  249. {
  250. self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options: nil];
  251. }
  252. }
  253. if(!instance){
  254. instance = self;
  255. }
  256. }
  257. [self initSupportServices];
  258. return _centralManager;
  259. }
  260. /**
  261. * CBCentralManagerDelegate
  262. **/
  263. - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
  264. NSLog(@"%ld",(long)central.state);
  265. }
  266. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI{
  267. NSLog(@"did discover peripheral: %@",peripheral);
  268. NSDictionary *idAndName =@{@"address":peripheral.identifier.UUIDString,@"name":peripheral.name?peripheral.name:@""};
  269. NSDictionary *peripheralStored = @{peripheral.identifier.UUIDString:peripheral};
  270. if(!self.foundDevices){
  271. self.foundDevices = [[NSMutableDictionary alloc] init];
  272. }
  273. [self.foundDevices addEntriesFromDictionary:peripheralStored];
  274. if(hasListeners){
  275. [self sendEventWithName:EVENT_DEVICE_FOUND body:@{@"device":idAndName}];
  276. }
  277. if(_waitingConnect && [_waitingConnect isEqualToString: peripheral.identifier.UUIDString]){
  278. [self.centralManager connectPeripheral:peripheral options:nil];
  279. [self callStop];
  280. }
  281. }
  282. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
  283. NSLog(@"did connected: %@",peripheral);
  284. connected = peripheral;
  285. NSString *pId = peripheral.identifier.UUIDString;
  286. if(_waitingConnect && [_waitingConnect isEqualToString: pId] && self.connectResolveBlock){
  287. NSLog(@"Predefined the support services, stop to looking up services.");
  288. // peripheral.delegate=self;
  289. // [peripheral discoverServices:nil];
  290. self.connectResolveBlock(nil);
  291. _waitingConnect = nil;
  292. self.connectRejectBlock = nil;
  293. self.connectResolveBlock = nil;
  294. }
  295. NSLog(@"going to emit EVENT_CONNECTED.");
  296. if(hasListeners){
  297. [self sendEventWithName:EVENT_CONNECTED body:@{@"device":@{@"name":peripheral.name?peripheral.name:@"",@"address":peripheral.identifier.UUIDString}}];
  298. }
  299. }
  300. - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
  301. if(!connected && _waitingConnect && [_waitingConnect isEqualToString:peripheral.identifier.UUIDString]){
  302. if(self.connectRejectBlock){
  303. RCTPromiseRejectBlock rjBlock = self.connectRejectBlock;
  304. rjBlock(@"",@"",error);
  305. self.connectRejectBlock = nil;
  306. self.connectResolveBlock = nil;
  307. _waitingConnect=nil;
  308. }
  309. connected = nil;
  310. if(hasListeners){
  311. [self sendEventWithName:EVENT_UNABLE_CONNECT body:@{@"name":peripheral.name?peripheral.name:@"",@"address":peripheral.identifier.UUIDString}];
  312. }
  313. }else{
  314. connected = nil;
  315. if(hasListeners){
  316. [self sendEventWithName:EVENT_CONNECTION_LOST body:nil];
  317. }
  318. }
  319. }
  320. - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
  321. if(self.connectRejectBlock){
  322. RCTPromiseRejectBlock rjBlock = self.connectRejectBlock;
  323. rjBlock(@"",@"",error);
  324. self.connectRejectBlock = nil;
  325. self.connectResolveBlock = nil;
  326. _waitingConnect = nil;
  327. }
  328. connected = nil;
  329. if(hasListeners){
  330. [self sendEventWithName:EVENT_UNABLE_CONNECT body:@{@"name":peripheral.name?peripheral.name:@"",@"address":peripheral.identifier.UUIDString}];
  331. }
  332. }
  333. /**
  334. * END OF CBCentralManagerDelegate
  335. **/
  336. /*!
  337. * @method peripheral:didDiscoverServices:
  338. *
  339. * @param peripheral The peripheral providing this information.
  340. * @param error If an error occurred, the cause of the failure.
  341. *
  342. * @discussion This method returns the result of a @link discoverServices: @/link call. If the service(s) were read successfully, they can be retrieved via
  343. * <i>peripheral</i>'s @link services @/link property.
  344. *
  345. */
  346. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
  347. if (error){
  348. NSLog(@"扫描外设服务出错:%@-> %@", peripheral.name, [error localizedDescription]);
  349. return;
  350. }
  351. NSLog(@"扫描到外设服务:%@ -> %@",peripheral.name,peripheral.services);
  352. for (CBService *service in peripheral.services) {
  353. [peripheral discoverCharacteristics:nil forService:service];
  354. NSLog(@"服务id:%@",service.UUID.UUIDString);
  355. }
  356. NSLog(@"开始扫描外设服务的特征 %@...",peripheral.name);
  357. if(error && self.connectRejectBlock){
  358. RCTPromiseRejectBlock rjBlock = self.connectRejectBlock;
  359. rjBlock(@"",@"",error);
  360. self.connectRejectBlock = nil;
  361. self.connectResolveBlock = nil;
  362. connected = nil;
  363. }else
  364. if(_waitingConnect && _waitingConnect == peripheral.identifier.UUIDString){
  365. RCTPromiseResolveBlock rsBlock = self.connectResolveBlock;
  366. rsBlock(peripheral.identifier.UUIDString);
  367. self.connectRejectBlock = nil;
  368. self.connectResolveBlock = nil;
  369. connected = peripheral;
  370. }
  371. }
  372. /*!
  373. * @method peripheral:didDiscoverCharacteristicsForService:error:
  374. *
  375. * @param peripheral The peripheral providing this information.
  376. * @param service The <code>CBService</code> object containing the characteristic(s).
  377. * @param error If an error occurred, the cause of the failure.
  378. *
  379. * @discussion This method returns the result of a @link discoverCharacteristics:forService: @/link call. If the characteristic(s) were read successfully,
  380. * they can be retrieved via <i>service</i>'s <code>characteristics</code> property.
  381. */
  382. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{
  383. if(toWrite && connected
  384. && [connected.identifier.UUIDString isEqualToString:peripheral.identifier.UUIDString]
  385. && [service.UUID.UUIDString isEqualToString:supportServices[0].UUIDString]){
  386. if(error){
  387. NSLog(@"Discrover charactoreristics error:%@",error);
  388. if(writeDataDelegate)
  389. {
  390. [writeDataDelegate didWriteDataToBle:false];
  391. return;
  392. }
  393. }
  394. for(CBCharacteristic *cc in service.characteristics){
  395. NSLog(@"Characterstic found: %@ in service: %@" ,cc,service.UUID.UUIDString);
  396. if([cc.UUID.UUIDString isEqualToString:[writeableCharactiscs objectForKey: supportServices[0]]]){
  397. @try{
  398. [connected writeValue:toWrite forCharacteristic:cc type:CBCharacteristicWriteWithoutResponse];
  399. if(writeDataDelegate) [writeDataDelegate didWriteDataToBle:true];
  400. if(toWrite){
  401. NSLog(@"Value wrote: %lu",[toWrite length]);
  402. }
  403. }
  404. @catch(NSException *e){
  405. NSLog(@"ERRO IN WRITE VALUE: %@",e);
  406. [writeDataDelegate didWriteDataToBle:false];
  407. }
  408. }
  409. }
  410. }
  411. if(error){
  412. NSLog(@"Discrover charactoreristics error:%@",error);
  413. return;
  414. }
  415. // ServiceUUID:49535343-fe7d-4ae5-8fa9-9fafd205e455;
  416. // 写的是
  417. //characteristicUUID:49535343-8841-43f4-a8d4-ecbe34729bb3;
  418. // 读的是
  419. //characteristicUUID:49535343-1e4d-4bd9-ba61-23c647249616;
  420. //
  421. // 如果要写,翻译成base64位;
  422. //
  423. // 调用监听改变,需要去使能设备的Notify,var param={
  424. // serviceUUID: '000018f0-0000-1000-8000-00805f9b34fb',//service的UUID 这个值需要获取查看设备,我暂认为他是通用的
  425. // characteristicUUID:'00002af0-0000-1000-8000-00805f9b34fb',//characteristic的UUID 这个值也需要获取查看,我认为他是通用的
  426. // enable:true //true 或false,开启或关闭监听
  427. // };
  428. // param = JSON.stringify(param);
  429. // uexBluetoothLE.setCharacteristicNotification(param);
  430. /** TESTING NSLOG OUTPUT:: ***/
  431. // 2018-10-01 21:29:24.136033+0800 bluetoothEscposPrinterExamples[8239:4598148] Trying to connect....D7D39238-EF56-71A7-7DCC-D464EFD3BFF1
  432. // 2018-10-01 21:29:24.302880+0800 bluetoothEscposPrinterExamples[8239:4598148] did connected: <CBPeripheral: 0x1c4302d90, identifier = D7D39238-EF56-71A7-7DCC-D464EFD3BFF1, name = BlueTooth Printer, state = connected>
  433. // 2018-10-01 21:29:24.302982+0800 bluetoothEscposPrinterExamples[8239:4598148] going to discover services.
  434. // 2018-10-01 21:29:24.303375+0800 bluetoothEscposPrinterExamples[8239:4598148] going to emit EVEnT_CONNECTED.
  435. // 2018-10-01 21:29:24.431164+0800 bluetoothEscposPrinterExamples[8239:4598148] 扫描到外设服务:BlueTooth Printer -> (
  436. // "<CBService: 0x1c246b200, isPrimary = YES, UUID = 49535343-FE7D-4AE5-8FA9-9FAFD205E455>",
  437. // "<CBService: 0x1c246b280, isPrimary = YES, UUID = 18F0>",
  438. // "<CBService: 0x1c246a740, isPrimary = YES, UUID = E7810A71-73AE-499D-8C15-FAA9AEF0C3F2>"
  439. // )
  440. // 2018-10-01 21:29:24.431354+0800 bluetoothEscposPrinterExamples[8239:4598148] 服务id:49535343-FE7D-4AE5-8FA9-9FAFD205E455
  441. // 2018-10-01 21:29:24.431448+0800 bluetoothEscposPrinterExamples[8239:4598148] 服务id:18F0
  442. // 2018-10-01 21:29:24.431535+0800 bluetoothEscposPrinterExamples[8239:4598148] 服务id:E7810A71-73AE-499D-8C15-FAA9AEF0C3F2
  443. // 2018-10-01 21:29:24.431552+0800 bluetoothEscposPrinterExamples[8239:4598148] 开始扫描外设服务的特征 BlueTooth Printer...
  444. // 2018-10-01 21:29:24.432374+0800 bluetoothEscposPrinterExamples[8239:4598148] Characterstic found: <CBCharacteristic: 0x1c04afa20, UUID = 49535343-1E4D-4BD9-BA61-23C647249616, properties = 0x10, value = <5f47505f 4c383031 3630>, notifying = NO> in service: 49535343-FE7D-4AE5-8FA9-9FAFD205E455
  445. // 2018-10-01 21:29:24.432406+0800 bluetoothEscposPrinterExamples[8239:4598148] Notify
  446. // 2018-10-01 21:29:24.432417+0800 bluetoothEscposPrinterExamples[8239:4598148] known properties: 16
  447. // 2018-10-01 21:29:24.432455+0800 bluetoothEscposPrinterExamples[8239:4598148] Characterstic found: <CBCharacteristic: 0x1c04af480, UUID = 49535343-8841-43F4-A8D4-ECBE34729BB3, properties = 0xC, value = (null), notifying = NO> in service: 49535343-FE7D-4AE5-8FA9-9FAFD205E455
  448. // 2018-10-01 21:29:24.432753+0800 bluetoothEscposPrinterExamples[8239:4598148] WriteWithoutResponse
  449. // 2018-10-01 21:29:24.432772+0800 bluetoothEscposPrinterExamples[8239:4598148] Write
  450. // 2018-10-01 21:29:24.432785+0800 bluetoothEscposPrinterExamples[8239:4598148] known properties: 12
  451. // 2018-10-01 21:29:24.432988+0800 bluetoothEscposPrinterExamples[8239:4598148] Characterstic found: <CBCharacteristic: 0x1c44ac9c0, UUID = 2AF0, properties = 0x30, value = (null), notifying = NO> in service: 18F0
  452. // 2018-10-01 21:29:24.433005+0800 bluetoothEscposPrinterExamples[8239:4598148] Notify
  453. // 2018-10-01 21:29:24.433015+0800 bluetoothEscposPrinterExamples[8239:4598148] Indicate
  454. // 2018-10-01 21:29:24.433024+0800 bluetoothEscposPrinterExamples[8239:4598148] known properties: 48
  455. // 2018-10-01 21:29:24.433079+0800 bluetoothEscposPrinterExamples[8239:4598148] Characterstic found: <CBCharacteristic: 0x1c44aca80, UUID = 2AF1, properties = 0xC, value = (null), notifying = NO> in service: 18F0
  456. // 2018-10-01 21:29:24.433647+0800 bluetoothEscposPrinterExamples[8239:4598148] WriteWithoutResponse
  457. // 2018-10-01 21:29:24.433662+0800 bluetoothEscposPrinterExamples[8239:4598148] Write
  458. // 2018-10-01 21:29:24.433672+0800 bluetoothEscposPrinterExamples[8239:4598148] known properties: 12
  459. // 2018-10-01 21:29:24.433900+0800 bluetoothEscposPrinterExamples[8239:4598148] Characterstic found: <CBCharacteristic: 0x1c44ac780, UUID = BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F, properties = 0x3E, value = (null), notifying = NO> in service: E7810A71-73AE-499D-8C15-FAA9AEF0C3F2
  460. // 2018-10-01 21:29:24.433928+0800 bluetoothEscposPrinterExamples[8239:4598148] Read
  461. // 2018-10-01 21:29:24.433953+0800 bluetoothEscposPrinterExamples[8239:4598148] WriteWithoutResponse
  462. // 2018-10-01 21:29:24.433964+0800 bluetoothEscposPrinterExamples[8239:4598148] Write
  463. // 2018-10-01 21:29:24.433973+0800 bluetoothEscposPrinterExamples[8239:4598148] Notify
  464. // 2018-10-01 21:29:24.434378+0800 bluetoothEscposPrinterExamples[8239:4598148] Indicate
  465. // 2018-10-01 21:29:24.434389+0800 bluetoothEscposPrinterExamples[8239:4598148] known properties: 62
  466. // for(CBCharacteristic *cc in service.characteristics){
  467. // // NSLog(@"Characterstic found: %@ in service: %@" ,cc,service.UUID.UUIDString);
  468. // CBCharacteristicProperties pro = cc.properties;
  469. // Byte p = (Byte)pro;
  470. //// CBCharacteristicPropertyBroadcast = 0x01,
  471. //// CBCharacteristicPropertyRead = 0x02,
  472. //// CBCharacteristicPropertyWriteWithoutResponse = 0x04,
  473. //// CBCharacteristicPropertyWrite = 0x08,
  474. //// CBCharacteristicPropertyNotify = 0x10,
  475. //// CBCharacteristicPropertyIndicate = 0x20,
  476. //// CBCharacteristicPropertyAuthenticatedSignedWrites = 0x40,
  477. //// CBCharacteristicPropertyExtendedProperties = 0x80,
  478. //// CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(10_9, 6_0) = 0x100,
  479. //// CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(10_9, 6_0) = 0x200
  480. // if((p) & 0x01){
  481. // NSLog(@"Broadcast");
  482. // }
  483. // if((p>>1) & 0x01){
  484. // NSLog(@"Read");
  485. // }
  486. // if((p>>2) & 0x01){
  487. // NSLog(@"WriteWithoutResponse");
  488. // }
  489. // if((p>>3) & 0x01){
  490. // NSLog(@"Write");
  491. // }
  492. // if((p>>4) & 0x01){
  493. // NSLog(@"Notify");
  494. // }
  495. // if((p>>5) & 0x01){
  496. // NSLog(@"Indicate");
  497. // }
  498. // if((p>>6) & 0x01){
  499. // NSLog(@"AuthenticatedSignedWrites");
  500. // }
  501. // if((p>>7) & 0x01){
  502. // NSLog(@"ExtendedProperties");
  503. // }
  504. // {
  505. // NSLog(@"known properties: %lu", pro);
  506. // }
  507. // }
  508. }
  509. /*!
  510. * @method peripheral:didWriteValueForCharacteristic:error:
  511. *
  512. * @param peripheral The peripheral providing this information.
  513. * @param characteristic A <code>CBCharacteristic</code> object.
  514. * @param error If an error occurred, the cause of the failure.
  515. *
  516. * @discussion This method returns the result of a {@link writeValue:forCharacteristic:type:} call, when the <code>CBCharacteristicWriteWithResponse</code> type is used.
  517. */
  518. - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error{
  519. if(error){
  520. NSLog(@"Error in writing bluetooth: %@",error);
  521. if(writeDataDelegate){
  522. [writeDataDelegate didWriteDataToBle:false];
  523. }
  524. }
  525. NSLog(@"Write bluetooth success.");
  526. if(writeDataDelegate){
  527. [writeDataDelegate didWriteDataToBle:true];
  528. }
  529. }
  530. @end