我有一个项目要求,其中我们有一个包含两个组件的产品 - 一个 Android 平板电脑和一个 PCB(包含 RN42 蓝牙芯片)。这两个组件位于塑料外壳内,因此总是彼此靠近。这两个组件设备最初会配对一次,因此配对信息将永远存在于平板电脑中。
Problem:
在我的代码中,我使用一个函数来初始化蓝牙套接字并访问 DataOut 和 DataIn 端口,使用这些端口可以在平板电脑和 PCB 之间交换数据。
案例1:当我使用三星S2(4.1.2)和PCB时,这段代码100%有效,
Case2: BUT当我将平板电脑(iball 3G 7271(4.1.2))与印刷电路板一起使用时,大多数时候我在连接到插座时遇到问题。我收到错误说明-
-Service discovery failed,
-Connection is not created (failed or aborted).
-Resource is busy
在大多数情况下。
我尝试过的:
- 尝试使用反射,但没有成功。
- 已使用通用UUID - 00001101-0000-1000-8000-00805F9B34FB,但没有用。
我不明白为什么当我使用带有 RN42 芯片的三星 S2 时代码工作正常,但当我使用带有 RN42 芯片的 iball 平板电脑时代码会抛出错误。在进一步挖掘时,我发现了这篇文章http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/#comment-1414 http://redacacia.me/2012/07/17/overcoming-android-bluetooth-blues-with-reflection-method/#comment-1414据报道,中国设备(iball 就是其中之一)使用 MTK 处理器,并且存在蓝牙连接问题。
下面是我在设备和 RN42 芯片之间进行蓝牙连接的代码
public class AppFunctions {
public static BluetoothAdapter mBluetoothAdapter;
public static BluetoothAdapter iballAdapter = null;
public static BluetoothSocket Socket = null;
public static BluetoothDevice RN42_Device;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static String address = "00:06:66:49:57:60"; // MAC of RN42 bluetooth chip on PCB
public static OutputStream DataOut = null;
public static InputStream DataIn = null;
//Function to turn on BT socket and to initialize it
public static void setBluetooth(Context context) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.disable(); //Disable bluetooth on device
mBluetoothAdapter.enable(); //Enable bluetooth on device
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
@Override
public void run()
{
iballAdapter = BluetoothAdapter.getDefaultAdapter();
Log.i("AppFuntions", "iballAdapter - " + iballAdapter);
RN42_Device = iballAdapter.getRemoteDevice(address);
Log.i("AppFuntions", "RN42_Device - " + RN42_Device);
try {
Socket = RN42_Device.createRfcommSocketToServiceRecord(MY_UUID);
/* tried reflection,but no use
Method m = RN42_Device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
Socket = (BluetoothSocket) m.invoke(RN42_Device, 5);*/
resetConnection();
connect();
Log.i("AppFunctions", "DataOut -" + DataOut);
Log.i("AppFunctions", "DataIn -" + DataIn);
} catch (Exception e) {e.printStackTrace();}
}
}, 10000);
}
private static void resetConnection() {
if (DataIn != null) {
try {DataIn.close();} catch (Exception e) {}
DataIn = null;
}
if (DataOut != null) {
try {DataOut.close();} catch (Exception e) {}
DataOut = null;
}
if (Socket != null) {
try {Socket.close();} catch (Exception e) {}
Socket = null;
}
}
private static boolean connect() {
// Reset all streams and socket.
resetConnection();
if (RN42_Device == null)
RN42_Device = iballAdapter.getRemoteDevice(address);
// Make an RFCOMM binding.
try {
Socket = RN42_Device.createRfcommSocketToServiceRecord(MY_UUID);
/* tried reflection,but no use
Method m = RN42_Device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
Socket = (BluetoothSocket) m.invoke(RN42_Device, 5);*/
} catch (Exception e1) {e1.printStackTrace();return false;}
try {
Socket.connect(); // Fails here most of the time for iball tablet and RN42,though works always for samsung S2 and Rn42
} catch (Exception e) {e.printStackTrace();return false;}
try {
DataOut = Socket.getOutputStream();
DataIn = Socket.getInputStream();
} catch (Exception e) {e.printStackTrace();return false;}
return true;
}
}
三星 s2 的日志(一直工作正常):
I/AppFuntions(21103): iballAdapter - android.bluetooth.BluetoothAdapter@41fe9250
I/AppFuntions(21103): RN42_Device - 00:06:66:49:57:60
V/BluetoothSocket.cpp(21103): initSocketNative
V/BluetoothSocket.cpp(21103): ...fd 66 created (RFCOMM, lm = 26)
V/BluetoothSocket.cpp(21103): initSocketFromFdNative
I/BluetoothConn(21103): connecting to Socket
V/BluetoothSocket.cpp(21103): abortNative
V/BluetoothSocket.cpp(21103): ...asocket_abort(53) complete
V/BluetoothSocket.cpp(21103): destroyNative
V/BluetoothSocket.cpp(21103): ...asocket_destroy(53) complete
V/BluetoothSocket.cpp(21103): abortNative
V/BluetoothSocket.cpp(21103): ...asocket_abort(66) complete
V/BluetoothSocket.cpp(21103): destroyNative
V/BluetoothSocket.cpp(21103): ...asocket_destroy(66) complete
V/BluetoothSocket.cpp(21103): initSocketNative
V/BluetoothSocket.cpp(21103): ...fd 53 created (RFCOMM, lm = 26)
V/BluetoothSocket.cpp(21103): initSocketFromFdNative
D/BluetoothUtils(21103): isSocketAllowedBySecurityPolicy start : device null
V/BluetoothSocket.cpp(21103): connectNative
V/BluetoothSocket.cpp(21103): ...connect(53, RFCOMM) = 0 (errno 115)
I/AppFunctions(21103): DataOut -android.bluetooth.BluetoothOutputStream@41e8b688
I/AppFunctions(21103): DataIn -android.bluetooth.BluetoothInputStream@41e95790
使用 MKT 系列处理器记录 iball 平板电脑:(logcat 错误之一):
I/AppFuntions(10598): iballAdapter - android.bluetooth.BluetoothAdapter@426ebb88
I/AppFuntions(10598): RN42_Device - 00:06:66:49:57:60
I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
I/BluetoothConn(10598): connecting to Socket
I/BluetoothSocket_MTK(10598): [JSR82] close
I/BluetoothSocket_MTK(10598): [JSR82] readLock got.
I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP
I/BluetoothSocket_MTK(10598): [JSR82] SdpHelper::onRfcommChannelFound: channel=1
I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP done; mPort=1
W/System.err(10598): java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).
W/System.err(10598): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:395)
W/System.err(10598): at com.example.guardmonitor.commons.AppFunctions.connect(AppFunctions.java:193)
W/System.err(10598): at com.example.guardmonitor.commons.AppFunctions.access$3(AppFunctions.java:176)
W/System.err(10598): at com.example.guardmonitor.commons.AppFunctions$1.run(AppFunctions.java:109)
W/System.err(10598): at java.util.Timer$TimerImpl.run(Timer.java:284)
I/AppFunctions(10598): DataOut -null
I/AppFunctions(10598): DataIn -null
请告诉我 :
- 我做错了什么/我应该做什么来解决我的 iball 平板电脑与 Rn42 芯片的连接问题。
- 任何有用的资源都可以分享。
提前致谢!