本文针对手表开发中的问题做一些记录,希望能帮到需要的人,写的不好不对的地方请指教(本文采用Kotlin代码编写,作者的手表为小米手表)。相关demo连接:https://github.com/bestarandyan/TestWearProject
前提条件
1,手机上必须同时装有Wear OS 和小米穿戴 两个app。
2,手表已和手机通过wear OS 和 小米穿戴app 建立连接。
项目创建
创建android wear 项目的过程就不讲解了,很容易能查到,这里只讲一下注意事项。
1,手表app的包名必须和对应的手机app的包名保持一致。
2,手表app的签名文件必须和手机app的签名文件保持一致。
使用GoogleServices 7.8.87的使用方法:
1,mobile app的build.gradle文件中加入
wearApp project(':wearapptest')//换成你的手表app的 project name
implementation 'com.google.android.gms:play-services-wearable:17.0.0'
2,wear app的build.gradle文件中加入
implementation 'com.google.android.gms:play-services-wearable:17.0.0'
compileOnly 'com.google.android.wearable:wearable:2.5.0'
implementation 'com.google.android.support:wearable:2.5.0'
手表和手机之间建立连接(手机和手表通用)
//当前activity实现相关接口
class MainActivity : WearableActivity(), GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, CapabilityApi.CapabilityListener {
//.... 此处省略了一些无关代码
var googleApiClient: GoogleApiClient? = null
private fun connectServer() {
googleApiClient = GoogleApiClient.Builder(this)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.addApi(Wearable.API).build()
googleApiClient!!.registerConnectionCallbacks(this)
googleApiClient!!.registerConnectionFailedListener(this)
googleApiClient!!.connect()
}
override fun onConnected(p0: Bundle?) {
Log.e("wearmain", "连接成功")
}
override fun onConnectionSuspended(p0: Int) {
Log.e("wearmain", "连接中断")
}
override fun onConnectionFailed(p0: ConnectionResult) {
Log.e("wearmain", "连接失败")
}
}
手表与手机之间的通信(手表和手机通用)
1,数据接收方实现数据监听接口并注册数据监听器
class MainActivity : Activity(),DataClient.OnDataChangedListener, GoogleApiClient.ConnectionCallbacks{
//.... 此处省略了一些无关代码
override fun onConnected(p0: Bundle?) {
Log.e("AppLog", "连接成功")
Wearable.DataApi.addListener(googleApiClient, this)
}
override fun onDataChanged(p0: DataEventBuffer?) {
Log.d("AppLog", "收到发过来的数据")
for (data in p0!!) {
val uri = data.dataItem.uri
val path = uri.path
if (!TextUtils.isEmpty(path) && "/message" == path) {
val dataMap = DataMapItem.fromDataItem(data.dataItem).dataMap
val cmdValue = dataMap.getString("message")
Log.e("AppLog", "收到数据的时间是:" + dataMap.getString("time"))
}
}
}
}
2,数据发送方发送数据
private fun sendData() {
val dataMap = PutDataMapRequest.create("/message")
dataMap.dataMap.putLong("time", Date().time)
dataMap.dataMap.putString("message","数据时间:"+Date().time)
val request = dataMap.asPutDataRequest()
request.setUrgent()
val dataItemTask = Wearable.getDataClient(this).putDataItem(request)
dataItemTask.addOnSuccessListener { dataItem ->
Log.e(
"MobileAppLog",
"Sending image was successful: $dataItem"
)
}
}
手表app通过RemoteIntent + scheme 启动并唤醒手机app
1,在手机app的Manifest中给需要被打开的activity添加scheme配置
注意:scheme不要用http,因为使用http手机端会出现一个选择框,让你选择使用浏览器打开还是选择应用打开)
2,通过RemoteIntent.startRemoteActivity打开第一步中Activity即可
private val ANDROID_MARKET_APP_URI = "ingeek://ingeek:8080/homeActivity?tool_id=100"
private fun startRemoteIntent() {
val intentAndroid = Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(Uri.parse(ANDROID_MARKET_APP_URI))
RemoteIntent.startRemoteActivity(
applicationContext,
intentAndroid,
mResultReceiver
)
}
// Result from sending RemoteIntent to phone to open app in play/app store.
private val mResultReceiver: ResultReceiver = object : ResultReceiver(Handler()) {
override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
if (resultCode == RemoteIntent.RESULT_OK) {
ConfirmationOverlay().showOn(this@MainActivity)
Log.e("wearmain", "启动app成功")
} else if (resultCode == RemoteIntent.RESULT_FAILED) {
ConfirmationOverlay()
.setType(ConfirmationOverlay.FAILURE_ANIMATION)
.showOn(this@MainActivity)
Log.e("wearmain", "启动app失败")
} else {
throw IllegalStateException("Unexpected result $resultCode")
}
}
}