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 }