Android蓝牙连接汽车OBD设备

//设备连接
public class BluetoothConnect implements Runnable {

    private static final UUID CONNECT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private Loger loger = Loger.getLoger();
    private BluetoothDevice mDevice;
    private BluetoothSocket mSocket;
    private IInterface iInterface;
    private OutputStream out;
    private boolean isConnected = false;

    public BluetoothConnect(BluetoothDevice device) {
        this.mDevice = device;
    }

    public interface IInterface {

        void connected(BluetoothDevice device);
        
        void receive(String string);
        
        void disconnect(BluetoothDevice device);
        
        void connectError();
    }
    
    @Override
    public void run() {
        try {
            if (iInterface == null) {
                loger.e("IBluetoothData interface is null");
                return;
            }
            loger.d("connecting bluetooth device................");
            int sdk = Build.VERSION.SDK_INT;
            if (sdk >= 10) {
                mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(CONNECT_UUID);
            } else {
                mSocket = mDevice.createRfcommSocketToServiceRecord(CONNECT_UUID);
            }
            mSocket.connect();
            iInterface.connected(mDevice);
            isConnected = true;
            InputStream in = mSocket.getInputStream();
            out = mSocket.getOutputStream();
            String s = "";
            byte[] buffer = new byte[1024 * 3];
            int len;
            while((len = in.read(buffer)) > 0){
                s += new String(buffer, 0, len, "GBK");
                int index = -1;
                while ((index = s.indexOf("\r\n")) != -1) {
                    iInterface.receive(s.substring(0, index + 2));
                    s = s.substring(index + 2, s.length());
                    index = -1;
                }
            }
        } catch (IOException e) {
            loger.e("", e);
            iInterface.connectError();
        }finally{
            iInterface.disconnect(mDevice);
            isConnected = false;
        }
    }
    
    public void setInterface(IInterface iInterface) {
        this.iInterface = iInterface;
    }
    
    public boolean isConnected(){
        return isConnected;
    }

    public void write(byte[] buffer) {
        if (out != null) {
            try {
                out.write(buffer);
                out.flush();
            } catch (IOException e) {
                loger.e("write error", e);
            }
        }
    }

    public void close() {
        try {
            if (mSocket != null) {
                mSocket.close();
            }
        } catch (IOException e) {
            loger.e("close error", e);
        }
    }
}

搜索设备

        //首先动态注册广播
        IntentFilter filter = new IntentFilter();
        filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(receiver, filter);
//创建广播接收器
public BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device;
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                String address = device.getAddress();
                int state = device.getBondState();
                loger.d(String.format("found bluetooth device name=%s address=%s state=%s", name, address, state == BluetoothDevice.BOND_BONDED ? "BOND_BONDED" : "BOND_NONE"));
                if (!adapter.getDevices().contains(device)) {
                    adapter.addDevice(device);
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                loger.d("select bluetooth device over!!!");
                if (mBluetoothAdapter.isDiscovering()) {
                    mBluetoothAdapter.cancelDiscovery();
                }
            } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (device.getBondState()) {
                case BluetoothDevice.BOND_BONDING:
                    loger.d("bluetooth device bonding....");
                    break;
                case BluetoothDevice.BOND_BONDED:
                    adapter.notifyDataSetChanged();
                    loger.d("bluetooth device bonded");
                    break;
                case BluetoothDevice.BOND_NONE:
                    loger.d("bluetooth device none!!!");
                    break;
                default:
                    break;
                }
            }
        }
    };

//配对方法
public boolean createBond(BluetoothDevice device) {
        try {
            Method createBondMethod = device.getClass().getMethod("createBond");
            return (Boolean) createBondMethod.invoke(device);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


转载于:https://my.oschina.net/hes/blog/228911

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值