蓝牙:我需要知道设备的UUID? [英] Bluetooth: Do i need to know the UUID of the Devices?

查看:194
本文介绍了蓝牙:我需要知道设备的UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的Nexus 7设备符合我的Nexus 4设备连接,后来我想我的Nexus 7设备与微控制器连接。 我要知道UUID 我的设备使用蓝牙进行连接?

I want to connect my Nexus 7 device with my Nexus 4 device and later i want to connect my Nexus 7 device with a micro controller. Do i have to know the UUID of my devices to connect them using bluetooth?

是UUID的时候我我配对的设备正在交换?

Are the UUID's being exchanged when i pair my devices?

如果是:为什么会出现在android的例子定义的UUID

If yes: Why is there a defined UUID in android example?

public class BluetoothService extends Thread {
    private static final String TAG = BluetoothService.class.getSimpleName();
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    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 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() {
        Log.d(TAG, "start");

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

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

        this.setBluetoothState(STATE_LISTEN);
    }

    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;
        }

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

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

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

        this.handler.sendMessage(msg);
        this.setBluetoothState(STATE_CONNECTED);
    }

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

        if (this.connectedThread != null) {
            this.connectedThread.cancel();
            this.connectedThread = 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() {
        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() {
        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 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.createInsecureRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + "create() failed", e);
            }

            this.bluetoothSocket = tempBluetoothSocket;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectThread");

            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) {
            Log.d(TAG, "create ConnectedThread");

            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的,以便能够连接设备

If no: How to exchange the UUID's to be able to connect the devices?

推荐答案

在谷歌样品/示例中定义的UUID必须由服务器和客户端被称为:

The UUID defined in the google sample/example must be known by the server and the client :


  • 服务器创建一个RFCOMM的ServerSocket侦听与此UUID
  • 传入连接
  • 客户端创建一个RFCOMM的BluetoothSocket会尝试连接到服务器套接字

如果的UUID匹配连接建立

if the uuids matches the connection is established.

配对保存有关远程设备(名称,ADRESS,...等),所以,当你想再次连接,你不必搜索设备,让他们做的信息=)

The pairing saves the informations about the remote device (name, adress, ... etc) so that when you want to connect again you don't have to search for the device to get them =)

这篇关于蓝牙:我需要知道设备的UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆