Created
July 25, 2014 12:00
-
-
Save sudonhim/43d785617ba950c5e317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bluetooth as bt | |
import sys, time | |
from random import random | |
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" | |
matches = bt.find_service(uuid=uuid) | |
if len(matches) == 0: | |
raw_input("No matching service found!") | |
sys.exit(0) | |
print "%i matching services found! Taking first" %len(matches) | |
server = matches[0] | |
name = server["name"] | |
port = server["port"] | |
host = server["host"] | |
print "Connecting to %s on %s" %(name, host) | |
sock = bt.BluetoothSocket(bt.RFCOMM) | |
sock.connect((host, port)) | |
print "Connected. Data will be sent..." | |
data = [0.5 for x in range(5)]; | |
while True: | |
for i,d in enumerate(data): | |
data[i] = d + random()*0.02-0.01 + (0.5-d)/200 | |
sock.send(", ".join("%0.3f"%x for x in data)+"\n") | |
time.sleep(0.15); | |
sock.close() | |
raw_input("This session has concluded peacefully. Goodbye!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
private Button Connect; | |
private TextView SerialConsole; | |
private BluetoothAdapter BA; | |
private Set<BluetoothDevice>pairedDevices; | |
private BluetoothServerSocket bsvrsock; | |
private BluetoothSocket bsock; | |
private UUID uuid; | |
private String incomingdata; | |
private String newdataline; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Connect = (Button)findViewById(R.id.makeVisibleButton); | |
SerialConsole = (TextView)findViewById(R.id.serialStatus); | |
BA = BluetoothAdapter.getDefaultAdapter(); | |
uuid = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); | |
if (BA == null) finish(); | |
incomingdata = ""; | |
} | |
public void connect(View view){ | |
try { | |
bsvrsock = BA.listenUsingRfcommWithServiceRecord("Freeflow Bluetooth", uuid); | |
} catch (IOException e) { | |
Toast.makeText(getApplicationContext(),"Unable to create socket!" , | |
Toast.LENGTH_LONG).show(); | |
return; | |
} | |
try { | |
bsock = bsvrsock.accept(); | |
bsvrsock.close(); | |
} catch (IOException e) { | |
Toast.makeText(getApplicationContext(),"Socket accept failed!" , | |
Toast.LENGTH_LONG).show(); | |
return; | |
} | |
Toast.makeText(getApplicationContext(),"Connected!" , | |
Toast.LENGTH_LONG).show(); | |
SerialConsole.setText("Awaiting communication..."); | |
(new Timer()).schedule(new TimerTask() { | |
@Override | |
public void run() { | |
try { | |
InputStream inp = bsock.getInputStream(); | |
while (inp.available() > 0) { | |
char c = (char)inp.read(); | |
if (c == ("\n").charAt(0)) { | |
newdataline = incomingdata; | |
incomingdata = ""; | |
MainActivity.this.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
SerialConsole.setText("Received:\n"+newdataline); | |
} | |
}); | |
} | |
incomingdata = incomingdata + String.valueOf(c); | |
} | |
} catch (IOException e) { | |
MainActivity.this.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
SerialConsole.setText("Could not read from socket."); | |
} | |
}); | |
this.cancel(); | |
} | |
} | |
}, 500, 40); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code for connecting a Python client to an Android server using the pybluez bluetooth library.