安卓 创建手表wear应用 并实现与app蓝牙通讯

记忆力下降的厉害,年前做的demo,年后就忘了差不多了,当然也有一部分原因是因为这个年假确实有点长,,,,whatever,还是写个博客记录一下,忘了也有地方找

结果,还是忘了,,,

贴两个地址吧

谷歌官方文档

如何调试wear应用

wear应用和手机端通过蓝牙互相传数据方面,有个数据层,手机端数据改变发送到数据层,数据层会随之改变,并在wear应用打开的时候通知监听

wear方面的监听

package com.example.win.aodwear2;

import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItem;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

/**
 * Created by win on 2019/12/25.
 */

public class DataLayerListenerService extends WearableListenerService{

    private CtrApp app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = (CtrApp) getApplication();
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        System.out.println("消息=========");
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEventBuffer) {
        System.out.println("收到的数据变化ondatachanged222===");

        Log.i("info", "数据发生变化喽");


        for (DataEvent event : dataEventBuffer) {
            if (event.getType() == DataEvent.TYPE_CHANGED) {
                String typeString = event.getDataItem().getUri().getPath();
                DataItem item = event.getDataItem();
                DataMap map = DataMapItem.fromDataItem(item).getDataMap();
                //item.getUri().getPath().compareTo("/count") == 0
                if ("/image".equals(typeString)) {
                    Log.i("info", "是图片");
                    Asset mAsset = map.getAsset("asset");
                } else if (typeString.compareTo("/bledata") == 0){
                    app.dev_name = map.getString("dev_name");
                    app.bt_key = map.getString("bt_key");

                    app.isbleconnect=0;

                    SharedPreferences.Editor editor = getSharedPreferences("login", MODE_PRIVATE).edit();
                    editor.putString("dev_name", app.dev_name);
                    editor.putString("bt_key", app.bt_key);
                    editor.commit();
                    System.out.println("数据变化类型事件:222"+app.dev_name+"=="+app.bt_key);
                    Toast.makeText(getApplicationContext(),"车辆更换,正在重新连接蓝牙",Toast.LENGTH_LONG).show();
                    //Log.i("info", "数据变化类型事件:" + getString);
                }
            } else if (event.getType() == DataEvent.TYPE_DELETED) {
                Log.i("info", "数据删除");
            }
        }

//        for (DataEvent event : dataEventBuffer) {
//            if (event.getType() == DataEvent.TYPE_CHANGED) {
//                // DataItem changed
//                DataItem item = event.getDataItem();
//                if (item.getUri().getPath().compareTo("/count") == 0) {
//                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
//                    String date = dataMap.getString("btkey");
//                    System.out.println("收到的数据==="+date);
//                    Toast.makeText(this,date,Toast.LENGTH_SHORT).show();
//
//                    //updateCount(dataMap.getInt(COUNT_KEY));
//                }
//            } else if (event.getType() == DataEvent.TYPE_DELETED) {
//                // DataItem deleted
//            }
//        }

    }
}

手机端同步数据层:

    //与手表通讯部分,暂不开启
    public int iswearopen=0;
    public void changeDevName(String dev_name,String bt_key){
        if (iswearopen==0){
            return;
        }
        SharedPreferences read = getSharedPreferences("login", MODE_PRIVATE);
        String olddev_name = read.getString("dev_name", "");
        if(!TextUtils.isEmpty(dev_name)&&!TextUtils.isEmpty(olddev_name)&&!dev_name.equals(olddev_name)){
            if (isgooglecon==1) {
                PutDataMapRequest mapRequest = PutDataMapRequest.create("/bledata");
                DataMap dataMap = mapRequest.getDataMap();
                dataMap.putString("dev_name", dev_name);
                dataMap.putString("bt_key", bt_key);
                mapRequest.setUrgent();
                PutDataRequest dataRequest = mapRequest.asPutDataRequest();
                dataRequest.setUrgent();
                Wearable.DataApi.putDataItem(mGoogleAppiClient, dataRequest).setResultCallback(new ResultCallback() {


                    @Override
                    public void onResult(DataApi.DataItemResult arg0) {
                        if (!arg0.getStatus().isSuccess()) {
                        } else {
                            Log.i("info", "发送成功了");
                            //Toast.makeText(MainActivity.this, "Notification  Success", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            } else {
                Log.i("info", "没连上");
            }
        }
    }

 

你可能感兴趣的:(安卓,手表)