binding a android bluetooth server to a socket

Android 4.04(java) server to ubuntu 12.04(python) client with bluetooth (RFCOMM)

I'm currently trying to get a bluetooth communication between and android 4.04 galaxy taplet and linux computer with python (the computer will be running a ROS robot, and I need the tablet for GPS and INS data). I need the taplet to function as a server. Both drivers support RFCOMM, but the python option connects based on "Address" and "Port" where the java android connects on "UUID".

I'm currently looking for a way to give my android server a static port number, but I'm also open for other ways to solve the problem. I have been stuck here for quite a while and have found lots of simular issues but none which solved my case, hope you guys can help me out.

Question: how can I bind my android bluetooth server to a socket ?

Ps: forgive that the code android code isen't perfect it have been modified quite a few times looking for workarounds

My current android code:

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) { }
        }
    }
}

My current python code:

#!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()

Note that the python code finds the device in both searches, returns its address correctly, and concludes the port and name to be none. I then get an error in sock.connect(addr,port) since port carnt be none.

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

上一篇: 用usbManager android无法看到鼠标和键盘设备

下一篇: 将android蓝牙服务器绑定到套接字