gpt4 book ai didi

wear-os - 将文件 Google Wear 发送到手机

转载 作者:行者123 更新时间:2023-12-02 04:50:46 27 4
gpt4 key购买 nike

我正在尝试将文件从我的 Google Wear 发送到我的 Nexus 5。我已经阅读了一些教程并想出了以下代码,但我的手机没有收到文件。

手机:

private GoogleApiClient mGoogleApiClient;
private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

Log.d(TAG, "CONNECTING:");

mGoogleApiClient.connect();
}

@Override
protected void onResume() {
super.onStart();
}

@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "CONNECTED");
Toast.makeText(getApplicationContext(), "Connected.",
Toast.LENGTH_LONG).show();
Wearable.DataApi.addListener(mGoogleApiClient, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
protected void onPause() {
super.onPause();
Wearable.DataApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Toast.makeText(getApplicationContext(), "Data changed.",
Toast.LENGTH_LONG).show();
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED &&
event.getDataItem().getUri().getPath().equals("/txt")) {
// Get the Asset object
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
Asset asset = dataMapItem.getDataMap().getAsset("sensordataAsset");
Log.d(TAG, "");

ConnectionResult result =
mGoogleApiClient.blockingConnect(10000, TimeUnit.MILLISECONDS);
if (!result.isSuccess()) {
return;
}

// Convert asset into a file descriptor and block until it's ready
InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
mGoogleApiClient, asset).await().getInputStream();
mGoogleApiClient.disconnect();
if (assetInputStream == null) {
return;
}

// Get folder for output
File storage = getApplicationContext().getFilesDir();
Log.d(TAG, "Location:" + storage.toString());
File dir = new File(storage.getAbsolutePath() + "/Clockwork/");
if (!dir.exists()) {
dir.mkdirs();
} // Create folder if needed
Log.d(TAG, dir.getAbsolutePath());

// Read data from the Asset and write it to a file on external storage
final File file = new File(dir, "sensordata.txt");
if (!file.exists()) {
file.mkdirs();
}
Log.d(TAG, 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 (Exception e) {
}

// Rescan folder to make it appear
try {
String[] paths = new String[1];
paths[0] = file.getAbsolutePath();
MediaScannerConnection.scanFile(this, paths, null, null);
} catch (Exception e) {
}
}
}
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

穿着:

public void sendTextFile() {

readFromFile();
FileInputStream fileInputStream = null;

byte[] bFile = new byte[(int) file.length()];
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
} catch (Exception e) {
Log.e(TAG, "sendTextFile: " + e.toString());
}

connectGoogleApi();
//Create an Asset from the byte array and set it via the DataApi
Asset asset = Asset.createFromBytes(bFile);
PutDataMapRequest dataMap = PutDataMapRequest.create("/txt");
dataMap.getDataMap().putAsset("sensordataAsset", asset);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
.putDataItem(mGoogleApiClient, request);

}
private void connectGoogleApi() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "CONNECTED");
Toast.makeText(context, "Connected.",
Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

我用上面列出的方法制作了一个记录器。有人可以告诉我我做错了什么。提前致谢。

最佳答案

在数据图中添加一个键值:map.put("时间戳", System.currentmillions())您的手机没有收到任何更改事件,因为您将相同的数据项放入数据 api 队列中,请尝试放入不同的项

关于wear-os - 将文件 Google Wear 发送到手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28808283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com