简体   繁体   中英

How to check if Samsung Gear 2 is paired with Android device programmatically?

I am using Tizen SDK for Wearable from samsung-gear site in order to communicate a provider android application with Samsung Gear 2 device. I am able to send notifications to gear and once I run the consumer application on gear 2, I am able to transfer data between the watch and my Android phone as well.

What I am trying to do is to check within the Android application if the phone is paired with Gear 2. Something as simple as creating a communication object using the accessory service and calling a method like isPaired()?:

CommunicationObject commObject = new CommunicationObject(Communication parameters);

// I am assuming some connection call like commObject.connect() should be invoked first
// where I can check for it's result afterwards such as

if(commObject.isPaired())
{
  // do something
}

I think SDK examples such as consumer/provider application they provide on their site already assume that the device is paired, hence they show how to transfer data between phone and the gear watch. Yet I am seeking something as simple as asking the phone if it's paired with a gear device, which should be the prerequisite for transferring the data, which is done automatically by Samsung Gear Manager I believe right now.

Note: For the case of example provider/consumer applications, one can just check if any connection is available using the code in them. But the data transfer connection enabled only when I manually start the consumer app from the gear device, otherwise it acts like gear device is not paired even though it is.

I believe this is not the most popular topic these days so I will post what I came up with as an answer although I doubt anyone will need it, without being perfect, it's the closest way I could get to my goal using the available documentation.

I should also mention that this slide helped me stay on track as well.

In my solution, there must be an active 2-way connection between the gear widget(consumer/.wgt) and the host side application(provider/.apk) as in the example application provided by Samsung(Hello Accessory) at all times, at least during the time where I wanted to check for the pairing condition. The documentation refers to it as:

Hello Gear is a simple application that consists of:

  • Host-side application(provider) : HelloAccessoryProvider.apk
  • Wearable-side Application(consumer) : HelloAccessoryConsumer.wgt (Web app)

See that both sides have some xml configuration and Android requires specific permissions which are explained in detail in Hello Gear documentation.

This 2 way communication is provided by the Samsung Accessory Framework on the network layer(through Samsung Accessory Protocol, SAP) given that both sides implement the same Accessory Service Profile, again, configured via the xml files on both ends(service name, channel id etc.).

连接的体系结构

Android side implements the protocol as a service, extending the SAAgent abstract class. Then the widget on gear side application(.wgt) can invoke the SAAgent callbacks and provider/consumer communication is handled through SASocket objects claimed on both ends over the predefined channel in the xml configuration files.

SAAgent工作流程

Please note that this communication has to be initialized on both ends, in my case I had to open the widget application once on Gear(I believe there should be a way to start the gear widget via an intent or notification, somehow, but I could not find yet) after the Android application has started, here started means that SAAgent service is up and bound to an Activity, being eligible to receive callbacks and send state messages to the rest of the application via broadcasts. Such as the number of active connections, or any data transmission between the gear socket and Android application can be done this way.

Note that if you don't have to transfer data between the gear widget and the Android application, you may just be OK with the notifications. The only requirement to send notifications to the Gear from Android applications seems to be that the Gear is paired with your phone and connected via Bluetooth. Then you can just send an intent as explained in more detail here in Section 6. All you need should be the permission:

com.samsung.wmanager.ENABLE_NOTIFICATION

and some metadata definition in your ApplicationManifest.xml file explained in the same section.

<meta-data
android:name="master_app_packagename"
 android:value="com.example.gearMasterApp"/>
<meta-data
android:name="app_notification_maxbyte"
 android:value="300 "/>

And here is the sample code for intent, in order to send notifications to the Gear:

public static final String ALERT_NOTIFICATION = 
“com.samsung.accessory.intent.action.ALERT_NOTIFICATION_ITEM”; 
public static final int NOTIFICATION_SOURCE_API_SECOND = 3;
Bitmap bitmapImg;
// Put data to Intent 
Intent myIntent = new Intent(ALERT_NOTIFICATION); 
myIntent.putExtra("NOTIFICATION_PACKAGE_NAME", “com.example.gearApp”); 
myIntent.putExtra("NOTIFICATION_VERSION", NOTIFICATION_SOURCE_API_SECOND);
myIntent.putExtra("NOTIFICATION_TIME", System.currentTimeMillis();); 
myIntent.putExtra("NOTIFICATION_MAIN_TEXT", “Title Text”); 
myIntent.putExtra("NOTIFICATION_TEXT_MESSAGE", ”Body text); 
byte [] byteArray = convertResizeBitmapToByte(bitmapImg);
myIntent.putExtra("NOTIFICATION_APP_ICON", byteArray); 
myIntent.putExtra("NOTIFICATION_LAUNCH_INTENT", “com.example.gearMasterApp”); 
myIntent.putExtra("NOTIFICATION_LAUNCH_TOACC_INTENT", “com.example.gearSideApp”); 
sendBroadcast(myIntent); 

public byte[] convertResizeBitmapToByte(Bitmap bitmap){
 Bitmap scBitmap = Bitmap.createScaledBitmap(bitmap, 75, 75, false);
 ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
 scBitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayStream);

 return byteArrayStream.toByteArray();
}

Once the notification is read on the gear side, you can receive the intent action along with some optional parameters:

Intent Action : "com.samsung.accessory.intent.action.UPDATE_NOTIFICATION_ITEM"

This could be another approach to check active communication with the Gear and your phone, but there is no guarantee that the notification will be read and my case did require to keep the Gear communication optional in order to allow the Android application continue it's tasks even though there is no active connection with the Gear.

About the original question, where I asked for a way to detect if the Gear is paired or not, I tried listing paired Bluetooth devices using getBondedDevices() method of Android's BluetoothAdapter but it shows that your Gear is paired even when your Gear is turned off, which was not enough for my needs and I did not find it logical. It's true though once your device is turned back on.

I'm happy with the above solution since it was enough for my needs, therefore I will accept my own answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM