react-native-thermal-receipt-printer-image-qr icon indicating copy to clipboard operation
react-native-thermal-receipt-printer-image-qr copied to clipboard

BLEPrinter.getDeviceList() not response

Open ngondat97 opened this issue 2 years ago • 2 comments

image I don't get any response from func getDeviceList() including error message

ngondat97 avatar Aug 10 '22 02:08 ngondat97

I have the same issue @thiendangit, any idea?

vramaharba avatar Dec 21 '22 02:12 vramaharba

RCT_EXPORT_METHOD(getDeviceList:(RCTResponseSenderBlock)successCallback
                  fail:(RCTResponseSenderBlock)errorCallback) {
    @try {
        !_printerArray ? [NSException raise:@"Null pointer exception" format:@"Must call init function first"] : nil;
        
        __block BOOL foundDevice = NO; // flag để kiểm tra xem có thiết bị nào được tìm thấy hay không
        
        [[PrinterSDK defaultPrinterSDK] scanPrintersWithCompletion:^(Printer* printer){
            foundDevice = YES; // đánh dấu đã tìm thấy thiết bị
            [self->_printerArray addObject:printer];
        }];
        
        // chờ cho hàm scanPrintersWithCompletion kết thúc
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (!foundDevice) {
                // không có thiết bị nào được tìm thấy
                successCallback(@[]);
            } else {
                NSMutableArray *mapped = [NSMutableArray arrayWithCapacity:[self->_printerArray count]];
                [self->_printerArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    if ([obj isKindOfClass:[Printer class]]) {
                        Printer *printer = (Printer *)obj;
                        NSDictionary *dict = @{ @"device_name" : printer.name, @"inner_mac_address" : printer.UUIDString};
                        [mapped addObject:dict];
                    }
                }];
                NSMutableArray *uniquearray = (NSMutableArray *)[[NSSet setWithArray:mapped] allObjects];
                successCallback(@[uniquearray]);
            }
        });
    } @catch (NSException *exception) {
        errorCallback(@[exception.reason]);
    }
}

megabeehothanhminh avatar Apr 03 '23 03:04 megabeehothanhminh