在Handheld

时间:2016-01-26 11:44:59

标签: wear-os android-wear-data-api

我将文件从磨损发送到手持设备。我发送了4个或5个文件(其中2个更大),但每次手持端只收到一个较大的文件...

磨损:

public void sendFile(String filename){
    //BLE files
    fileToArray("BLEdata/" + filename + "_RightDevice.txt", "/BLEdata/");
    //send log file
    fileToArray(filename + "_LOG.txt", "/LOGdata/");
    //send GPS file
    fileToArray("GPSdata/" + filename + "_GPS.txt", "/GPSdata/");
    //send Report files
    fileToArray("Report/" + filename + "_Report.txt", "/REPORTdata/");
    //BLE files
    fileToArray("BLEdata/" + filename + "_LeftDevice.txt", "/BLEdata/");
}

public void fileToArray(String filename, String path)
{
    FileInputStream fileInputStream = null;

    File file = new File(Environment.getExternalStorageDirectory() + "/TuneWear/"  + filename);

    System.out.println("PATH: " + file.getPath());

    if(file.exists()){
        byte[] bFile = new byte[(int) file.length()];

        try {
            //convert file into array of bytes
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();

            WearResultsActivity main = (WearResultsActivity) getActivity();
            long time = main.getInitTime();

            if(filename.contains("_RightDevice")){
                new SendToDataLayerThread(path + time, bFile, 2).start();
            }else if (filename.contains("_LeftDevice")){
                new SendToDataLayerThread(path + time, bFile, 1).start();
            } else
                new SendToDataLayerThread(path + time, bFile).start();
        }catch(Exception e){
            e.printStackTrace();
        }
    } else System.out.println("Doesn't exist:\n" + file.getPath());
}

class SendToDataLayerThread extends Thread {
    String path;
    byte[] bFile;
    int footSide;

    // Constructor for sending data objects to the data layer
    SendToDataLayerThread(String p, byte[] bytes, int footside) {
        path = p;
        bFile = bytes;
        footSide = footside;
    }

    SendToDataLayerThread(String p, byte[] bytes) {
        path = p;
        bFile = bytes;
    }

    public void run() {
        WearResultsActivity main = (WearResultsActivity) getActivity();
        GoogleApiClient googleClient = main.getGoogleClient();
        Asset asset = Asset.createFromBytes(bFile);

        System.out.println(asset.toString());

        PutDataMapRequest dataMap = PutDataMapRequest.create(path);
        dataMap.getDataMap().putLong("timestamp", Calendar.getInstance().getTimeInMillis());
        dataMap.getDataMap().putLong("/InitialTime", ((WearResultsActivity) getActivity()).getInitTime());
        dataMap.getDataMap().putAsset("asset", asset);
        if(footSide == 1) {
            dataMap.getDataMap().putInt("footside", footSide);
            System.out.println("DATAMAP COM LEFT " + footSide);
        }else if (footSide == 2) {
            dataMap.getDataMap().putInt("footside", footSide);
            System.out.println("DATAMAP COM RIGHT " + footSide);
        }
        PutDataRequest request = dataMap.asPutDataRequest();
        PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(googleClient, request);
        pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
            @Override
            public void onResult(DataApi.DataItemResult dataItemResult) {
                System.out.println("RESULT");
            }
        });
        //Wearable.DataApi.putDataItem(googleClient, request);
    }
}

手持:

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    System.out.println("COUNT " + dataEvents.getCount());
    for (DataEvent event : dataEvents) {
        DataItem item = event.getDataItem();
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            // DataItem changed
            if (item.getUri().getPath().contains(LOGdata)) {
                final DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
                Asset asset = dataMapItem.getDataMap().getAsset("asset");

                //TODO here
                Wearable.DataApi.getFdForAsset(googleClient, asset).setResultCallback(
                        new ResultCallback<DataApi.GetFdForAssetResult>() {
                            @Override
                            public void onResult(DataApi.GetFdForAssetResult getFdForAssetResult) {
                                InputStream assetInputStream = getFdForAssetResult.getInputStream();

                                long initialTime = dataMapItem.getDataMap().getLong(KEY_INITIALTIME);
                                String fileName = new SimpleDateFormat("HH'h'mm'm'ss's'_dd-MM-yyyy").format(initialTime);
                                String dataPath = Environment.getExternalStorageDirectory().toString() + "/TuneWear/";

                                File myDir = new File(dataPath);
                                myDir.mkdirs();

                                File file = new File(myDir, "Run_" + fileName + "_LOG.txt");
                                System.out.println("FILE: " + file.getPath());
                                try {
                                    FileOutputStream fOut = new FileOutputStream(file);
                                    int nRead;
                                    byte[] data = new byte[16384];
                                    while ((nRead = assetInputStream.read(data, 0, data.length)) != -1) {
                                        fOut.write(data, 0, nRead);
                                    }

                                    fOut.flush();
                                    fOut.close();
                                } catch (IOException e) {
                                    System.out.println("ERROR File write failed: " + e.toString());
                                }
                            }
                        }
                );


            } else if (item.getUri().getPath().contains(GPSdata)) {
                final DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
                Asset asset = dataMapItem.getDataMap().getAsset("asset");

                //TODO here
                Wearable.DataApi.getFdForAsset(googleClient, asset).setResultCallback(
                        new ResultCallback<DataApi.GetFdForAssetResult>() {
                            @Override
                            public void onResult(DataApi.GetFdForAssetResult getFdForAssetResult) {
                                InputStream assetInputStream = getFdForAssetResult.getInputStream();

                                long initialTime = dataMapItem.getDataMap().getLong(KEY_INITIALTIME);
                                String fileName = new SimpleDateFormat("HH'h'mm'm'ss's'_dd-MM-yyyy").format(initialTime);
                                String dataPath = Environment.getExternalStorageDirectory().toString() + "/TuneWear/GPSdata/";

                                File myDir = new File(dataPath);
                                myDir.mkdirs();

                                File file = new File(myDir, "Run_" + fileName + "_GPS.txt");
                                System.out.println("FILE: " + file.getPath());
                                try {
                                    FileOutputStream fOut = new FileOutputStream(file);
                                    int nRead;
                                    byte[] data = new byte[16384];
                                    while ((nRead = assetInputStream.read(data, 0, data.length)) != -1) {
                                        fOut.write(data, 0, nRead);
                                    }

                                    fOut.flush();
                                    fOut.close();
                                } catch (IOException e) {
                                    System.out.println("ERROR File write failed: " + e.toString());
                                }
                            }
                        }
                );


            } else if (item.getUri().getPath().contains(REPORTdata)) {
                final DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
                Asset asset = dataMapItem.getDataMap().getAsset("asset");


                //TODO here
                Wearable.DataApi.getFdForAsset(googleClient, asset).setResultCallback(
                        new ResultCallback<DataApi.GetFdForAssetResult>() {
                            @Override
                            public void onResult(DataApi.GetFdForAssetResult getFdForAssetResult) {
                                InputStream assetInputStream = getFdForAssetResult.getInputStream();

                                long initialTime = dataMapItem.getDataMap().getLong(KEY_INITIALTIME);
                                String fileName = new SimpleDateFormat("HH'h'mm'm'ss's'_dd-MM-yyyy").format(initialTime);
                                String dataPath = Environment.getExternalStorageDirectory().toString() + "/TuneWear/Report/";

                                File myDir = new File(dataPath);
                                myDir.mkdirs();

                                File file = new File(myDir, "Run_" + fileName + "_Report.txt");
                                System.out.println("FILE: " + file.getPath());
                                try {
                                    FileOutputStream fOut = new FileOutputStream(file);
                                    int nRead;
                                    byte[] data = new byte[16384];
                                    while ((nRead = assetInputStream.read(data, 0, data.length)) != -1) {
                                        fOut.write(data, 0, nRead);
                                    }

                                    fOut.flush();
                                    fOut.close();
                                } catch (IOException e) {
                                    System.out.println("ERROR File write failed: " + e.toString());
                                }
                            }
                        }
                );
            } else if (item.getUri().getPath().contains(BLEdata)) {
                final DataMapItem dataMapItem = DataMapItem.fromDataItem(item);
                Asset asset = dataMapItem.getDataMap().getAsset("asset");                  

                //TODO here
                Wearable.DataApi.getFdForAsset(googleClient, asset).setResultCallback(
                        new ResultCallback<DataApi.GetFdForAssetResult>() {
                            @Override
                            public void onResult(DataApi.GetFdForAssetResult getFdForAssetResult) {
                                InputStream assetInputStream = getFdForAssetResult.getInputStream();

                                long initialTime = dataMapItem.getDataMap().getLong(KEY_INITIALTIME);
                                String fileName = new SimpleDateFormat("HH'h'mm'm'ss's'_dd-MM-yyyy").format(initialTime);
                                String dataPath = Environment.getExternalStorageDirectory().toString() + "/TuneWear/BLEdata/";

                                File myDir = new File(dataPath);
                                myDir.mkdirs();

                                File file = null;
                                System.out.println("FOOT SIIIIIIDE: " + dataMapItem.getDataMap().getInt("footside"));
                                if(dataMapItem.getDataMap().getInt("footside") == 1){
                                    file = new File(myDir, "Run_" + fileName + "_LeftDevice.txt");
                                    System.out.println("FILE: " + file.getPath());
                                } else if(dataMapItem.getDataMap().getInt("footside") == 2){
                                    file = new File(myDir, "Run_" + fileName + "_RightDevice.txt");
                                    System.out.println("FILE: " + file.getPath());
                                }

                                try {
                                    FileOutputStream fOut = new FileOutputStream(file);
                                    int nRead;
                                    byte[] data = new byte[16384];
                                    while ((nRead = assetInputStream.read(data, 0, data.length)) != -1) {
                                        fOut.write(data, 0, nRead);
                                    }

                                    fOut.flush();
                                    fOut.close();
                                }
                                catch (IOException e) {
                                    System.out.println("ERROR File write failed: " + e.toString());
                                }
                            }
                        }
                );
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            // DataItem deleted
            System.out.println("DataItem deleted: " + event.getDataItem().getUri());
        }
        //Wearable.DataApi.deleteDataItems(googleClient, event.getDataItem().getUri(), DataApi.FILTER_PREFIX);
    }
}

我每次只收到GPS,报告和LOG数据。其他两个文件我只收到一个...我发送它们的方式完全相同,但我只收到其中一个。 有没有人检测到我的代码错误???

修改 我刚刚发现,如果智能手表在发送文件时连接到掌上电脑,则会全部收到。如果它们没有连接,则在连接时不会收到其中一个文件(RightDevice.txt或LeftDevice.txt)...

1 个答案:

答案 0 :(得分:1)

我解决了!

这是所有人中最基本的错误....我正在为两个文件(RightDevice和LeftDevice)发送相同的路径,因此只有一个对象在googleapiclient上更新。