0
我想通过一个简单的按更改变量的某个值Python中的按钮在我的应用我的智能手机和我的覆盆子PI 3蓝牙连接,在蟒蛇在Android(Java)的改变一些变量
我之间使用此Java代码来通过蓝牙从我的覆盆子PI获得价值:
private void startThreadConnected(BluetoothSocket socket){
myThreadConnected = new ThreadConnected(socket);
myThreadConnected.start();
}
/*
ThreadConnectBTdevice:
Background Thread to handle BlueTooth connecting
*/
private class ThreadConnectBTdevice extends Thread {
private BluetoothSocket bluetoothSocket = null;
private final BluetoothDevice bluetoothDevice;
Dialog dialog;
private ThreadConnectBTdevice(BluetoothDevice device, Dialog dialog) {
this.dialog = dialog;
bluetoothDevice = device;
connexion.setText("Déconnexion");
try {
bluetoothSocket = device.createRfcommSocketToServiceRecord(myUUID);
Toast.makeText(getApplicationContext(), "Connexion établi !", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
boolean success = false;
try {
bluetoothSocket.connect();
success = true;
} catch (IOException e) {
e.printStackTrace();
final String eMessage = e.getMessage();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "something wrong bluetoothSocket.connect(): \n" + eMessage, Toast.LENGTH_SHORT).show();
}
});
try {
bluetoothSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(success){
//connect successful
/* final String msgconnected = "connect successful:\n"
+ "BluetoothSocket: " + bluetoothSocket + "\n"
+ "BluetoothDevice: " + bluetoothDevice;*/
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
startThreadConnected(bluetoothSocket);
}else{
//fail
}
}
public void cancel() {
Toast.makeText(getApplicationContext(), "Déconnexion !", Toast.LENGTH_LONG).show();
try {
bluetoothSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class ThreadConnected extends Thread {
private final BluetoothSocket connectedBluetoothSocket;
private final InputStream connectedInputStream;
private final OutputStream connectedOutputStream;
boolean running;
public ThreadConnected(BluetoothSocket socket) {
connectedBluetoothSocket = socket;
InputStream in = null;
OutputStream out = null;
running = true;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connectedInputStream = in;
connectedOutputStream = out;
}
@Override
public void run() {
final byte[] buffer = new byte[1000000]; // buffer
final GraphView graph=(GraphView) findViewById(R.id.graph);
while (running) {
try {
final int bytes;
bytes=connectedInputStream.read(buffer);
final String strReceived = new String(buffer,0, bytes);
final String strByteCnt = String.valueOf(bytes) + " bytes received.\n";
final String terminale=new String(buffer,0,bytes);
runOnUiThread(new Runnable(){
@Override
public void run() {
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myThreadConnected.write(data like you want)
}});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
cancel();
final String msgConnectionLost = "Connection lost:\n" + e.getMessage();
runOnUiThread(new Runnable(){
@Override
public void run() {
}});
}
}
}
public void write(byte[] buffer) {
try {
connectedOutputStream.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void cancel() {
running = false;
try {
connectedBluetoothSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
而这种代码在python发送数据:
from bluetooth import*
a=5
#Bluetooth connection
server_sock=BluetoothSocket(RFCOMM)
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port=server_sock.getsockname()[1]
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
client_sock,client_info=server_sock.accept()
client_sock.send(a)
try
data=client_sock.recv(1024)
if len(data)==0: break
data2=float(data)
a=data2
print ("received [%f]" %a)
except IOError:
pass
现在我想从Java代码来查在Python中我的变量的值。我如何用相同的逻辑进行处理?
链接有用:http://android-er.blogspot.fr/2015/11/android-bluetooth-terminal.html
谢谢!
你必须接受你的蟒蛇在Android中端送什么。将它存储在Android中的一些新变量上,然后尝试将其发送回python方 – EsmaeelE
是的谢谢!我终于找到了。这很容易。 – azzerty2017