Skip to content

Instantly share code, notes, and snippets.

@shifei010
Created October 7, 2012 03:11
Show Gist options
  • Save shifei010/3846970 to your computer and use it in GitHub Desktop.
Save shifei010/3846970 to your computer and use it in GitHub Desktop.
sl4a Androino
<html>
<head>
<title>Androino</title>
<style>
body {
background: #32323D;
}
div#content {
padding-top:100px;
width:150px;
margin:auto;
}
div#output {
border:1px solid black;
background:white;
text-align:center;
}
</style>
<script>
var droid = new Android();
var fetch_data = function() {
// fire the event that the python script is waiting for.
droid.eventPost('fetch_data', '');
}
// this function gets called from the python script when it receives new sensor data.
droid.registerCallback('display_data', function(data) {
document.getElementById('output').innerHTML = 'Sensor value: ' + data.data;
});
</script>
</head>
<body>
<div id="content">
<div id="output">-</div>
<button onclick="fetch_data();">get sensor data</button>
</div>
</body>
</html>
import android
# Change the BT_DEVICE_ID in androino.py to the address of your BT module.
# If set to None, Android will ask you at program start which bluetooth device to use.
BT_DEVICE_ID = '00:11:11:18:62:60'
droid = android.Android()
"""The first parameter is the service UUID for SerialPortServiceClass.
The second one is the address of your bluetooth module.
If the second one is ommited, Android shows you a selection at program start.
When this function succeeds the little led on the bluetooth module should stop blinking.
"""
droid.bluetoothConnect('00001101-0000-1000-8000-00805F9B34FB', BT_DEVICE_ID)
droid.webViewShow('file:///sdcard/sl4a/scripts/androino.html')
while True:
result = droid.eventWaitFor('fetch_data').result
droid.bluetoothWrite(' ') # send a space to your arduino to signal it to read a value from the sensor.
sensor_data = droid.bluetoothReadLine().result # read the line with the sensor value from arduino.
droid.eventClearBuffer() # workaround for a bug in SL4A r4.
droid.eventPost('display_data', sensor_data) # send an event with the sensor data back to the html page.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment