将资产从手表转移到手持设备

时间:2015-04-30 05:28:30

标签: android google-play-services wear-os android-wear-data-api

我试图复制此功能https://developer.android.com/training/wearables/data-layer/assets.html。 手表中的发射器部分很好。在掌上电脑中onDataChanged的{​​{1}}函数中,每次调用WearableListenerService时都会发生异常。

例外情况如下:

DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());

发射器代码如下:

04-30 13:02:21.003  16588-16588/com.xxx.hbips I/dalvikvm﹕ Could not find method     android.content.pm.PackageManager.getPackageInstaller, referenced from method   com.google.android.gms.common.GooglePlayServicesUtil.zzh
04-30 13:02:21.003  16588-16588/com.xxx.hbips W/dalvikvm﹕ VFY: unable to resolve virtual method 464:    Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
04-30 13:02:21.003  16588-16588/com.xxx.hbips D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000b
04-30 13:02:21.053  16588-16790/com.xxx.hbips E/DataLayerSample﹕ WEAR :: Service connected to   GoogleApiClient.
04-30 13:02:21.053  16588-16790/com.xxx.hbips W/dalvikvm﹕ threadid=19: thread exiting with uncaught     exception (group=0x417bfd58)
04-30 13:02:21.053  16588-16790/com.xxx.hbips E/AndroidRuntime﹕ FATAL EXCEPTION: WearableListenerService
    Process: com.xxx.hbips, PID: 16588
    java.lang.IllegalStateException: Cannot find DataItemAsset referenced in data at 0 for DataItemEntity   [@4287a7e0,dataSz=0, numAssets=1, uri=wear://df70fc47-0580-4750-b6ea-6e47439ed1ed/audio]
            at com.google.android.gms.wearable.DataMapItem.zza(Unknown Source)
            at com.google.android.gms.wearable.DataMapItem.<init>(Unknown Source)
            at com.google.android.gms.wearable.DataMapItem.fromDataItem(Unknown Source)
            at com.xxx.hbips.DataLayerListenerService.onDataChanged(DataLayerListenerService.java:58)
            at com.google.android.gms.wearable.WearableListenerService$zza$1.run(Unknown Source)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.os.HandlerThread.run(HandlerThread.java:61)

接收者代码如下:

class IpsTimerTask extends TimerTask {
        public void run() {
            sem.acquireUninterruptibly();
            byte[] buffer = new byte[60000];
            audioRecord.startRecording();
            audioRecord.read(buffer, 0, 60000);
            audioRecord.stop();
            sem.release();

            Asset asset = Asset.createFromBytes(buffer);
            PutDataRequest request = PutDataRequest.create("/audio");
            request.putAsset("Audio", asset);
            Wearable.DataApi.putDataItem(mGoogleApiClient, request);
            Log.d(TAG, "transmitted"+buffer.length);
        }
    }

请问任何想法?

1 个答案:

答案 0 :(得分:0)

我通过替换

解决了这个问题
    PutDataRequest putDataRequest = PutDataRequest.create("/image");
    putDataRequest.putAsset("debugFile", asset);
    Wearable.DataApi.putDataItem(client, putDataRequest);

用这个

    PutDataMapRequest dataMap = PutDataMapRequest.create("/image");
    dataMap.getDataMap().putAsset("debugFile", asset);
    PutDataRequest request = dataMap.asPutDataRequest();
    PendingResult<DataItemResult> pendingResult = Wearable.DataApi.putDataItem(client, request);
    pendingResult.setResultCallback(new ResultCallback<DataItemResult> () {
        @Override
        public void onResult(DataItemResult dataItemResult) {
            // something 
        }
    } );

现在它对我有用