
protected voID connect(BluetoothDevice device) { //BluetoothSocket socket = null; try { //Create a Socket connection: need the server's UUID number of registered socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666")); socket.connect(); Log.d("EF-BTBee",">>ClIEnt connectted"); inputStream inputStream = socket.getinputStream(); OutputStream outputStream = socket.getoutputStream(); outputStream.write(new byte[] { (byte) 0xa0,7,16,4,0 }); new Thread() { public voID run() { while(true) { try { Log.d("EF-BTBee",">>Send data thread!"); OutputStream outputStream = socket.getoutputStream(); outputStream.write(new byte[] { (byte) 0xa2,0 }); } catch (IOException e) { Log.e("EF-BTBee","",e); } } }; }.start(); } catch (IOException e) { Log.e("EF-BTBee",e); } finally { if (socket != null) { try { Log.d("EF-BTBee",">>ClIEnt Close"); socket.close(); finish(); return ; } catch (IOException e) { Log.e("EF-BTBee",e); } } }}` 我也试过用
Method m = device.getClass().getmethod("createRfcommSocket",new Class[] {int.class}); socket = (BluetoothSocket) m.invoke(device,1); 而不仅仅是上面的“socket =”行仍然没有成功.
解决方法 如果设备已配对,则可以使用if(device.getBondState()==device.BOND_BONDED){ Log.d(TAG,device.getname()); //BluetoothSocket mSocket=null; try { mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e1) { // Todo auto-generated catch block Log.d(TAG,"socket not created"); e1.printstacktrace(); } try{ mSocket.connect(); } catch(IOException e){ try { mSocket.close(); Log.d(TAG,"Cannot connect"); } catch (IOException e1) { Log.d(TAG,"Socket not closed"); e1.printstacktrace(); } } 用于MY_UUID使用
private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB"); 总结 以上是内存溢出为你收集整理的连接到Android蓝牙插座全部内容,希望文章能够帮你解决连接到Android蓝牙插座所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)