×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    Error while transferring files over bluetooth

    Currently am into transferring files between devices in J2ME (Nokia S40 devices are being the target) . am having nullpointer exception at connection = Connection.open(url);

    The url I get is
    "btgoep://9814584D8994:9;authenticate=true;encrypt=false;master=false"
    I don't know why it returns Nullpointer exception when the there is a valid url. Does anyone have any idea about this..??

    This is my code

    Code:
    public class Share implements DiscoveryListener,BluetoothDeviceList.BluetoothListner
    {
    
    private LocalDevice ldev;
    private DiscoveryAgent disagent; 
    private Vector devvec;  
    
    private String url = null;
    private Vector serviceFound = new Vector();
    private String type = null;
    private String filename = null;
    public static String filepath = null;
    private int FFormat = -1;
    private String textData = null;
        Display display;
    
    public Share(int FFormat, Display view, String textContent) 
    {
      
        this.FFormat = FFormat;
        ldev = null;
        disagent = null;
                display = view;
                textData = textContent;
        devvec = new Vector();
                searchDevices();
    }
    
    public boolean isBluetoothAvaliable(String filepath)
    {
        switch (FFormat) {
        case 0:
            System.out.println("file path is "+filepath);
            Share.filepath = filepath;
            filename = filepath.substring(filepath.lastIndexOf('/')+1, filepath.length());
            System.out.println("file name is "+filename);
            type = filename.substring(filename.indexOf('.')+1, filename.length());//filepath.lastIndexOf('.'+1)
           
            break;
        case 1:
            filename = "note.txt";     
            break;
    
        default:
            break;
        }
    
    
                return false; 
    
    
    }
    
    private void searchDevices() 
    {
        try
        {
          
            ldev = LocalDevice.getLocalDevice();
           
            disagent = ldev.getDiscoveryAgent();
           
            disagent.startInquiry(DiscoveryAgent.GIAC, this);
    
        }
        catch (BluetoothStateException e)
        {
            System.out.println("Bluetooth.searchDevices() State excption "+e.toString());       
        }
    
    }
    
    /*Discovery Listners */
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) 
    {
            devvec.addElement(btDevice);
    
      }
    public void inquiryCompleted(int discType) 
    {       
    
        serviceFound.removeAllElements();
       
        switch (discType) {
        case INQUIRY_COMPLETED:
            disagent.cancelInquiry(this);
            System.out.println("Bluetooth.inquiryCompleted()");
            try 
            {
                                    
                           BluetoothDeviceList list = new BluetoothDeviceList(display, devvec);
                           list.setListner(this);
    
            } catch (Exception e) 
            {
                System.out.println("Bluetooth.inquiryCompleted() Exception "+e.toString());
                devvec.removeAllElements();
                disagent = null;
                searchDevices();                          
    
            }
    
            break;
        case INQUIRY_ERROR:
           
    
                    disagent.cancelInquiry(this);
            System.out.println("Enquiry error");
                      
            break;
        case INQUIRY_TERMINATED:
         
            disagent.cancelInquiry(this);
            System.out.println("Enquiry terminated");
                       
            break;
    
        default:
            break;
        }
    
    
    
    }
    
    
    public void serviceSearchCompleted(int transID, int respCode) 
    {
        switch (respCode) {
        case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
            BluetoothServiceThread bluetoothServiceThread = new BluetoothServiceThread();
            bluetoothServiceThread.start();
            break;
        case DiscoveryListener.SERVICE_SEARCH_ERROR:
            break;
        case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
            break;
        case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
           
            break;
        case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
            break;
    
        default:
            break;
        }
    
    }
    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) 
    {
            for(int i = 0;i<servRecord.length;i++)
            {
                try {
                    url = servRecord[i].getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, false);              
                }
                catch (Exception e) {
                    System.out.println("exception is ");
                }
            }            
    
    
    }
    
    public void selectedDevice(RemoteDevice remoteDevice) 
    {   
    
            try {       
    
                UUID[] uuidset = {new UUID(0x1105)};
                int[] attrset = {0x0100, 0x0003, 0x0004};
                disagent.searchServices(null, new UUID[]{new UUID(0x1105)}, remoteDevice, this);
            } catch (Exception e) 
            {
                System.out.println("Bluetooth.deviceDiscovered() Exception "+e.toString());
               
            }
    
    }
    public class BluetoothServiceThread extends Thread
    {
                int i =0;
        public void run() 
        {
    
                    Connection connection = null;
                    OutputStream outputStream = null;
                    Operation putOperation = null;
                    ClientSession cs = null;
    
                    try 
                    {
    
                        try
                        {
    
                            System.out.println("Connect -1" +url);
                            connection =  Connector.open(url);
                            i = 1;
                            System.out.println("Connect 0 ");
    
                            cs =  (ClientSession)connection;
                            i = 2;
                            System.out.println("Connect 1");
                            HeaderSet set = cs.createHeaderSet();
                            cs.connect(set);
                            i = 3;
                            System.out.println("Connect 2 Respose codes "+set.getResponseCode() );
                            final byte filebytes[];
                            if (FFormat == 0) 
                                    filebytes = readBytesFromFile();
                            else
                                    filebytes = textData.getBytes();
                            i = 4;
                            final HeaderSet hs = cs.createHeaderSet();
                            hs.setHeader(HeaderSet.NAME, filename);                            
                            i = 5;
                            System.out.println("Connect 3");
                            hs.setHeader(HeaderSet.LENGTH, new Long(filebytes.length));
                            i = 6;
                            System.out.println("Connect 4 header length "+HeaderSet.LENGTH);
                            putOperation = cs.put(hs);
                            i = 7;
                            System.out.println("Connect 5");
                            outputStream = putOperation.openOutputStream();
                            i = 8;
                            System.out.println("Connect 6");
                            outputStream.write(filebytes);
                            i = 9;
                            System.out.println("Connect 7");
    
    
                        }
                        finally
                        {
                            outputStream.close();
                            putOperation.close();
                            cs.disconnect(null);
                            connection.close();
    
                        }
                    }
                    catch (Exception e) 
                    {
    
                        Alert alert2 = new Alert("BluetoothServiceThread " );
                        alert2.setString("BluetoothServiceThread i is "+ i + "    Exception " + e.toString() + "   url is   " + url);
                        display.setCurrent(alert2);
                    }           
    
        }
        private byte[] readBytesFromFile()
        {
    
                    try 
                    {
                        InputStream is = null ;
                        FileConnection fileConnection = null;
                        try
                        {
                            fileConnection = (FileConnection) Connector.open(filepath,Connector.READ_WRITE);
                            int size = (int)fileConnection.fileSize();
                            is = fileConnection.openInputStream();
                            byte[] streambytes = new byte[size];
                            is.read(streambytes, 0, size);
                            is.close();
                            fileConnection.close(); 
                            return streambytes;
                        }
                        finally
                        {
                            is.close();
                            fileConnection.close();
                        }
                    }
                    catch (Exception e) 
                    {
                        System.out.println("Bluetooth.BluetoothServiceThread.readBytesFromFile() Exception "+e.toString());
                        return null;
                    }
    
    
        }       
    }
    
    }

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,567

    Re: Error while transferring files over bluetooth

    Just to make sure: you see a "Connect -1btgoep://9814584D8994:9;authenticate=true;encrypt=false;master=false", but do not see "Connect 0" line?

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    Re: Error while transferring files over bluetooth

    yes that's what happening.. do you have any idea about this... still am getting null pointer exception..

  4. #4
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,104

    Re: Error while transferring files over bluetooth

    When you got the URL you are past service discovery (SDP). Is the OBEX package available on your device model: Do me a favour and change the URL to start with btspp. Does that avoid your NullPointerException?

Similar Threads

  1. Transferring files using Bluetooth
    By Banupriya in forum Bluetooth Technology
    Replies: 5
    Last Post: 2009-10-13, 14:40
  2. Transferring large files via bluetooth
    By jharker1987 in forum Mobile Java General
    Replies: 0
    Last Post: 2009-06-24, 11:44
  3. Transferring Files through Bluetooth?
    By sunil_bhambani123 in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2009-03-01, 02:44
  4. transferring jar files w/ bluetooth from 6630
    By petschy in forum Mobile Java Tools & SDKs
    Replies: 8
    Last Post: 2007-08-30, 10:28
  5. Transferring files to mac
    By rajasg in forum Symbian User Interface
    Replies: 1
    Last Post: 2004-01-20, 08:48

Posting Permissions

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