Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.util.Vector;
- import javax.bluetooth.*;
- import javax.microedition.io.Connector;
- import javax.microedition.io.StreamConnection;
- import javax.microedition.lcdui.*;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- public class client extends MIDlet implements DiscoveryListener, CommandListener {
- Command sendCmd;
- Vector services = new Vector();
- Vector devices = new Vector();
- LocalDevice localdrive;
- DiscoveryAgent agent;
- String myServiceUUID = "2d26618601fb47c28d9f10b8ec891363";
- UUID MYSERVICEUUID_UUID = new UUID(myServiceUUID, false);
- UUID[] uuids = {MYSERVICEUUID_UUID};
- String connurl;
- StreamConnection SC;
- ServiceRecord service;
- String url;
- StreamConnection conn;
- DataOutputStream output;
- DataInputStream input;
- protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
- }
- protected void pauseApp() {
- }
- protected void startApp() throws MIDletStateChangeException {
- Form f = new Form("Client");
- sendCmd = new Command("Send", Command.OK, 0);
- f.addCommand(sendCmd);
- f.setCommandListener(this);
- Display d = Display.getDisplay(this);
- d.setCurrent(f);
- try {
- localdrive = LocalDevice.getLocalDevice();
- agent = localdrive.getDiscoveryAgent();
- agent.startInquiry(DiscoveryAgent.GIAC, this);
- } catch (BluetoothStateException ex) {
- ex.printStackTrace();
- }
- }
- public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
- devices.addElement(btDevice);
- }
- public void inquiryCompleted(int discType) {
- try {
- RemoteDevice remote = (RemoteDevice) devices.elementAt(0);
- agent.searchServices(null, uuids, remote, this);
- } catch (BluetoothStateException ex) {
- ex.printStackTrace();
- }
- }
- public void serviceSearchCompleted(int transID, int respCode) {
- switch (respCode) {
- case SERVICE_SEARCH_COMPLETED:
- createConnection();
- break;
- case SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
- System.out.println("service not reachable");
- break;
- }
- }
- public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
- service = servRecord[0];
- }
- private void createConnection() {
- try {
- url = service.getConnectionURL(
- ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
- conn = (StreamConnection) Connector.open(url);
- output = conn.openDataOutputStream();
- input = conn.openDataInputStream();
- } catch (Exception e) {
- }
- }
- public void commandAction(Command c, Displayable d) {
- if (c == sendCmd) {
- try {
- output.writeUTF("Hey server");
- String msg = input.readUTF();
- System.out.println(msg);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement