Android蓝牙连接汽车OBD设备
2014-04-30 16:28
337 查看
//设备连接 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; }
相关文章推荐
- Android和iOS如何连接指定的蓝牙设备
- android蓝牙开发 蓝牙设备的查找和连接
- 关于Android蓝牙设备的连接使用
- Android/iOS/Unity与BLE蓝牙设备连接失败问题指南
- Android开发笔记(一百六十二)蓝牙设备的连接与配对
- Android 蓝牙设备的查找和连接
- win7与android设备通过蓝牙串口的连接方法
- Android 蓝牙设备的查找和连接
- android当蓝牙设备连接或者断开时候的广播事件处理
- Android开发之蓝牙连接与配对设备
- android wifi(WAP/PSK加密)蓝牙设备的连接
- android手机蓝牙连接蓝牙设备
- 4000 Android 如何获取已连接的蓝牙设备
- android蓝牙开发 蓝牙设备的查找和连接
- win7与android设备通过蓝牙串口的连接方法
- android蓝牙开发 蓝牙设备的查找和连接
- Android 扫描附近的蓝牙设备并连接蓝牙音响
- android蓝牙开发 蓝牙设备的查找和连接
- Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
- win7与android设备通过蓝牙串口的连接方法