Android 4.04(java)服务器到ubuntu 12.04(python)带蓝牙(RFCOMM)客户端
我目前正在尝试在android4.04galaxyaltaplet和linux计算机之间使用python进行蓝牙通信(该计算机将运行ROS机器人,我需要用于GPS和INS数据的平板电脑)。我需要小龙头作为服务器。这两个驱动程序都支持RFCOMM,但是python选项基于javaandroid在“UUID”上连接的“地址”和“端口”进行连接。在
我目前正在寻找一种方法给我的android服务器一个静态端口号,但我也对其他解决问题的方法持开放态度。 我在这里呆了很长时间,发现了很多类似的问题,但都没有解决我的问题,希望你们能帮我解决。在
问:如何将android蓝牙服务器绑定到插座?在
Ps:请原谅android代码并不完美,它已经被修改了很多次以寻找解决方案
我当前的android代码:
public class MainActivity extends Activity{
ArrayAdapter<String> listadapter;
ListView listView;
BluetoothAdapter mBluetoothAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
BroadcastReceiver receiver;
AcceptThread acceptThread;
public static final UUID MY_UUID = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
acceptThread = new AcceptThread();
acceptThread.start();
if(mBluetoothAdapter == null){
Toast.makeText(getApplicationContext(), "No bluetooth detected", 0).show();
finish();
}
else{
if(!mBluetoothAdapter.isEnabled()){
turnOnBluetooth();
}
getPairedDevices();
startDiscorevy();
}
}
private void startDiscorevy() {
mBluetoothAdapter.cancelDiscovery();
mBluetoothAdapter.startDiscovery();
}
private void turnOnBluetooth() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
private void getPairedDevices() {
devicesArray = mBluetoothAdapter.getBondedDevices();
if(devicesArray.size()>0){
for(BluetoothDevice device:devicesArray){
pairedDevices.add(device.getName()+"\n"+device.getAddress());
}
}
}
private void init(){
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 600);
startActivity(discoverableIntent);
listView = (ListView) findViewById(R.id.listView);
listadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
listView.setAdapter(listadapter);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
pairedDevices = new ArrayList<String>();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Bluetooth must be enable to continue", 0).show();
finish();
}
}
@Override
protected void onPause() {
super.onPause();
acceptThread.cancel();
}
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("Location_Service", MY_UUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
if (socket != null) {
manageConnectedSocket(socket);
try {
mmServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
private void manageConnectedSocket(BluetoothSocket socket) {
OutputStream mmOutStream;
BluetoothSocket mmSocket;
mmSocket = socket;
OutputStream tmpOut = null;
try {
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) { }
mmOutStream = tmpOut;
byte[] bytes = null;
String toSend = "hello world";
bytes = toSend.getBytes();
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
try {
mmSocket.close();
} catch (IOException e) { }
}
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
}
我当前的python代码:
^{pr2}$请注意,python代码在这两个搜索中都会找到设备,并正确返回其地址,并将端口和名称断定为none。然后我在插座连接(地址,港口)因为港口没有港口。在
目前没有回答
相关问题 更多 >
编程相关推荐