Android蓝牙连接汽车OBD设备

1//设备连接 2public class BluetoothConnect implements Runnable { 3 4    private static final UUID CONNECT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 5    private Loger loger = Loger.getLoger(); 6    private BluetoothDevice mDevice; 7    private BluetoothSocket mSocket; 8    private IInterface iInterface; 9    private OutputStream out; 10    private boolean isConnected = false; 11 12    public BluetoothConnect(BluetoothDevice device) { 13        this.mDevice = device; 14    } 15 16    public interface IInterface { 17 18        void connected(BluetoothDevice device); 19         20        void receive(String string); 21         22        void disconnect(BluetoothDevice device); 23         24        void connectError(); 25    } 26     27    @Override 28    public void run() { 29        try { 30            if (iInterface == null) { 31                loger.e("IBluetoothData interface is null"); 32                return; 33            } 34            loger.d("connecting bluetooth device................"); 35            int sdk = Build.VERSION.SDK_INT; 36            if (sdk >= 10) { 37                mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(CONNECT_UUID); 38            } else { 39                mSocket = mDevice.createRfcommSocketToServiceRecord(CONNECT_UUID); 40            } 41            mSocket.connect(); 42            iInterface.connected(mDevice); 43            isConnected = true; 44            InputStream in = mSocket.getInputStream(); 45            out = mSocket.getOutputStream(); 46            String s = ""; 47            byte[] buffer = new byte[1024 * 3]; 48            int len; 49            while((len = in.read(buffer)) > 0){ 50                s += new String(buffer, 0, len, "GBK"); 51                int index = -1; 52                while ((index = s.indexOf("\r\n")) != -1) { 53                    iInterface.receive(s.substring(0, index + 2)); 54                    s = s.substring(index + 2, s.length()); 55                    index = -1; 56                } 57            } 58        } catch (IOException e) { 59            loger.e("", e); 60            iInterface.connectError(); 61        }finally{ 62            iInterface.disconnect(mDevice); 63            isConnected = false; 64        } 65    } 66     67    public void setInterface(IInterface iInterface) { 68        this.iInterface = iInterface; 69    } 70     71    public boolean isConnected(){ 72        return isConnected; 73    } 74 75    public void write(byte[] buffer) { 76        if (out != null) { 77            try { 78                out.write(buffer); 79                out.flush(); 80            } catch (IOException e) { 81                loger.e("write error", e); 82            } 83        } 84    } 85 86    public void close() { 87        try { 88            if (mSocket != null) { 89                mSocket.close(); 90            } 91        } catch (IOException e) { 92            loger.e("close error", e); 93        } 94    } 95}

搜索设备

1        //首先动态注册广播 2        IntentFilter filter = new IntentFilter(); 3        filter.setPriority(Integer.MAX_VALUE); 4        filter.addAction(BluetoothDevice.ACTION_FOUND); 5        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 6        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 7        registerReceiver(receiver, filter); 8//创建广播接收器 9public BroadcastReceiver receiver = new BroadcastReceiver() { 10 11        @Override 12        public void onReceive(Context context, Intent intent) { 13            String action = intent.getAction(); 14            BluetoothDevice device; 15            if (BluetoothDevice.ACTION_FOUND.equals(action)) { 16                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 17                String name = device.getName(); 18                String address = device.getAddress(); 19                int state = device.getBondState(); 20                loger.d(String.format("found bluetooth device name=%s address=%s state=%s", name, address, state == BluetoothDevice.BOND_BONDED ? "BOND_BONDED" : "BOND_NONE")); 21                if (!adapter.getDevices().contains(device)) { 22                    adapter.addDevice(device); 23                } 24            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 25                loger.d("select bluetooth device over!!!"); 26                if (mBluetoothAdapter.isDiscovering()) { 27                    mBluetoothAdapter.cancelDiscovery(); 28                } 29            } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { 30                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 31                switch (device.getBondState()) { 32                case BluetoothDevice.BOND_BONDING: 33                    loger.d("bluetooth device bonding...."); 34                    break; 35                case BluetoothDevice.BOND_BONDED: 36                    adapter.notifyDataSetChanged(); 37                    loger.d("bluetooth device bonded"); 38                    break; 39                case BluetoothDevice.BOND_NONE: 40                    loger.d("bluetooth device none!!!"); 41                    break; 42                default: 43                    break; 44                } 45            } 46        } 47    }; 48 49//配对方法 50public boolean createBond(BluetoothDevice device) { 51        try { 52            Method createBondMethod = device.getClass().getMethod("createBond"); 53            return (Boolean) createBondMethod.invoke(device); 54        } catch (Exception e) { 55            e.printStackTrace(); 56        } 57        return false; 58    }
点赞
收藏

评论区

加载中...

相关推荐

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Android app ADB命令

\查看设备adbdevicesps这个命令是查看当前连接的设备,连接到计算机的android设备或者模拟器将会列出显示若有多台安卓设备,可以通过在adb后面加上s<设备id对指定设备进行装包、卸载等操作\启动adbadbstartserver\关闭adbadbkillserver\安装软件

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid

Twitter的分布式自增ID算法snowflake (Java版)

概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移