Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 蓝牙多UUID';s_Android - Fatal编程技术网

Android 蓝牙多UUID';s

Android 蓝牙多UUID';s,android,Android,我实现了一个BluetoothService,可以在两个设备之间连接。下一步是连接到具有不同UUID的设备。我有三种设备: Nexus 7 Nexus 4 微控制器 Nexus 7应该能够连接到Nexus 4和Micronus控制器。Nexus 4和微控制器都有不同的UUID。不必同时打开与两个设备的连接。只需要1:1的连接 Nexus7正在打开BluetoothServerSocket,因此它需要同时关闭两个UUID 我要做什么才能检查是Nexus 4的UUID还是微控制器的UUID试图连

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

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

Nexus7正在打开
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);
            }
        }
    }
}

uuid用于服务而不是设备
这回答了你的两个问题。你可以通过我第一次读到它的蓝牙地址来识别蓝牙设备。我阅读的所有蓝牙教程都使用UUID在设备之间进行连接。那么,如何使用BluetoothAddress而不是UUID连接到设备?我只需要从我的Nexus7发送一些消息到我的Nexus4,或者发送到我的微控制器UUID来连接,没错。因为您与服务建立了连接。BluetoothDevice类为不同的设备提供了一个唯一的对象(按地址区分)。您必须在BluetoothAdapter类的方法getBluetoothDevice(“address”)中提供一个地址,才能获得唯一的设备。UUID是服务的,我还是不明白。我需要连接到设备,我需要连接到服务??我能够建立从nexus 7到nexus 4的连接这不是问题,但我必须在nexus 7和nexus 4上使用相同的UUID。我的微控制器有另一个uuid,因此我无法使用当前代码连接到它。我添加了我使用的Bluetooth服务。android的Bluetooth聊天示例也正在使用UUID。从来没有像设备地址这样的东西。