Results 1 to 2 of 2
  1. #1
    Paul D is offline Member
    Join Date
    Nov 2015
    Posts
    2
    Rep Power
    0

    Default Java / Android Bluetooth help

    Hello, I am trying to communicate with my pc and android tablet with blue-tooth. I am using eclispe as my ide to write my java files. I have added the bluecove jar file as a referenced library. I am using the code from this this webpage Overview (bluecove 2.1.0 API)

    When I run the ObexPutClient class I receive and can open the Hello.txt file on my android. What I would like to do is connect my android tablet and connect with blueterm2 and receive a string instead of a txt file.

    When I run the PutServer class on my pc and I open BlueTerm on my android tablet and connect to my pc. I can see the connection open but not any text i type in blueterm.

    Any help would be greatly appreciated. By directional bluetooth communication between a raspberry-pi using java and an android smartphone running an app is my ultimate end goal.

    Java Code:
    import java.io.IOException;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.obex.*;
    
    public class ObexPutClient {
    
        public static void main(String[] args) throws IOException, InterruptedException {
    
            String serverURL = null; // = "btgoep://0019639C4007:6";
            if ((args != null) && (args.length > 0)) {
                serverURL = args[0];
            }
            if (serverURL == null) {
                String[] searchArgs = null;
                // Connect to OBEXPutServer from examples
                // searchArgs = new String[] { "11111111111111111111111111111123" };
                ServicesSearch.main(searchArgs);
                if (ServicesSearch.serviceFound.size() == 0) {
                    System.out.println("OBEX service not found");
                    return;
                }
                // Select the first service found
                serverURL = (String)ServicesSearch.serviceFound.elementAt(0);
            }
    
            System.out.println("Connecting to " + serverURL);
    
            ClientSession clientSession = (ClientSession) Connector.open(serverURL);
            HeaderSet hsConnectReply = clientSession.connect(null);
            if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
                System.out.println("Failed to connect");
                return;
            }
    
            HeaderSet hsOperation = clientSession.createHeaderSet();
            hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
            hsOperation.setHeader(HeaderSet.TYPE, "text");
    
            //Create PUT Operation
            Operation putOperation = clientSession.put(hsOperation);
    
            // Send some text to server
            byte data[] = "Hello world!".getBytes("iso-8859-1");
            OutputStream os = putOperation.openOutputStream();
            os.write(data);
            os.close();
    
            putOperation.close();
    
            clientSession.disconnect(null);
    
            clientSession.close();
        }
    }
    
    
    
    OBEX Put Server
    This class will start an OBEX service that will accept Put commands and print it to standard out.
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.bluetooth.*;
    import javax.microedition.io.Connector;
    import javax.obex.*;
    
    public class OBEXPutServer {
    
     static final String serverUUID = "11111111111111111111111111111123";
    
     public static void main(String[] args) throws IOException {
    
     LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
    
     SessionNotifier serverConnection = (SessionNotifier) Connector.open("btgoep://localhost:"
     + serverUUID + ";name=ObexExample");
    
     int count = 0;
     while(count < 2) {
     RequestHandler handler = new RequestHandler();
     serverConnection.acceptAndOpen(handler);
     System.out.println("Received OBEX connection " + (++count));
     }
     }
    
     private static class RequestHandler extends ServerRequestHandler {
    
     public int onPut(Operation op) {
     try {
     HeaderSet hs = op.getReceivedHeaders();
     String name = (String) hs.getHeader(HeaderSet.NAME);
     if (name != null) {
     System.out.println("put name:" + name);
     }
    
     InputStream is = op.openInputStream();
    
     StringBuffer buf = new StringBuffer();
     int data;
     while ((data = is.read()) != -1) {
     buf.append((char) data);
     }
    
     System.out.println("got:" + buf.toString());
    
     op.close();
     return ResponseCodes.OBEX_HTTP_OK;
     } catch (IOException e) {
     e.printStackTrace();
     return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
     }
     }
     }
    }

  2. #2
    Paul D is offline Member
    Join Date
    Nov 2015
    Posts
    2
    Rep Power
    0

    Default Re: Java / Android Bluetooth help

    Here is the solution, I am now sending strings over bluetooth from my pc to andriod phone and back. NICE!
    Java Code:
    public static void main(String[] args) {
                    
            try {
    			startServer();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
              }
        
        private static void startServer() throws IOException{
    
            UUID uuid = new UUID("1101", true);
            String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
            StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
            System.out.println("\nServer Started. Waiting for client to connect...");
            
            StreamConnection connection=streamConnNotifier.acceptAndOpen();
            
            InputStream inStream = connection.openInputStream();
            OutputStream outStream = connection.openOutputStream();
            PrintWriter pWriter = new PrintWriter(new OutputStreamWriter(
    				outStream));
    		BufferedReader bReader = new BufferedReader(new InputStreamReader(
    				inStream));
            
    		int x = 0;
    		
    		while (x == 0) {
    			String lineRead = bReader.readLine();
    			System.out.println(lineRead);
    			pWriter.write("Response String back to ANDROID PHONE " + lineRead
    					+ "\r\n");
    			pWriter.flush();
    			if (lineRead.trim().toLowerCase().equals("quit"))
    			{
    				x++;
    			}
    		}
    		pWriter.close();
    		streamConnNotifier.close();
        }
    }

Similar Threads

  1. Need Android Java programmer with bluetooth experience
    By highupstudio in forum Jobs Offered
    Replies: 0
    Last Post: 12-09-2013, 06:26 PM
  2. Bluetooth Obex Android Java
    By jb66 in forum Android
    Replies: 0
    Last Post: 06-21-2013, 07:52 PM
  3. Java Bluetooth Application Help
    By TheITRanger in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2011, 10:20 AM
  4. java code for bluetooth.
    By rainfan in forum Java Applets
    Replies: 4
    Last Post: 01-13-2010, 05:11 AM
  5. Replies: 0
    Last Post: 02-26-2009, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •