Re: Some tests for BT and GPS in Kivy Android

2,373 views
Skip to first unread message

Mathieu Virbel

unread,
Nov 6, 2012, 9:05:09 AM11/6/12
to am...@prodeo.es, kivy-...@googlegroups.com, m...@kivy.org

Hi,

Thanks for sharing this work, maybe someone could clean it and merge it into python-for-android, then add it in the pyjnius documentation?

Anyway, that's cool to see a working GPS example :)

Good work!

Mathieu

Le 5 nov. 2012 17:48, "Angel Maza" <am...@prodeo.es> a écrit :
Hello Mathieu.

I'm Angel, and I'm a happy boy since using Kivy.

I've seen kivy about 1 year, but I haven't used because of lack of BT and GPS for my android (and future iOs) projects....

But this is old problem, in new version we can use both, thanks to your work (and friends...)   ;-)

I've seen pyjnius and it's promissing....

Some tests I've done (and working):

FOR BT (without using java at all):


# Bluetooth part
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
UUID = autoclass('java.util.UUID')


btAdapter = BluetoothAdapter.getDefaultAdapter()
if  btAdapter.isEnabled():
    print 'enabled'
    btdevices = btAdapter.getBondedDevices()

    for i in btdevices.toArray():
        a = i.getAddress()
        device = btAdapter.getRemoteDevice( a )
        socket = device.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB"  )  )
        try:
            socket.connect()
        except:
            print 'BAD connection'
else:
    # We don't have bt, try to enable and later re-try using...
    print 'disabled'
    intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
    currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
    currentActivity.startActivity(intent)
    #intent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
    #currentActivity.startActivity(intent)







For GPS (some harder, excuse my programming, I'm a dumb java programmer)
KivyGps.java (in renpy/android as you say in doc)

//import java.util.ArrayList;
//import java.util.List;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import java.lang.Thread;
import android.app.Activity;

public class KivyGps {
    LocationManager lm;
    Thread gpsThread;
    public long minDistance = 1;
    public int  minTime = 1000;


   static class KivyLocationListener implements LocationListener {

public Location lastLocation = new Location("Other");
//private List<LocationListener> listeners = new ArrayList<LocationListener>();
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
lastLocation = location;
//updateListeners(location);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

public Location getCurrentLocation() {
// TODO Auto-generated method stub
return lastLocation;
}
    }

    static public KivyLocationListener locationListener = new KivyLocationListener();
    public Thread init(final Activity currActivity) {
    
        gpsThread = new Thread( new Runnable() {
    
          public void run() {
            try {
                Looper.prepare();
                 lm = (LocationManager) currActivity.getSystemService( Context.LOCATION_SERVICE );
                 lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener );
                 Looper.loop();
                
                }
            catch ( Exception e ) {
                e.printStackTrace();
            }
          }
    
        } );
        return gpsThread;
    
    }
    //gpsThread.start();



}


Using in python:
LocationListener = autoclass('android.location.LocationListener')
LocationManager = autoclass('android.location.LocationManager')
LocationProvider = autoclass('android.location.LocationProvider')
Location = autoclass('android.location.Location')
Looper = autoclass('android.os.Looper')
Context = autoclass('android.content.Context')
KivyGps = autoclass('org.renpy.android.KivyGps')

currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
lm = currentActivity.getSystemService( Context.LOCATION_SERVICE)
if lm.isProviderEnabled( LocationManager.GPS_PROVIDER ):
    print 'CON GPS'
  
else:
    print 'SIN GPS'


lps = lm.getAllProviders()
for lp in lps.toArray():
    print lp
#Arreglar problema de derechos ACCESS_FINE_LOCATION en Kivy Launcher
lp = lm.getProvider('gps')

ll = KivyGps.locationListener
kgps = KivyGps()
gpsThread = kgps.init( currentActivity )
gpsThread.start()

loc = ll.getCurrentLocation()
if loc:
    print loc.getLatitude()
    print loc.getLongitude()


Best Regards.
Angel Maza.


T500

unread,
Nov 6, 2012, 7:52:09 PM11/6/12
to kivy-...@googlegroups.com, am...@prodeo.es, m...@kivy.org
Very cool. Dear Santa can I add this to my Christmas wishlist:)
This would be great in kivy android!

I have one dumb question, UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB"  ) 
...would this not vary according to model or type of module fitted?

am...@prodeo.es

unread,
Nov 11, 2012, 1:31:34 PM11/11/12
to kivy-...@googlegroups.com, am...@prodeo.es, m...@kivy.org
There is some bug in the android system using bluetooth SPP with BT modules in market...
For bypass do something like this:
(the trick is using startDiscovery and sleep a little, I'm using 2 seconds and works fine, but I haven't tested lesser time. I've seen the trick, but not documented, in Bluetooth facade SL4A. So discovering simplifies bt device connection, without this you has Connection Error)



BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
UUID = autoclass('java.util.UUID')

btAdapter = BluetoothAdapter.getDefaultAdapter()
if  btAdapter.isEnabled():
    btAdapter.startDiscovery()
    btdevices = btAdapter.getBondedDevices()
    print dir(btdevices)
    import time
    time.sleep(2)
    
    
    for i in btdevices.toArray():
            # select device you want to connect.... by name or by address, this example works with last device.
            socket = i.createRfcommSocketToServiceRecord( UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB"  )  )
            try:
                btAdapter.cancelDiscovery()
                socket.connect()
                istream = socket.getInputStream()
            except:
                print 'BAD trying connection'

Nurlan Nabiev

unread,
May 27, 2013, 1:16:05 AM5/27/13
to kivy-...@googlegroups.com, am...@prodeo.es, m...@kivy.org
Hi Angel! Thank you for your code. Im new in KIVY and python-android. I need to make simple app for android, which shows GPS coordinates. But, as i said, i'm new in kivy and pyforandroid. Can you show/give me example,using your GPScode above, which shows my coordinates in simple kivy-label-widget?

Thanks a LOT!

понедельник, 12 ноября 2012 г., 0:31:34 UTC+6 пользователь am...@prodeo.es написал:

Nurlan Nabiev

unread,
Jun 4, 2013, 6:36:58 AM6/4/13
to kivy-...@googlegroups.com, am...@prodeo.es, m...@kivy.org
Hello! Is there anybody?


понедельник, 12 ноября 2012 г., 0:31:34 UTC+6 пользователь am...@prodeo.es написал:
There is some bug in the android system using bluetooth SPP with BT modules in market...

Akshay Arora

unread,
Jun 4, 2013, 1:53:06 PM6/4/13
to kivy-...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Нурлан Набиев

unread,
Jun 5, 2013, 2:27:36 AM6/5/13
to kivy-...@googlegroups.com
Thanks a lot! God bless you!


2013/6/4 Akshay Arora <akshay...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/n_cMbFzf_1A/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Успехов в благих начинаниях!
Набиев Нурлан, компания "Raindrop"

www.raindrop.kz
+7 (705) 575 48 28
+7 (7272) 72 70 92

Нурлан Набиев

unread,
Jun 5, 2013, 6:29:56 AM6/5/13
to kivy-...@googlegroups.com
I ve installed on my Samsung gt-3100 with Android 4.1.2. But, when i do press on "gps-location" in menu of application, application stops and switches off...
Any ideas? =)


2013/6/4 Akshay Arora <akshay...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/n_cMbFzf_1A/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Akshay Arora

unread,
Jun 7, 2013, 5:29:19 AM6/7/13
to kivy-...@googlegroups.com
Try to see what error you get in the log using adb logcat

Нурлан Набиев

unread,
Jun 8, 2013, 6:55:11 AM6/8/13
to kivy-...@googlegroups.com
Hi!
where i need to launch this command? Can you give a source or link about debugging in kivy/pyforandroid/pyjnius?

Thanks!


2013/6/7 Akshay Arora <akshay...@gmail.com>

Akshay Arora

unread,
Jun 8, 2013, 11:44:42 AM6/8/13
to kivy-...@googlegroups.com
http://forum.xda-developers.com/showthread.php?t=1726238 this is a very usefull link that explain different options.
You can either different apps like alogcat etc to access log directly on your phone or use the adb tool provided with android sdk to run adb logcat http://developer.android.com/tools/help/logcat.html
Reply all
Reply to author
Forward
0 new messages