2017-11-02 34 views
0

私はandroid wearとandroid phone appを持っています。私はそれらを接続させようとしており、最終的に時計から電話にメッセージを送信します。Android Wearデバイスがノード(電話)を検出していませんDataApi

public class MainActivity extends WearableActivity { 
 
    public static final String TAG = "WatchApp"; 
 
    public GoogleApiClient mApiClient; 
 
    public static final int CONNECTION_TIME_OUT_MS = 1000; 
 
    public static String nodeId; 
 
    private static final String AIR_LIFT_CAPABILITY = "airlift"; 
 

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

 
     initApi(); 
 

 
     final Button btnUp = (Button)findViewById(R.id.btnUp); 
 
     btnUp.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       String text = "Preset"; 
 
       //mAdapter.add(text); 
 
       //mAdapter.notifyDataSetChanged(); 
 

 

 
       sendMessage(text); 
 
       Log.i("WatchApp", "btnUp Sent Preset"); 
 

 
      } 
 
     }); 
 

 
     Button btnDown = (Button)findViewById(R.id.btnDown); 
 
     btnDown.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       String text = "AirOut"; 
 
       //mAdapter.add(text); 
 
       //mAdapter.notifyDataSetChanged(); 
 

 

 
       sendMessage(text); 
 
       Log.i("WatchApp", "btnUp Sent AirOut"); 
 
      } 
 
     }); 
 

 
     // Enables Always-on 
 
     setAmbientEnabled(); 
 

 

 

 
    } 
 

 

 
    private void initApi() { 
 
     mApiClient = getGoogleApiClient(this); 
 
     setOrUpdateNotification(); 
 
     Log.i("WatchApp", "initApi()"); 
 

 
    } 
 

 
    private GoogleApiClient getGoogleApiClient(Context context) { 
 
     return new GoogleApiClient.Builder(context) 
 
       .addApi(Wearable.API) 
 
       .build(); 
 

 
    } 
 

 

 

 
    private void setOrUpdateNotification() { 
 
     new Thread(new Runnable() { 
 
      @Override 
 
      public void run() { 
 
       CapabilityApi.GetCapabilityResult result = 
 
         Wearable.CapabilityApi.getCapability(
 
           mApiClient, AIR_LIFT_CAPABILITY, 
 
           CapabilityApi.FILTER_REACHABLE).await(1000, TimeUnit.MILLISECONDS); 
 
       updateFindMeCapability(result.getCapability()); 
 

 
       CapabilityApi.CapabilityListener capabilityListener = 
 
         new CapabilityApi.CapabilityListener() { 
 
          @Override 
 
          public void onCapabilityChanged(CapabilityInfo capabilityInfo) { 
 
           updateFindMeCapability(capabilityInfo); 
 
          } 
 
         }; 
 

 
       // Wearable.CapabilityApi.addCapabilityListener(mApiClient, capabilityListener, AIR_LIFT_CAPABILITY); 
 
      } 
 
     }).start(); 
 
    } 
 

 
    private void updateFindMeCapability(CapabilityInfo capabilityInfo) { 
 
     if (capabilityInfo.getNodes() == null) return; 
 
     Set<Node> connectedNodes = capabilityInfo.getNodes(); 
 
     if (connectedNodes.isEmpty()) { 
 
      Log.i(TAG,"No Nodes Found."); 
 
     } else { 
 
      for (Node node : connectedNodes) { 
 
       // we are only considering those nodes that are directly connected 
 
       if (node.isNearby()) { 
 
        Log.i(TAG,"FOUND NODE"); 
 
        nodeId = node.getId(); 
 
       } 
 
      } 
 
     } 
 
    } 
 

 

 
    private void sendMessage(final String MESSAGE) { 
 
     if (nodeId != null) { 
 
      new Thread(new Runnable() { 
 
       @Override 
 
       public void run() { 
 
        mApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); 
 
        Wearable.MessageApi.sendMessage(mApiClient, nodeId, MESSAGE, null); 
 
        mApiClient.disconnect(); 
 
        Log.i("WatchApp", "Message ("+MESSAGE+") sent"); 
 
       } 
 
      }).start(); 
 
     } else { 
 
      Log.i("WatchApp", "No Nodes to send message!"); 
 
     } 
 
    } 
 

 

 

 

 

 
    @Override 
 
    protected void onDestroy() { 
 
     super.onDestroy(); 
 
     mApiClient.disconnect(); 
 
     mApiClient = null; 
 
     Log.i("WatchApp", "onDestroy()"); 
 
    } 
 

 

 
}

着用AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
    package="com.schoen.jonathan.airliftwatch"> 
 

 

 
    <uses-feature android:name="android.hardware.type.watch" /> 
 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
 

 
    <application 
 
     android:allowBackup="true" 
 
     android:icon="@mipmap/ic_launcher" 
 
     android:label="@string/app_name" 
 
     android:supportsRtl="true" 
 
     android:theme="@android:style/Theme.DeviceDefault"> 
 
     <uses-library 
 
      android:name="com.google.android.wearable" 
 
      android:required="true" /> 
 

 
     <meta-data android:name="com.google.android.gms.version" 
 
      android:value="@integer/google_play_services_version" /> 
 

 
     <activity 
 
      android:name="com.schoen.jonathan.airliftwatch.MainActivity" 
 
      android:label="@string/app_name"> 
 
      <intent-filter> 
 

 
       <category android:name="android.intent.category.LAUNCHER" /> 
 

 
      </intent-filter> 
 
     </activity> 
 
    </application> 
 

 
</manifest>
しかし時計で、Wearable.CapabilityApi.getCapability()はノードここ

が着用MainActivityではありません返します

電話MainActivity:

public class MainActivity extends Activity { 
 

 
    boolean mBound = false; 
 

 
    public static final String TAG = "PhoneApp"; 
 
    public ListenerService listener; 
 
    public GoogleApiClient client; 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 

 
     Button btnStart = (Button)findViewById(R.id.btnStart); 
 
     Button btnStop = (Button)findViewById(R.id.btnStop); 
 

 

 
     Log.i("PhoneApp", "onCreate() Receiver Registered "); 
 
     btnStart.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 

 
       Log.i("PhoneApp", "btnStart Receiver Registered"); 
 
      } 
 
     }); 
 

 
     btnStop.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 

 
       Log.i("PhoneApp", "btnStop Receiver unRegistered"); 
 
      } 
 
     }); 
 

 

 

 
     registerListener(); 
 
    } 
 

 
public void registerListener() { 
 
    client = new GoogleApiClient.Builder(this) 
 
      .addApi(Wearable.API) 
 
      .build(); 
 
    listener = new ListenerService(); 
 
    Wearable.MessageApi.addListener(client ,listener); 
 
    Log.i("PhoneApp", "registerListener() Listener Registered "); 
 
} 
 

 
    @Override 
 
    protected void onStart() { 
 
     super.onStart(); 
 
     registerListener(); 
 
     Log.i("PhoneApp", "onStart() Listener Registered "); 
 
    } 
 

 
    protected void onResume() { 
 
     // TODO Auto-generated method stub 
 
     super.onResume(); 
 
     registerListener(); 
 

 
     Log.i("PhoneApp", "onResume() Listener Registered "); 
 
    } 
 

 
    @Override 
 
    protected void onPause() { 
 
     // TODO Auto-generated method stub 
 
     super.onPause(); 
 
     //unregister our receiver 
 
     if (client != null && listener != null) 
 
      Wearable.MessageApi.removeListener(client, listener); 
 
     client = null; 
 
     listener = null; 
 
     Log.i("PhoneApp", "onPause() Listener Unregistered "); 
 
    } 
 

 
    @Override 
 
    protected void onDestroy() { 
 
     // TODO Auto-generated method stub 
 
     super.onDestroy(); 
 
     //unregister our receiver 
 
     if (client != null && listener != null) 
 
      Wearable.MessageApi.removeListener(client, listener); 
 
     client = null; 
 
     listener = null; 
 
     Log.i("PhoneApp", "onPause() Listener Unregistered "); 
 
    } 
 
}

電話・リスナー・サービス:

public class ListenerService extends WearableListenerService { 
 

 
    String nodeId; 
 

 
    @Override 
 
    public void onMessageReceived(MessageEvent messageEvent) { 
 
     nodeId = messageEvent.getSourceNodeId(); 
 
     Log.i(MainActivity.TAG, messageEvent.getPath()); 
 
     showToast(messageEvent.getPath()); 
 

 

 
     String msg_for_me = messageEvent.getPath(); 
 

 
     if (msg_for_me == "AirOut") 
 
     { 
 
      Log.i(MainActivity.TAG, "Air Out"); 
 
     } else if (msg_for_me == "Preset") 
 
     { 
 
      
 
      Log.i(MainActivity.TAG, "Preset"); 
 
     } 
 
     
 
    } 
 

 
    
 

 
    private void showToast(String message) { 
 
     Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
 
    } 
 

 
}

電話Androidのマニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
    package="com.schoen.jonathan.airliftwatch"> 
 

 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
 

 
    <application 
 
     android:allowBackup="true" 
 
     android:icon="@mipmap/ic_launcher" 
 
     android:label="@string/app_name" 
 
     android:roundIcon="@mipmap/ic_launcher_round" 
 
     android:supportsRtl="true" 
 
     android:theme="@style/AppTheme"> 
 

 
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
 

 
     <service 
 
      android:name="com.schoen.jonathan.airliftwatch.ListenerService" > 
 
      <intent-filter> 
 
       <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> 
 
      </intent-filter> 
 
     </service> 
 

 

 
     <meta-data android:name="com.google.android.gms.version" 
 
      android:value="@integer/google_play_services_version" /> 
 

 
     <activity 
 
      android:name=".MainActivity" 
 
      android:label="@string/app_name" 
 
      android:theme="@style/AppTheme.NoActionBar"> 
 
      <intent-filter> 
 
       <category android:name="android.intent.category.LAUNCHER" /> 
 
      </intent-filter> 
 

 
     </activity> 
 
    </application> 
 

 
</manifest>

電話wear.xml:

<resources> 
 
    <string-array name="android_wear_capabilities"> 
 
     <item>airlift</item> 
 
    </string-array> 
 
</resources>

すべてのアイデアは、私が戻ったノードの場合はnull結果を得るなぜ?私の電話と腕時計は現在ペアになっており、私はFacebookやEメールのようなものから私の電話から私の時計の更新を取得します。

答えて

0

このthreadに記載されているように、NodeApiはデバイス側で使用できます。

NodeApi.GetConnectedNodesResult nodes = 
    Wearable.NodeApi.getConnectedNodes(mApiClient).await(); 

ノード内の各ノードについて、getDisplayName()を呼び出すことができます。

また、このlinkに基づいて、ハンドヘルドアプリとウェアラブルアプリの両方で同じデバッグ証明書を使用してください。そうしないとCapabilityAPIは機能しません。

関連する問題