经典蓝牙连接(安卓与安卓连接)(未解决)

该篇只讲解到经典蓝牙的连接和通信。
注意相关传限要加好。
从ui界面开始设计逻辑代码;
ui界面以及其VIew的相关属性已贴出;
这里写图片描述
打开蓝牙;
1;先判断该设备是否支持蓝牙
2;打开蓝牙;
这里写图片描述
关闭蓝牙
1;关闭蓝牙;
这里写图片描述
搜索蓝牙;
1;利用广播
这里写图片描述

注意;
1;在这之前,我们需要startDiscovery()方法是一个异步方法,它会对其他蓝牙设备进行搜索,持续时间为12public void findBlue(View v) {  
        setProgressBarIndeterminateVisibility(true);  
        setTitle("正在扫描....");  
        // 如果正在搜索,就先取消搜索  
        if (mBluetoothAdapter.isDiscovering()) {  
            mBluetoothAdapter.cancelDiscovery();  
        }  
        // 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回  
        mBluetoothAdapter.startDiscovery();  
    }  
2;广播需要注册文件
    // 注册用以接收到已搜索到的蓝牙设备的receiver  
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver, filter);
3;在activity结束时要注销;
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();  
        //解除注册  
        unregisterReceiver(mReceiver);  
    } 

listview;
这里写图片描述
获取已匹配设备;
这里写图片描述
获取可连接设备
这里写图片描述
对两个listview进行操作;
这里写图片描述

对可连接设备进行匹配
这里写图片描述

进行连接;
这里写图片描述
这里写图片描述
方案一;利用反射进行客服端连接(成功)

public class ConnectThread extends Thread {

    private final BluetoothDevice mmdevice;
    private final BluetoothSocket mmSocket;
    private Boolean isConnect  = false;

    public ConnectThread(BluetoothDevice device){
        BluetoothSocket tmp = null;
        //赋值给设备
        mmdevice = device;
        try {
         Method m = mBluetoothDevice.getClass().getMethod(  
                    "createRfcommSocket", new Class[] { int.class });  
                temp = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);//这里端口为1  

        } catch(IOException e) {}
        //赋值给BluetoothSocket
        mmSocket = tmp;
    }

    public void run(){
        try {
            System.out.println("连接ing.....");
            mmSocket.connect();
            isConnect = true;
            System.out.println("连接成功");
        } catch (IOException e) {
            try {
                mmSocket.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return ;
            }
        }
        //在线程中管理连接; 
    }
    public void cancel(){

        try {
            mmSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

方案二使用uuid;客服端进行连接,服务端接收连接

//<客服端》
public class ConnectThread extends Thread {

    private final BluetoothDevice mmdevice;
    private final BluetoothSocket mmSocket;
    public final String s = "00001101-0000-1000-8000-00805F9B34FB";
    private Boolean isConnect  = false;

    public ConnectThread(BluetoothDevice device){
        BluetoothSocket tmp = null;
        //赋值给设备
        mmdevice = device;
        try {
            //根据UUID创建并返回一个BluetoothSocket
            tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(s));
        } catch(IOException e) {}
        //赋值给BluetoothSocket
        mmSocket = tmp;
    }

    public void run(){
        try {
            System.out.println("连接ing.....");
            mmSocket.connect();
            isConnect = true;
            System.out.println("连接成功");
        } catch (IOException e) {
            try {
                mmSocket.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return ;
            }
        }
        //在线程中管理连接; 
    }
    public void cancel(){

        try {
            mmSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
//服务端
private final BluetoothServerSocket mmserverSocket;

    public AcceptThread(){
        BluetoothServerSocket tmp = null;
        try {
            tmp = BluetoothAdapter.getDefaultAdapter().listenUsingRfcommWithServiceRecord("蓝牙连接", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mmserverSocket = tmp;
    }

    public void run(){
        BluetoothSocket Socket = null;
        while(true){
            try {
                Socket = mmserverSocket.accept();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                break;
            }
            if(Socket == null){
                try {
                    //manageConnectedSocket(Socket);
                    mmserverSocket.close();
                    break;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    public void cancel(){
        try { 
            mmserverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值