蓝牙多个UUID

时间:2015-02-16 09:32:52

标签: android

我实现了BluetoothService,我可以在两台设备之间进行连接。下一步是连接到具有不同UUID的设备。我有三个设备:

  • Nexus 7
  • Nexus 4
  • 微控制器

Nexus 7应该能够连接到Nexus 4和Microncontroller。 Nexus 4和微控制器都有不同的UUID。无需同时打开与两个设备的连接。只需1:1连接。

Nexus 7正在打开BluetoothServerSocket,因此它不需要两个UUID。

我需要做些什么才能检查是否是Nexus 4的UUID或尝试连接的微控制器的UUID?不幸的是我不能做这样的事情:

tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_NEXUS_UUID);
tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_MICRO_UUID);

听取UUID的所有内容。连接也是一样的:

tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_NEXUS_UUID | MY_MICRO_UUID);

修改

我使用以下BluetoothService

public class BluetoothService extends Thread {
    private static final String TAG = BluetoothService.class.getSimpleName();
    private static final String NAME = "My_App";
    private static final UUID MY_UUID = UUID.fromString("d0c722b0-7e15-11e1-b0c4-0800200c9a66");

    public static final int STATE_NONE = 0;  
    public static final int STATE_LISTEN = 1;
    public static final int STATE_CONNECTING = 2;
    public static final int STATE_CONNECTED = 3;

    private BluetoothAdapter bluetoothAdapter = null;
    private Handler handler = null;
    private AcceptThread acceptThread = null;
    private ConnectThread connectThread = null;
    private ConnectedThread connectedThread = null;
    private int bluetoothState = STATE_NONE;

    public BluetoothService(Handler handler) {
        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        this.bluetoothState = STATE_NONE;
        this.handler = handler;
    }

    public synchronized void startConnection() {
        if (this.connectThread != null) {
            this.connectThread.cancel();
            this.connectThread = null;
        }

        if (this.connectedThread != null) {
            this.connectedThread.cancel();
            this.connectedThread = null;
        }

        this.setBluetoothState(STATE_LISTEN);

        if (this.acceptThread == null) {
            this.acceptThread = new AcceptThread();
            this.acceptThread.start();
        }
    }

    public synchronized void connect(BluetoothDevice device) {
        if (this.bluetoothState == STATE_CONNECTING) {
            if (this.connectThread != null) {
                this.connectThread.cancel();
                this.connectThread = null;
            }
        }

        if (this.connectedThread != null) {
            this.connectedThread.cancel();
            this.connectedThread = null;
        }

        this.connectThread = new ConnectThread(device);
        this.connectThread.start();

        this.setBluetoothState(STATE_CONNECTING);
    }

    public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
        if (this.connectThread != null) {
            this.connectThread.cancel();
            this.connectThread = null;
        }

        if (this.connectedThread != null) {
            this.connectedThread.cancel();
            this.connectedThread = null;
        }

        if (this.acceptThread != null) {
            this.acceptThread.cancel(); 
            this.acceptThread = null;
        }

        this.connectedThread = new ConnectedThread(socket);
        this.connectedThread.start();

        this.setBluetoothState(STATE_CONNECTED);
        Message msg = this.handler.obtainMessage(Globals.MESSAGE_DEVICE_NAME);

        Bundle bundle = new Bundle();

        bundle.putString(Globals.EXTRA_DEVICE_NAME, device.getName());
        msg.setData(bundle);

        this.handler.sendMessage(msg);
    }

    public synchronized void stopConnection() {
        if (this.connectThread != null) {
            this.connectThread.cancel();
            this.connectThread = null;
        }

        if (this.connectedThread != null) {
            this.connectedThread.cancel();
            this.connectedThread = null;
        }

        if (this.acceptThread != null) {
            this.acceptThread.cancel(); 
            this.acceptThread = null;
        }

        this.setBluetoothState(STATE_NONE);
    }

    public void write(byte[] out) {
        ConnectedThread connectedThread = null;

        synchronized (this) {
            if (this.bluetoothState != STATE_CONNECTED) {
                return;
            }

            connectedThread = this.connectedThread;
        }

        connectedThread.write(out);
    }

    private void connectionFailed() {
        this.setBluetoothState(STATE_LISTEN);

        Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);

        Bundle bundle = new Bundle();
        bundle.putString(Globals.TOAST, "Unable to connect device");

        msg.setData(bundle);

        this.handler.sendMessage(msg);

        BluetoothService.this.startConnection();
    }

    private void connectionLost() {
        this.setBluetoothState(STATE_LISTEN);

        Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
        Bundle bundle = new Bundle();

        bundle.putString(Globals.TOAST, "Device connection was lost");
        msg.setData(bundle);

        this.handler.sendMessage(msg);

        BluetoothService.this.startConnection();
    }

    public synchronized int getBluetoothState() {
        return this.bluetoothState;
    }

    private synchronized void setBluetoothState(int bluetoothState) {
        this.bluetoothState = bluetoothState;
    }

    private class AcceptThread extends Thread {
        private BluetoothServerSocket serverSocket = null;

        public AcceptThread() {
            BluetoothServerSocket tempBluetoothServerSocket = null;

            try {
                tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "listen() failed", e);
            }

            this.serverSocket = tempBluetoothServerSocket;
        }

        public void run() {
            this.setName("AcceptThread");

            BluetoothSocket socket = null;

            while (bluetoothState != STATE_CONNECTED) {
                try {
                    socket = this.serverSocket.accept();
                } catch (IOException e) {
                    Log.e(TAG, "accept() failed", e);

                    break;
                }

                if (socket != null) {
                    synchronized (BluetoothService.this) {
                        switch (bluetoothState) {
                        case STATE_LISTEN:
                        case STATE_CONNECTING:
                            connected(socket, socket.getRemoteDevice());

                            break;
                        case STATE_NONE:
                        case STATE_CONNECTED:
                            try {
                                socket.close();
                            } catch (IOException e) {
                                Log.e(TAG, "Could not close unwanted socket", e);
                            }

                            break;
                        }
                    }
                }
            }
        }

        public void cancel() {
            try {
                this.serverSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of server failed", e);
            }
        }
    }

    private class ConnectThread extends Thread {
        private BluetoothSocket bluetoothSocket = null;
        private BluetoothDevice bluetoothDevice = null;

        public ConnectThread(BluetoothDevice bluetoothDevice) {
            this.bluetoothDevice = bluetoothDevice;

            BluetoothSocket tempBluetoothSocket = null;

            try {
                tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + "create() failed", e);
            }

            this.bluetoothSocket = tempBluetoothSocket;
        }

        public void run() {
            this.setName("ConnectThread");

            bluetoothAdapter.cancelDiscovery();

            try {
                this.bluetoothSocket.connect();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage(), e);

                connectionFailed();

                try {
                    this.bluetoothSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() socket during connection failure", e2);
                }

                return;
            }

            synchronized (BluetoothService.this) {
                connectThread = null;
            }

            connected(this.bluetoothSocket, this.bluetoothDevice);
        }

        public void cancel() {
            try {
                this.bluetoothSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }

    private class ConnectedThread extends Thread {
        private BluetoothSocket bluetoothSocket = null;
        private InputStream inputStream = null;
        private OutputStream outputStream = null;

        public ConnectedThread(BluetoothSocket bluetoothSocket) {
            this.bluetoothSocket = bluetoothSocket;

            InputStream tempInputStream = null;
            OutputStream tempOutputStream = null;

            try {
                tempInputStream = this.bluetoothSocket.getInputStream();
                tempOutputStream = this.bluetoothSocket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            this.inputStream = tempInputStream;
            this.outputStream = tempOutputStream;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            int bytes = 0;

            while (true) {
                try {
                    bytes = this.inputStream.read(buffer);

                    handler.obtainMessage(Globals.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);

                    connectionLost();

                    BluetoothService.this.start();

                    break;
                }
            }
        }

        public void write(byte[] buffer) {
            try {
                this.outputStream.write(buffer);

                handler.obtainMessage(Globals.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void cancel() {
            try {
                this.bluetoothSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

UUIDS 适用于服务,不适用于设备。 这回答了你的两个问题。您可以通过 BluetoothAddress

识别 BluetoothDevice