Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rubotoを使い、AndroidでのGPS衛星情報をNMEA形式で取得する
require 'ruboto/widget'
ruboto_import_widgets :LinearLayout, :TextView
java_import 'android.content.Context'
java_import 'android.location.LocationManager'
class NmeaActivity
def onCreate(bundle)
super
@lm = getSystemService(Context::LOCATION_SERVICE)
@ll = MyLocationListener.new
set_title 'NMEA Example'
self.content_view = linear_layout orientation: :vertical do
linear_layout do
text_view text: 'QZGSV: '
@qzgsv_view = text_view text: ''
end
linear_layout do
text_view text: 'QZGSA: '
@qzgsa_view = text_view text: ''
end
linear_layout do
text_view text: 'GPRMC: '
@gpmrc_view = text_view text: ''
end
linear_layout do
text_view text: 'GNGSA: '
@gngsa_view = text_view text: ''
end
linear_layout do
text_view text: 'GPGSV: '
@gpgsv_view = text_view text: ''
end
linear_layout do
text_view text: 'GPGSA: '
@gpgsa_view = text_view text: ''
end
linear_layout do
text_view text: 'GPGGA: '
@gpgga_view = text_view text: ''
end
linear_layout do
text_view text: 'PGLOR: '
@pglor_view = text_view text: ''
end
linear_layout do
text_view text: 'Others: '
@others_view = text_view text: ''
end
end
end
def onResume
super
@lm.addNmeaListener(MyNMEA.new(self))
@lm.requestLocationUpdates(LocationManager::GPS_PROVIDER, 1000, 5, @ll)
end
def onPause
super
Thread.start do
@lm.removeUpdates @ll
@lm.removeNmeaListener @ll
end
end
def update_nmea(nmea)
case nmea.split(',')[0]
when '$QZGSV'
@qzgsv_view.text = nmea
when '$QZGSA'
@qzgsa_view.text = nmea
when '$GPRMC'
@gpmrc_view.text = nmea
when '$PGLOR'
@pglor_view.text = nmea
when '$GNGSA'
@gngsa_view.text = nmea
when '$GPGSV'
@gpgsv_view.text = nmea
when '$GPGSA'
@gpgsa_view.text = nmea
when '$GPGGA'
@gpgga_view.text = nmea
else
@others_view.text = nmea
end
end
end
class MyNMEA
def initialize(activity)
@activity = activity
end
def onNmeaReceived(timestamp, nmea)
@activity.update_nmea(nmea)
end
# Required on the Java side when registered
def hashCode
hash
end
end
class MyLocationListener
# Add "__ruby_object" "equlas" method for error avoidance
def __ruby_object
end
def equals(hoge)
end
# Required on the Java side when registered
def hashCode
hash
end
def onLocationChanged(location)
end
def onProviderDisabled(provider)
end
def onProviderEnabled(provider)
end
def onStatusChanged(provider, status, extras)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.