Android蓝牙通信开发总结

Android手机蓝牙通讯开发步骤:
1.搜索蓝牙(BlueAdapter)
2.蓝牙配对
3.配对成功后,建立socket通讯

注意事项:
1.蓝牙配对成功,需要在socket连接成功后,才能进行蓝牙的通讯
2.建立socket通讯有俩种方法:一种是通过uuid,另外一种通过反射的方法
uuid方法代码:

 private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
 btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

反射方法代码:

Method m;
 m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
 btSocket = (BluetoothSocket) m.invoke(device, 1);

3.保持蓝牙长连接,建议使用service,在退出程序时,一定要关闭service。
4.蓝牙消息的传递,建议多使用通知。

service核心代码

 public void doJob( ){
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            DisplayToast("蓝牙设备不可用,请打开蓝牙!");
            bluetoothFlag  = false;
            return;
        }

        if (!mBluetoothAdapter.isEnabled()) {
            DisplayToast("请打开蓝牙并重新运行程序!");
            bluetoothFlag  = false;
            stopService();
            showToast("请打开蓝牙并重新运行程序!");
            return;
        }
       // showToast("搜索到蓝牙设备!");
        threadFlag = true;
        myThread = new MyThread();
        myThread.start();

    }
    public  void connectDevice(){
        DisplayToast("正在尝试连接蓝牙设备,请稍后····");
//        boolean ispair=BlueToothUtils.pair(address,"123456");
//        if (!ispair)
//            return;
        DisplayToast("蓝牙设备成功配对!");
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        //try {
           // btSocket= mBluetoothAdapter.listenUsingRfcommWithServiceRecord("11", MY_UUID);
            //btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);此种方法建立socket无法连接
            Method m;
            try {

                m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                try {
                    btSocket = (BluetoothSocket) m.invoke(device, 1);
                    //_bluetooth.cancelDiscovery();
                    try {
                        btSocket.connect();
                        DisplayToast("连接成功建立,可以开始操控了!");
                      //  showToast("连接成功建立,可以开始操控了!");
                        bluetoothFlag = true;
                        socketisconnet(bluetoothFlag);
                    } catch (IOException e) {
                        try {
                            btSocket.close();
                            bluetoothFlag = false;
                            socketisconnet(bluetoothFlag);
                        } catch (IOException e2) {
                            DisplayToast("连接没有建立,无法关闭套接字!");
                        }
                    }
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    DisplayToast("套接字创建失败!");
                    bluetoothFlag = false;
                    socketisconnet(bluetoothFlag);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    DisplayToast("套接字创建失败!");
                    bluetoothFlag = false;
                    socketisconnet(bluetoothFlag);
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    DisplayToast("套接字创建失败!");
                    bluetoothFlag = false;
                    socketisconnet(bluetoothFlag);
                }
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                DisplayToast("套接字创建失败!");
                bluetoothFlag = false;
                socketisconnet(bluetoothFlag);
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                DisplayToast("套接字创建失败!");
                bluetoothFlag = false;
                socketisconnet(bluetoothFlag);

            }
        DisplayToast("成功连接蓝牙设备!");
        mBluetoothAdapter.cancelDiscovery();
        if(bluetoothFlag){
            try {
                inStream = btSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            } //绑定读接口

            try {
                outStream = btSocket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            } //绑定写接口

        }
    }

你可能感兴趣的:(Android)