我正在尝试在Nexus手机和华为智能手表(WearOS)之间建立双向连接。手机和手表使用相同的数据发送类别,并且能够互相发送数据。手表能够将信息发送到手机,但不收听信息。我正在使用Message API。
问:如果一切设置正确,为什么手表上没有收到我的信息?
该项目的基本思想是,当从智能手表(通过加速度计数据)检测到跌倒时,手机和手表可以相互发送消息,以供手表改变活动并提示出现另一个屏幕。 (主屏幕与提示屏幕)。
public class MainActivity extends WearableActivity implements
SensorEventListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
MessageClient.OnMessageReceivedListener {
MessageListener messageListen;
private TextView mTextView;
private SensorManager mSensorManager;
private Sensor accelerometer;
private float[] values;
//private long startTime;
private GoogleApiClient mGoogleApiClient;
private String PATH_DATA = "/falldata";
private MessageSender dataSender;
String TAG = "WatchMainActivity";
private boolean listenerRegistered = false;
/*
This class is responsible for the android wear app.
The app should be able to:
-Run as a service so that the user does not need to keep the app open.
-Collect accelerometer data at a frequency of 32 ms. This should be done by either throwing a set number of samples away or by only sending a sample if we are at a interval of 32 ms. The latter option might be the best option.
-Send samples to the Android phone app. https://developer.android.com/training/wearables/data-layer/index.html
*/
@Override
protected void onCreate(Bundle savedInstanceState) //average delay 205.35 ms over 2150 samples
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextView = (TextView) findViewById(R.id.title_text);
// Enables Always-on
setAmbientEnabled();
if (null == mGoogleApiClient)
{
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Log.v(TAG,"GoogleApiClient created");
}
if (!mGoogleApiClient.isConnected())
{
mGoogleApiClient.connect();
Log.v(TAG, "Connecting to GoogleApiClient.");
}
final Button stop_button = findViewById(R.id.stop_button);
final Button start_button = findViewById(R.id.start_button);
start_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
start();
}
});
stop_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
dataSender = MessageSender.getInstance(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
values = event.values; //changed to a global value
dataSender.sendMessage(PATH_DATA, "" + values[0] + "," + values[1] + "," + values[2]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
System.out.println("Accuracy changed. Does something need to change?");
}
@Override
public void onConnected(@Nullable Bundle bundle) {
System.out.println("OnConnected has been called");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
System.out.println("Message received");
Log.i(TAG, "Message received!");
if(messageEvent.getPath().equals("/falldetected")) //a fall was detected for reals
{
System.out.println("Message received falldetected");
//Create the new intent
intentStuff();
}
}
public void start()
{
System.out.println("The START BUTTON has been clicked.");
Wearable.MessageApi.addListener(mGoogleApiClient, this);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
boolean sensorRegistered = mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
Log.d("Sensor Status:", " Sensor registered: " + (sensorRegistered ? "yes" : "no"));
}
public void stop()
{
mSensorManager.unregisterListener(this, accelerometer);
Wearable.MessageApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
public void intentStuff()
{
Intent startIntent = new Intent("android.intent.action.FallOptions");
//Pull up new activity
this.startActivity(startIntent); //starting the activity with the new intent
}
}
我希望手表和手机能够相互发送消息,以便当任一设备接收到消息时,将调用onMessageReceived(),并且应将我作为测试所使用的打印语句打印到律师中。
但是,手表应在收到消息后没有打印声明。