[Pebble Watch]How to do this in b4a?

salmander

Active Member
Licensed User
Longtime User
Hello All,

I have been looking at the Pebble Watch documentation (which only contains few methods for now) and was wondering how to do it in B4A. At the moment the pebble watch can only be communicated by sending intents to the pebble android app.
Can anyone help please with the following?

Send Notification to Pebble

B4X:
public void sendAlertToPebble() {
    final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
 
    final Map<String, Object> data = new HashMap<String, Object>();
    data.put("title", getAlertTitle());
    data.put("body", getAlertBody());
    final JSONObject jsonData = new JSONObject(data);
    final String notificationData = new JSONArray().put(jsonData).toString();
 
    i.putExtra("messageType", PEBBLE_ALERT);
    i.putExtra("sender", "MyAndroidApp");
    i.putExtra("notificationData", notificationData);
 
    Log.d(TAG, "About to send a modal alert to Pebble: " + notificationData);
    sendBroadcast(i);
}


Send Data to Pebble

B4X:
public void sendLocationInfoToPebble() {
    final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 
    if (location == null)
        return;
 
    final String latLong = String.format("%.02f %.02f",
            location.getLatitude(),
            location.getLongitude());
 
    final Intent i = new Intent("com.getpebble.action.SEND_DATA");
    i.putExtra("sender", "MyGeocachingApp");
    i.putExtra("recipient", "PebbleGeocache");
    i.putExtra("data", latLong);
 
    Log.d(TAG, "About to send location coordinates to Pebble: " + latLong);
    sendBroadcast(i);
}


Receive Data from Pebble

B4X:
public class SportsAppBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent inboundIntent) {
        final String pebbleRecipient = inboundIntent.getStringExtra("recipient");
        if (! "MyApp".equals(pebbleRecipient))
            return;
 
        final String pebbleData = inboundIntent.getStringExtra("data");
 
        if ("PauseResume".equals(pebbleData)) {
            toggleActivityTimerPause();
        }
    }
}


Receive a Notification When Pebble is Connected

B4X:
public class PebbleConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String pebbleAddress = intent.getStringExtra("address");
        Log.i(TAG, "Pebble (%s) connected", pebbleAddress);
    }
}


Receive a Notification When Pebble is Disconnected

B4X:
public class PebbleConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String pebbleAddress = intent.getStringExtra("address");
        Log.i(TAG, "Pebble (%s) disconnected", pebbleAddress);
    }
}

Thank you.
 

salmander

Active Member
Licensed User
Longtime User
Thank you Erel. I haven't got my Pebble yet and will not be getting it for couple of months at least :(, but, just wanted to interpret the Pebble Android sdk to b4a.

cheers
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Thank you Erel. I haven't got my Pebble yet and will not be getting it for couple of months at least :(, but, just wanted to interpret the Pebble Android sdk to b4a.

cheers

I have one on order as well. If you or anyone figures out code for B4A, could we have an area for info on it. Just a manual if you will of commands. It'd be nice to not have to reinvent the wheel.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I tried, but that JSON crap (which shouldn't be needed at all) breaks it. I have no idea how to put the JSON data in a JSON array.

B4X:
Sub SendAlertToPebble(ThisAppsName As String, Header As String, Body As String)
   Dim I As Intent,P As Phone, JSON As String ' , JSON As JSONGenerator , M As Map , alist As List
   I.Initialize("com.getpebble.action.SEND_NOTIFICATION", "")
   'SendAlertToPebble: [{"body":"TEST","title":"LCARS"}]
   'alist.Initialize
   'M.Initialize 
   'M.put("title", Header)
   'M.put("body", Body)
   'alist.Add(M)
   'JSON.Initialize2(alist)
   JSON = "[{""body"":""" & Body & """,""title"":""" & Header & """}]"
   I.putExtra("messageType", "PEBBLE_ALERT")
   I.putExtra("sender", ThisAppsName)
   I.putExtra("notificationData", JSON )
   'Log("SendAlertToPebble: " & JSON.ToString)
   P.SendBroadcastIntent(I)
End Sub

No JSON library needed. Feel free to delete the commented-out code
 
Last edited:
Upvote 0

Jake E

New Member
Licensed User
Longtime User
Would I have to put this code in a class module? or a code module or could I use it on the activity?

public class SportsAppBroadcastReceiver extends BroadcastReceiver {
@Overridepublic void onReceive(Context context, Intent inboundIntent) {
final String pebbleRecipient = inboundIntent.getStringExtra("recipient");if (! "MyApp".equals(pebbleRecipient))return;

final String pebbleData = inboundIntent.getStringExtra("data");
if ("PauseResume".equals(pebbleData)) {
toggleActivityTimerPause();
}
}
}
 
Upvote 0
Top