I have wsdl service. All request and response saving in minio bucket. But when content too big, in minio saved only part of this content.
My Interceptor
#Slf4j
#Component
public class LogResponseInterceptor extends AbstractPhaseInterceptor<Message> {
private final MinioService minioService;
private final AdministrationWebClient administrationWebClient;
public LogResponseInterceptor(MinioService minioService, AdministrationWebClient administrationWebClient) {
super(Phase.PRE_PROTOCOL_ENDING);
this.minioService = minioService;
this.administrationWebClient = administrationWebClient;
}
#Override
public void handleMessage(Message message) throws Fault {
try (CachedOutputStream outputStream = ((CachedOutputStream) message.getContent(OutputStream.class));
InputStream inputStream = outputStream.getInputStream()) {
UUID uuid = UUID.randomUUID();
minioService.uploadFile(
inputStream, Constantas.MinioConst.MINIO_SPV_OUT_PATH, uuid.toString(), "application/xml"
);
SpvRecord spvRecord = SpvRequestContext.getSpvRequest();
spvRecord.setRequestState(RequestState.COMPLETED);
spvRecord.setResponseXmlUrl(Constantas.MinioConst.MINIO_SPV_OUT_PATH + uuid);
spvRecord.setFinishDate(ZonedDateTime.now());
log.info("End spv action with {}", spvRecord);
spvRecord.setRequestNumber(administrationWebClient.getSpvLastRequestNumber() + 1);
administrationWebClient.createSpvRecord(spvRecord);
} catch (IOException e) {
log.error("### Error while get response xml");
throw new RuntimeException(e);
} finally {
SpvRequestContext.clear();
}
}
}
CachedOutputStream contains only part of response body. How i can get full response body?
You can try lowering the level of the phase.The attachment interceptor is at the receive level (version 3.3.7).
To get the request you can try this, copy the original content to be able to flush it.
// now get the request xml
InputStream is = message.getContent ( InputStream.class );
CachedOutputStream os = new CachedOutputStream ( );
IOUtils.copy ( is, os );
os.flush ( );
message.setContent ( InputStream.class, os.getInputStream ( ) );
is.close ( );
System.out.println ("The request is: " + IOUtils.toString ( os.getInputStream ( ) ));
os.close ( );
To get the response, you need made custom implementation del original
https://github.com/apache/cxf/blob/main/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutInterceptor.java
and modify the method onClose(CachedOutputStream cos) with this
public void onClose(CachedOutputStream cos) {
final LogEvent event = eventMapper.map(message);
if (shouldLogContent(event)) {
copyPayload(cos, event);
} else {
event.setPayload(CONTENT_SUPPRESSED);
}
StringBuilder sb = new StringBuilder("\nOutbound Message\n---------------------------\n");
sb.append("ID: " + event.getExchangeId() + " \n");
sb.append("Address: " + event.getAddress() + " \n");
sb.append("Encoding: " + event.getEncoding() + " \n");
sb.append("Content-Type: " + event.getContentType() + " \n");
sb.append("Headers: " + event.getHeaders() + " \n");
if(event.isTruncated()) {
sb.append("Payload (truncated) : " + event.getPayload() + " \n");
} else {
sb.append("Payload: " + event.getPayload() + " \n");
}
sb.append("---------------------------");
this.logger.trace(sb.toString());
try {
// empty out the cache
cos.lockOutputStream();
cos.resetOut(null, false);
} catch (Exception ex) {
// ignore
}
message.setContent(OutputStream.class, origStream);
}
IMPORTANT: the limit truncate the response, set limit to max for read the entire response
Related
I'm creating an application that uses web view to load the website and allow to download data like IMG/PDF/GIF. The problem is that the download link is not a normal link it's blob:.
I know blob: URL does not refer to the data that exists on the server, it refers to data that your browser currently has in memory.
class DownloadBlobFileJSInterface {
private Context mContext;
private DownloadGifSuccessListener mDownloadGifSuccessListener;
public DownloadBlobFileJSInterface(Context context) {
this.mContext = context;
}
public static String getBase64StringFromBlobUrl(String blobUrl) {
if (blobUrl.startsWith("blob")) {
return "javascript: var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '" + blobUrl + "', true);" +
"xhr.setRequestHeader('Content-type','image/gif');" +
"xhr.responseType = 'blob';" +
"xhr.onload = function(e) {" +
" if (this.status == 200) {" +
" var blobFile = this.response;" +
" var reader = new FileReader();" +
" reader.readAsDataURL(blobFile);" +
" reader.onloadend = function() {" +
" base64data = reader.result;" +
" Android.getBase64FromBlobData(base64data);" +
" }" +
" }" +
"};" +
"xhr.send();";
}
return "javascript: console.log('It is not a Blob URL');";
}
public void setDownloadGifSuccessListener(DownloadGifSuccessListener listener) {
mDownloadGifSuccessListener = listener;
}
#JavascriptInterface
public void getBase64FromBlobData(String base64Data) {
convertToGifAndProcess(base64Data);
}
private void convertToGifAndProcess(String base64) {
ContextWrapper wrapper = new ContextWrapper(mContext);
String fullPath =wrapper.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
File gifFile = new File(fullPath+ "/File_" + System.currentTimeMillis() + "_.gif");
saveGifToPath(base64, gifFile);
Toast.makeText(mContext, "Downloaded", Toast.LENGTH_SHORT).show();
if (mDownloadGifSuccessListener != null) {
mDownloadGifSuccessListener.downloadGifSuccess(gifFile.getAbsolutePath());
}
}
private void saveGifToPath(String base64, File gifFilePath) {
try {
byte[] fileBytes = Base64.decode(base64.replaceFirst(
"data:image/gif;base64,", ""), 0);
FileOutputStream os = new FileOutputStream(gifFilePath, false);
os.write(fileBytes);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public interface DownloadGifSuccessListener {
void downloadGifSuccess(String absolutePath);
}
}
I want to download the file in device storage.
Is there any way to get data from the blob: URL?
Or
Is it possible to send that data to chrome to download.
I know there is a lot of questions out there like this, this on StackOverflow, I almost tried all but didn't find any of these working.
I am working on a Java endpoint that I intend to use for HL7 message validation. I have a basic app running that uses a variation of the standard HAPI HL7 validation example. If I pass in a valid message I get the "Success" response. If I pass in a invalid message I still get a "Success" response.
The only way I get an error response is if the HL7 is badly formatted and the PipeParser throws an exception. In that case it gets caught in the catch block.
What I want to see is if I pass in an invalid message that it actually gets validated and returns all the validation errors. But I don't ever actually see any validation. It either parses or crashes trying to parse.
What am I missing here?
HapiContext context = new DefaultHapiContext();
ValidationContext validationContext = ValidationContextFactory.defaultValidation();
context.setValidationContext(validationContext);
try
{
context.getParserConfiguration().setUnexpectedSegmentBehaviour(UnexpectedSegmentBehaviourEnum.THROW_HL7_EXCEPTION);
Message messageValidationResults = context.getPipeParser().parse(hl7Message);
SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(context);
handler.setMinimumSeverityToCollect(Severity.INFO);
Validator<Boolean> validator = context.getMessageValidator();
if (!validator.validate(messageValidationResults, handler))
{
if (handler.getExceptions().size() == 0)
{
hl7ValidationResult = "SUCCESS - Message Validated Successfully";
}
else
{
hl7ValidationResult = "ERROR - Found " + handler.getExceptions().size() + " problems\n\n";
for (Exception e : handler.getExceptions())
{
hl7ValidationResult += (e.getClass().getSimpleName() + " - " + e.getMessage()) + "\n";
}
}
}
}
catch (Exception e)
{
hl7ValidationResult = "ERROR - " + e.getMessage();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString();
hl7ValidationResult += "\n\n" + sStackTrace;
}
Please ignore the answer if do you think is not correct, I stopped to work with HL7 but, looking at my old project I have found this and maybe it can help you to find the solution of your problem:
{
DefaultValidationBuilder builder = new DefaultValidationBuilder() {
private static final long serialVersionUID = 1L;
#Override
protected void configure() {
super.configure();
forVersion(Version.V26);
}
};
HapiContext context = new DefaultHapiContext();
context.setValidationRuleBuilder(builder);
PipeParser hapiParser = context.getPipeParser();
try {
hapiParser.parse(hl7Message);
} catch (ca.uhn.hl7v2.HL7Exception e) {
// String error, String language, String requisitionNumber, String controlId, String processinId, String senderApplication, String senderFacility
errors.add(new HL7ValidationError(
"HAPI Validator error found: " + e.getMessage(),
extractor.accessPatientDirectly().getLanguage(),
extractor.accessPatientDirectly().getRequisitionNumber(),
extractor.accessPatientDirectly().getControlID(),
"",
extractor.accessPatientDirectly().getSenderApplication(),
extractor.accessPatientDirectly().getSenderFacility())
);
log.debug("HAPI Validator error found: " + e.getMessage());
}
try {
context.close();
}
catch (Exception ex) {
log.debug("Unable to close HapiContext(): " + ex.getMessage());
}
}
Basically I used hapiParser.parse(hl7Message); and catch the HL7Exception
I want to compare the response from the server with a string, but I get a false result when testing the two strings. Why?
I found this but didn't help: How do I compare strings in Java?
I tried two ways:
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
String code;
if(Objects.equals((code = in.readLine()), "S")) { //Input string: "S"
//code
}
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
String code;
if((code = in.readLine()).equals("S")) { //Input string: "S"
//code
}
The code does not run in either case because the value of the test is false.
Full code
Server side - C# (Windows)
class ManagePhoneClients
{
public void managePhoneClients(object obj)
{
Boolean socketalive = true;
TcpClient tcpClient = (TcpClient)obj;
StreamReader sr = new StreamReader(tcpClient.GetStream(), Encoding.UTF8);
StreamWriter sw = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
Boolean isPhoneClientConnected = false;
String user;
String answer;
String tl;
List<string> LC = new List<string>();
Boolean qss = false;
Program program = new Program();
Int32 points = 0;
ConsoleMethods.writeLine("Thread started for the phone client.", "Info", ConsoleColor.Cyan);
sw.WriteLine("S");
sw.Flush();
while (socketalive == true)
{
try
{
if (Program.isMainClientConnected != true || Program.isPowerPointConnected != true)
{
ConsoleMethods.writeLine("Connection refused because the necessary clients are not connected!", "Error", ConsoleColor.Red);
sw.WriteLine("NS");
sw.Flush();
tcpClient.Close();
socketalive = false;
}
else
{
sw.WriteLine("LC");
sw.Flush();
}
if (isPhoneClientConnected != true & sr.Peek() != -1)
{
String rLC = sr.ReadLine();
LC.AddRange(rLC.Split('|'));
if (LC[1].ToString() == Program.passPhoneClient)
{
user = LC[0];
Program.userNames.Add(user);
ConsoleMethods.writeLine("Phone connected from: " + tcpClient.Client.RemoteEndPoint, "Info", ConsoleColor.Cyan);
sw.WriteLine("S");
sw.Flush();
Program.utnr = rLC;
isPhoneClientConnected = true;
}
else
{
sw.WriteLine("NS");
sw.Flush();
socketalive = false;
ConsoleMethods.writeLine("Phone client disconnected because the password was invalid!", "Error", ConsoleColor.Red);
}
}
switch (sr.ReadLine())
{
case "CLIENT-EXCEPTION":
ConsoleMethods.writeLine("Exception in phone client from: " + tcpClient.Client.RemoteEndPoint + "\n" + sr.ReadLine(), "Client-Error", ConsoleColor.DarkRed);
break;
case "RECEIVED_POINTS":
int point = int.Parse(sr.ReadLine());
points += point;
ConsoleMethods.writeLine("Phone client succesfully completed a task from: " + tcpClient.Client.RemoteEndPoint + " Point: " + point, "Client-Received Points", ConsoleColor.DarkRed);
ConsoleMethods.writeLine("Phone client collected points from: " + tcpClient.Client.RemoteEndPoint + " Points: " + points, "Client-Collected Points", ConsoleColor.DarkRed);
break;
}
}
catch (Exception e)
{
tcpClient.Close();
socketalive = false;
ConsoleMethods.writeLine(e.Message + e.StackTrace + e.StackTrace, "Error", ConsoleColor.Red);
}
}
}
}
(This is not yet complete!)
Client side - Java (Android)
public void login(View v) {
final Context context = this;
new Thread(new Runnable() {
public void run() {
try {
final Socket socket = new Socket("192.168.0.104", 90);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print("P" + "\r\n");
out.flush();
String code;
code = in.readLine();
if(code.equals("S")) {
if (Objects.equals((code = in.readLine()), "LC")) {
out.print(((EditText)findViewById(R.id.username)).getText().toString() + "|" + ((EditText)findViewById(R.id.password)).getText().toString() + "\r\n");
out.flush();
if(Objects.equals((code = in.readLine()), "S")) {
new ServerContact(context).Listener(socket);
startActivity(new Intent(Login.this, Waiting.class));
} else {
throw new Exception("Login failed because the server refused the login request. Server responded with status code: '" + code + "'.");
}
} else {
throw new Exception("Login failed because the server refused the login request. Server responded with status code: '" + code + "'.");
}
} else {
throw new Exception("Login failed because the server refused the login request. Server responded with status code: '" + code + "'.");
}
} catch (Exception e) {
new ExceptionWriter(e);
}
}
}).start();
}
(This is not yet complete!)
I managed to solve it. On the server side I have to disable the BOM.
No BOM:
StreamWriter sw = new StreamWriter(tcpClient.GetStream(), new UTF8Encoding(false));
With BOM:
StreamWriter sw = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
It works for me (using your first case). I think we've all concluded that code is not in fact equal to "S", sorry about that.
public class EqualsTest {
public static void main( String[] args ) throws IOException {
MyStream socket = new MyStream( new ByteArrayInputStream( "S\n".getBytes() ));
BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream(), "UTF8" ) );
String code;
if( Objects.equals( (code = in.readLine()), "S" ) ) {
System.out.println( "true" );
} else {
System.out.println( "false" );
}
}
static class MyStream {
private final InputStream ins;
public MyStream( InputStream ins ) {
this.ins = ins;
}
public InputStream getInputStream() {
return ins;
}
}
}
Output:
run:
true
BUILD SUCCESSFUL (total time: 0 seconds)
I'll add some ideas for testing code for debugging:
// how to debug
System.err.println( "code="+code+" length="+code.length() );
System.err.println( "code bytes="+Arrays.toString( code.getBytes() ) );
Firstly, let me say I not a java programmer, I am a programmer on the IBM Iseries. However, I've been tasked with changing a current java application that currently sends a stream of data to one URL that will allow that same stream of data to be sent to multiple URLs based on a properties file. Our java app runs on the Iseries and we are using the org.apache.commons.httpclient.HttpClient class to send the data and the response is processed. Everything works great right now, but I wanted to see if anyone could point me in the right direction to complete this task.
Essentially, I need to send the same block of data to multiple URLs within the same thread or instance. I'm not sure if its possible or the best way to try to complete this. So, is there a way to create multiple instances within the same thread that will send the same data stream to multiple URLs? Before you start commenting I will say again that I am not a java programmer and I wasn't even sure how to phrase the question.
Added code sample:
public class Replication_CC implements TextProcessor {
public static String VERSION = "v2014.1.0";
static Logger log = Logger.getLogger(Replication_CC.class);
String url;
int retries = 1;
public Replication_CC(Properties p) {
super();
url = p.getProperty("url");
log.info("Service URL set to " + url);
retries = PropertiesUtil.getOptionalIntProperty(p, "retries", 1);
log.info("Retries set to " + retries);
}
public static void main(String[] args) throws Exception {
log.info("Replication " + VERSION);
log.info("Initializing...");
Properties p = PropertiesUtil.loadProperties(Replication_CC.class.getResource("/Replication_CC.properties"));
DQServer server = new DQServer(p, new Replication_CC(p));
server.run();
}
public String process(String request) throws Exception {
long processStart = System.currentTimeMillis();
String response = null;
for (int i=0; i<=retries; i++) {
try {
response = send(request, url);
if (response!=null) break;
}
catch (Exception e) {
log.warn("Error processing: " + e.getMessage());
if (i<retries) {
log.warn("Trying again (retry " + (i+1) + "...");
}
else {
log.error("Giving up on this transaction.");
break;
}
}
}
long processFinished = System.currentTimeMillis();
log.info("Request was processed in " + (processFinished-processStart) + "ms.");
return response;
}
public String send(String request, String url) throws Exception {
log.debug("Creating request...");
HttpClientParams params = new HttpClientParams();
params.setParameter("http.useragent", "http-api / Replication");
HttpClient client = new HttpClient(params);
PostMethod post = new PostMethod(url);
/*
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (String key : globalRequest.keySet()) {
params.add(nvp(key, globalRequest.get(key)));
}
*/
post.setRequestBody(request);
// Log the request
if (log.isDebugEnabled()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
post.getRequestEntity().writeRequest(baos);
baos.close();
log.debug("HTTP Request: \n" + StringUtils.repeat("*", 100) + "\n" + "Content Type: "
+ post.getRequestEntity().getContentType() + "\n" + "Content Length: "
+ post.getRequestEntity().getContentLength() + "\n" + "Request Headers: "
+ ArrayUtils.toString(post.getRequestHeaders()) + "\n" + "Request Params: " + baos.toString() + "\n" +
StringUtils.repeat("*", 100));
}
try {
log.info("Sending request...");
int responseCode = client.executeMethod(post);
//log.debug(String.format("Http Response Code [%s]", responseCode));
log.debug("Http Response Code [" + responseCode + "]");
if (responseCode == HttpStatus.SC_OK) {
String charset = post.getResponseCharSet();
log.debug("Response Character Set [" + charset + "]");
/*
byte[] body = post.getResponseBody();
String response = new String(body, charset);
*/
String response = IOUtils.toString(post.getResponseBodyAsStream());
log.debug("Response Body: \n" + response);
return response;
}
else {
throw new Exception(post.getStatusLine().toString());
}
}
catch (IOException ioe) {
log.error(ioe);
throw ioe;
}
finally {
post.releaseConnection();
}
}
One simple way is to include multiple URL's in the existing url property separated by a unique character. I chose "|" (pipe) in this example because it's highly unlikely to see a pipe in a normal url.
Java identifies methods by name and parameter signature. We can use that to our advantage by adding a String url parameter to the existing process method and creating a new process(String request) method that will split and iterate over the url's. The only downside is that it will only return the last response to the DQServer class.
public String process(String request) throws Exception {
String response;
for (String u : url.split("\\|")) {
response = process(request, u);
}
return response;
}
public String process(String request, String url) throws Exception {
long processStart = System.currentTimeMillis();
String response = null;
for (int i=0; i<=retries; i++) {
try {
response = send(request, url);
if (response!=null) break;
}
catch (Exception e) {
log.warn("Error processing: " + e.getMessage());
if (i<retries) {
log.warn("Trying again (retry " + (i+1) + "...");
}
else {
log.error("Giving up on this transaction.");
break;
}
}
}
long processFinished = System.currentTimeMillis();
log.info("Request was processed in " + (processFinished-processStart) + "ms.");
return response;
}
The complete sample is available on GitHub Gist.
I try to connect to a custom Bluetooth device using BlueCove. I can pair the device, but when I try to search for services I always get SERVICE_SEARCH_DEVICE_NOT_REACHABLE in serviceSearchCompleted() and no services are discovered. If I try the same thing outside Java (in Windows), the PC bluetooth device discovers and can connect (using COM21, COM22, ...) to the SPP service on my device.
What am I doing wrong?
I also tried to do the service search after the device discovery is ended. Same issue.
Please find below my code.
Many thanks in advance for any idea on how to solve this,
Adrian.
public class Test {
private static Logger LOG = Logger.getLogger(Test.class.getName());
private static final String NAME = "XXXX";
private static final String PIN = "1234";
private static final UUID[] UUIDS = new UUID[] {new UUID(0x0003), new UUID(0x1101)};
private LocalDevice localDevice;
private DiscoveryAgent discoveryAgent;
private DiscoveryListener discoveryListener = new GDiscoveryListener();
private Map<Integer, RemoteDevice> searchForServices = new HashMap<Integer, RemoteDevice>();
private Collection<ServiceRecord> servicesDiscovered = new HashSet<ServiceRecord>();
private Object lock = new Object();
private CountDownLatch waitForDevices;
protected void connect() {
try {
localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
LOG.info("Local Device: " + localDevice.getFriendlyName()
+ "(" + localDevice.getBluetoothAddress() + ")");
discoveryAgent = localDevice.getDiscoveryAgent();
LOG.finest("Start discovering devices");
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, discoveryListener);
try {
synchronized(lock) {
lock.wait();
}
if (searchForServices.size() > 0) {
waitForDevices = new CountDownLatch(searchForServices.size());
waitForDevices.await();
}
}
catch (InterruptedException e) {
LOG.log(Level.WARNING, "Error waiting to terminate discovery", e);
}
LOG.finest(servicesDiscovered.size() + " services discovered");
LOG.finest("Device discovery completed");
} catch (BluetoothStateException e) {
LOG.log(Level.WARNING, "Error initializing Bluetooth", e);
}
}
private class GDiscoveryListener implements DiscoveryListener {
public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
try {
String name = rd.getFriendlyName(false);
boolean isMine = NAME.equals(name);
LOG.info("Discovered: " + name + "(" + rd.getBluetoothAddress() + ")"
+ (isMine ? "" : " - ignoring"));
if (!isMine)
return;
if (!rd.isAuthenticated()) {
LOG.finest("Try to pair with " + name
+ " PIN: " + PIN);
boolean paired = RemoteDeviceHelper.authenticate(rd, PIN);
LOG.info("Pair with " + name + (paired ? " succesfull" : " failed"));
}
int transID = discoveryAgent.searchServices(null, UUIDS, rd, discoveryListener);
searchForServices.put(transID, rd);
LOG.finest("Searching for services for " + name + " with transaction " + transID);
} catch (BluetoothStateException e) {
LOG.log(Level.WARNING, "Cannot search services for "
+ rd.getBluetoothAddress(), e);
} catch (IOException e) {
LOG.log(Level.WARNING, "Error connecting ", e);
} catch (Throwable t) {
LOG.log(Level.WARNING, "Cannot search services for "
+ rd.getBluetoothAddress(), t);
}
}
public void inquiryCompleted(int respCode) {
synchronized(lock) {
lock.notify();
}
switch (respCode) {
case DiscoveryListener.INQUIRY_COMPLETED :
LOG.fine("INQUIRY_COMPLETED");
break;
case DiscoveryListener.INQUIRY_TERMINATED :
LOG.fine("INQUIRY_TERMINATED");
break;
case DiscoveryListener.INQUIRY_ERROR :
LOG.fine("INQUIRY_ERROR");
break;
default :
LOG.fine("Unknown Response Code - " + respCode);
break;
}
}
public void serviceSearchCompleted(int transID, int respCode) {
String rd = searchForServices.get(transID).getBluetoothAddress();
//searchForServices.remove(transID);
switch (respCode) {
case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
LOG.fine(rd + ": The service search completed normally");
break;
case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
LOG.fine(rd + ": The service search request was cancelled by a call to DiscoveryAgent.cancelServiceSearch(int)");
break;
case DiscoveryListener.SERVICE_SEARCH_ERROR:
LOG.warning(rd + ": An error occurred while processing the request");
break;
case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
LOG.info(rd + ": No records were found during the service search");
break;
case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
LOG.warning(rd + ": The device specified in the search request could not be reached or the local device could not establish a connection to the remote device");
break;
default:
LOG.warning(rd + ": Unknown Response Code - " + respCode);
break;
}
if (waitForDevices != null)
waitForDevices.countDown();
}
public void servicesDiscovered(int transID, ServiceRecord[] srs) {
LOG.info("Services discovered in transaction " + transID + " : " + srs.length);
for (ServiceRecord sr : srs) {
LOG.info(sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false));
servicesDiscovered.add(sr);
}
}
}
public static void main(String[] args) {
new Test().connect();
}
}
I had the same problem while connecting to a Bluetooth earpiece. Like you I was also searching for more than one service at a time and It always returned SERVICE_SEARCH_DEVICE_NOT_REACHABLE. So, I tried searching for only one service and it worked. So, try modifying your code as:
...
private static final UUID[] UUIDS = new UUID[] {new UUID(0x0003)}