Search code examples
androidvb.netusbadb

android: transfer sqlite database from pc to device via usb programatically


The situation is this. I have an application written in vb.net. it consists or two parts. One on a PC and the other on a handheld windows mobile 6 device . The desktop program transfers a SQLServer compact database to and from the handheld device using activesync via USB. Potentially we want to look into having android handheld devices also supported by this application. Now I know I can use SQLite with .net. I know I can use ADB to push and pull data (and therefore the database files) to and from the device.

What I need to know is can I access ADB directly from VB.NET as an API or SDK rather than having to do it manually.

Unless of course I'm missing something and I can copy databases to and from the device in some other way?

thanks in anticipation


Solution

  • There's no direct API for that. We have a similar scenario, where the user syncs content (i.e. also the database) from a desktop client (in our case Java Swing based), which utilized adb manually and it's working fine so far.

    In our java client, we'd call:

    private static final String ADB_PUSH = "\"" + Utility.getWorkDir() + File.separator + "adb\" -s %s push \"%s\" %s";
    
    
    /**
     * Pushes a file to a connected device via ADB
     * @param deviceId Device serial number
     * @param from Path of source file (on PC)
     * @param to Path of file destination (on device)
     */
    public static void push(String deviceId, String from, String to) {
        try {
            String cmd = String.format(ADB_PUSH, deviceId, from, to);
            System.out.println("adb push: " + cmd);
            Process p = Runtime.getRuntime().exec(cmd);
            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
    
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println("adb: " + line);
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }