我想将消息从Android Wear设备发送到连接的手机。 Android智能手机应用程序是使用本地Java编写的并正在运行。磨损应用程序是用Xamarin编写的,不工作。 这是我走到这一步:Android Wear Xamarin消息
public class MainActivity : Activity, IMessageApiMessageListener,
IGoogleApiClientConnectionCallbacks, IResultCallback
{
private IGoogleApiClient mApiClient;
private SensorManager _SensorManager;
private Sensor _Gyroscop;
private static String WEAR_MESSAGE_PATH = "//message";
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var v = FindViewById<WatchViewStub> (Resource.Id.watch_view_stub);
v.LayoutInflated += delegate {
};
if(_SensorManager == null) {
_SensorManager = (SensorManager)GetSystemService(SensorService);
}
SensorListener s = new SensorListener();
_Gyroscop = _SensorManager.GetDefaultSensor (SensorType.Gyroscope);
_SensorManager.RegisterListener (s, _Gyroscop,SensorDelay.Normal);
s.SensorChangedEvent += delegate(SensorEvent e) {
if(mApiClient.IsConnected){
sendMessage(WEAR_MESSAGE_PATH,"Test");
}
};
initGoogleApiClient();
}
private void initGoogleApiClient() {
mApiClient = new GoogleApiClientBuilder(this)
.AddApi(WearableClass.Api)
.AddConnectionCallbacks(this)
.Build();
if (mApiClient != null && !(mApiClient.IsConnected || mApiClient.IsConnecting)) {
mApiClient.Connect();
}
}
private void sendMessage( String path, String text) {
WearableClass.NodeApi
.GetConnectedNodes (mApiClient)
.SetResultCallback (this);
}
public void OnResult (Java.Lang.Object raw)
{
Exception nodeException, messageException;
try {
//send the message
var nodeResult = raw.JavaCast<INodeApiGetConnectedNodesResult>();
foreach (var node in nodeResult.Nodes)
WearableClass.MessageApi.SendMessage (mApiClient, node.Id, WEAR_MESSAGE_PATH, StringToByteArray("Test")).SetResultCallback (this); //will go to second try/catch block
return;
} catch (Exception e) {
nodeException = e;
}
try {
//check that it worked correctly
var messageResult = raw.JavaCast<IMessageApiSendMessageResult>();
if (!messageResult.Status.IsSuccess)
Console.WriteLine ("Problem");
return;
} catch (Exception e) {
messageException = e;
}
}
public void OnConnected (Bundle p0)
{
WearableClass.MessageApi.AddListener (mApiClient, this);
}
public void OnConnectionSuspended (int p0)
{
WearableClass.MessageApi.RemoveListener (mApiClient, this);
}
}
的问题是,手机计量不接收消息。
您是否设法解决问题?我正在努力从手持设备发送消息(或数据)(或反之)。发送时,*接收*应用程序立即崩溃。我主要使用这两个引用:http://android-wear-docs.readthedocs.org/en/latest/sync.html#first-wearable-message和http://toastdroid.com/2014/08/18/messageapi -simple-conversations-with-android-wear/ – Falko 2015-02-24 09:47:54
我的Android版单机版发送问题未解决。我已经切换到本地Java,并且这个接口可以毫无问题地工作。我使用这个教程:http://www.binpress.com/tutorial/a-guide-to-the-android-wear-message-api/152[link] – user1120866 2015-02-24 12:04:07