Making successful Bluetooth connections in Android

落爺英雄遲暮 提交于 2019-12-10 10:08:04

问题


I am mystified by two approaches to making BT connections in Android.

This is what I have done for as long as I can remember, and it has worked from 2.3+ devices to early 4.x. This what the Android docs describe as well.

private static final UUID sppUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(sppUUID);
bluetoothSocket.connect();

This has ceased to work on some newer Androids (Nexus 7 running 4.4), Cyanogenmod with this result (or similar):

java.io.IOException: read failed, socket might closed or timeout, read ret: -1

After much thrashing around--much of it in SO!--I find this works

Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
bluetoothSocket = (BluetoothSocket) m.invoke(bluetoothDevice, 1);
bluetoothSocket.connect();

Where does this come from? Why does this work over the other approach?

Thank you

来源:https://stackoverflow.com/questions/21249603/making-successful-bluetooth-connections-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!