1. Download the free trial version

[Pebble Watch]How to do this in b4a?

Discussion in 'Questions' started by salmander, Jan 23, 2013.

  1. salmander

    salmander Active Member Licensed 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

    Code:
    public void sendAlertToPebble() {
        final 
    Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
     
        final 
    Map<StringObject> data = new HashMap<StringObject>();
        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

    Code:
    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

    Code:
    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

    Code:
    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

    Code:
    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.
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. salmander

    salmander Active Member Licensed 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
  4. wheretheidivides

    wheretheidivides Active Member Licensed User

    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.
  5. NeoTechni

    NeoTechni Well-Known Member Licensed 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.

    Code:
    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: May 3, 2013
  6. Jake E

    Jake E New Member Licensed 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();
    }
    }
    }

Share This Page

Loading...