AS蓝牙通信

1,首先开启蓝牙

2,搜索可用设备

3,创建蓝牙socket,获取输入输出流

4,读取和写入数据

5,断开连接关闭蓝牙

服务端

public class AcceptThread extends Thread {
    private static final String NAME ="LSY";
    private static final UUID MY_UUID =UUID.fromString("298a34ea-3e59-43c5-9128-48834a7a6cf4") ;
    private final BluetoothServerSocket mmServerSocket;
    private BluetoothAdapter bluetoothAdapter;
    private final Handler mHandler;
    private ConnectedThread mConnectedThread;


    public AcceptThread(Handler UIhandler) {
        // Use a temporary object that is later assigned to mmServerSocket
        // because mmServerSocket is final.
        mHandler = UIhandler;

        BluetoothServerSocket tmp = null;
        bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

        try {
            // MY_UUID is the app's UUID string, also used by the client code.

            tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "Socket's listen() method failed", e);
        }
        mmServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned.
        while (true) {
            try {
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "Socket's accept() method failed", e);
                break;
            }

            if (socket != null) {
                // A connection was accepted. Perform work associated with
                // the connection in a separate thread.
                try {
                    manageMyConnectedSocket(socket);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    mHandler.setEncoding("lsy");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    private void manageMyConnectedSocket(BluetoothSocket socket) throws UnsupportedEncodingException{

        mHandler.setEncoding("Constant.MSG_CONNECTED_TO_SERVER");
        mConnectedThread = new ConnectedThread(socket,mHandler);
        mConnectedThread.start();


    }


    // Closes the connect socket and causes the thread to finish.
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "Could not close the connect socket", e);
        }
    }

    public void sendData(byte[] data) {
        if(mConnectedThread != null) {
            mConnectedThread.write(data);
        }
    }

}

客户端

public class ConnectThread extends Thread{
    private static Object Constant;
    private static final UUID MY_UUID =UUID.fromString("298a34ea-3e59-43c5-9128-48834a7a6cf4") ;
    private BluetoothSocket mmSoket;
    private  BluetoothDevice mmDevice;
    private BluetoothAdapter mBluetoothAdapter;
    private Handler mHandler;
    private ConnectedThread mConnectedThread;
    public ConnectThread(BluetoothDevice device, BluetoothAdapter bluetoothAdapter, Handler mUIhandler) {
        mmDevice = device;
        mBluetoothAdapter = bluetoothAdapter;
        mHandler = mUIhandler;
        BluetoothSocket tmp = null;
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mmSoket = tmp;
    }
    @Override
    public void run() {
        super.run();
        mBluetoothAdapter.cancelDiscovery();
        try {
            mmSoket.connect();
        } catch (IOException e)
        {
            try {
                mmSoket.close();
            } catch (IOException e1) { }return;
        }
        manageConnectedSocket(mmSoket);
    }

    private void manageConnectedSocket(BluetoothSocket mmSoket) {
        try {
            mHandler.setEncoding("lsy");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        mConnectedThread = new ConnectedThread(mmSoket,mHandler);
        mConnectedThread.start();
    }
    public void cancle() {
        try {
            mmSoket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void sendData(byte[] data) {
        if(mConnectedThread != null) {
            mConnectedThread.write(data);
        }
    }
}

共同通讯处理类

public class ConnectedThread extends Thread{
    private final BluetoothSocket mmSokcet;
    private final InputStream mmInputStream;
    private final OutputStream mmOutputStream;
    private Handler mHandler;
    private String TAG ="ConnectedThread";
    private byte[][] newbyte;

    public ConnectedThread(BluetoothSocket socket,Handler handler) {
        mmSokcet = socket; mHandler = handler;
        InputStream tmpIn =null;
        OutputStream tmpOut =null;
        try{
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        }catch(IOException e) {
            e.printStackTrace();
        }
        mmInputStream = tmpIn;
        mmOutputStream = tmpOut;
    }

    @Override
    public void run() {

            super.run();
            byte[] buffer =newbyte[1024];
            while(true) {
                try{
                    int bytes = mmInputStream.read(buffer);
                    if(bytes >0) {
                        String data =new String(buffer,0,bytes,"utf-8");
                        mHandler.setEncoding("lsy");
                    }
                    Log.d(TAG,"messge size :"+ bytes);
                }catch(IOException e)
                { e.printStackTrace(); }
            }
    }


    public void cancle() {
        try{
            mmSokcet.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    public void write(byte[] data) {
        try{
            mmOutputStream.write(data);
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值