将android蓝牙服务器绑定到套接字

Android 4.04(java)服务器到ubuntu 12.04(python)客户端与蓝牙(RFCOMM)

我目前正在尝试与python(电脑将运行ROS机器人,我需要用于GPS和INS数据的平板电脑)之间的蓝牙通信和Android 4.04银河taplet和Linux计算机。 我需要taplet作为服务器。 两个驱动程序都支持RFCOMM,但python选项基于java android在“UUID”上连接的“Address”和“Port”进行连接。

我目前正在寻找一种方法来给我的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代码:

#!python 
import bluetooth 
from bluetooth import *  
import sys 

bluetooth_name = "GT-P5110" 
bluetooth_address = None 
uuid = None 

print "looking for device" 
nearby_devices = bluetooth.discover_devices() 
for bdaddr in nearby_devices: 
if bluetooth_name == bluetooth.lookup_name( bdaddr): 
    bluetooth_address == bdaddr 
    print "device found on address:" 
    print bdaddr 

service_matches = find_service(uuid = uuid, address = bluetooth_address)  

first_match = service_matches[0] 
port = first_match["port"] 
name = first_match["name"] 
host = first_match["host"] 

print " --- " 
print port 
print name 
print host 
print " --- " 

sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM) 
sock.connect((bluetooth_address, port)) 

sock.close()

请注意,python代码在两次搜索中找到设备,并正确返回其地址,并将端口和名称归为无。 然后我在sock.connect(addr,port)中得到一个错误,因为port carnt没有。

链接地址: http://www.djcxy.com/p/58313.html

上一篇: binding a android bluetooth server to a socket

下一篇: How can we know if iPad is using bluetooth keyboard or device virtual keyboard?