起初,我能够通过运行ICS(4.0.4)的Android手机轻松连接/断开和接收我的蓝牙远程设备。
最近,某种程度上,我的应用程序不再起作用了
没有错误,只是没有发送或接收数据,因为没有连接创建套接字
我的第一个猜测是我的硬件,但事实并非如此
我尝试了我的基本蓝牙连接应用程序,它们也不会工作。
这是logcat:
01-05 22:04:42.576: D/bluetooth2(5316): ...Connecting...
01-05 22:04:42.576: I/BluetoothSocket_MTK(5316): [JSR82] connect: do SDP
01-05 22:04:42.577: I/BluetoothSocket_MTK(5316): [JSR82] close
01-05 22:04:42.577: I/BluetoothSocket_MTK(5316): [JSR82] readLock got.
01-05 22:04:42.577: I/BluetoothSocket_MTK(5316): [JSR82] Start to aquire writeLock.
01-05 22:04:42.577: I/BluetoothSocket_MTK(5316): [JSR82] writeLock got.
01-05 22:04:42.578: D/bluetooth2(5316): ...Create Socket...
01-05 22:04:42.584: D/dalvikvm(5316): threadid=14: interp stack at 0x4d1b5000
01-05 22:04:42.587: D/dalvikvm(5316): threadid=14 (Thread-371): calling run()
01-05 22:04:42.594: D/Buffer read(5316): ...[B@417c0930
01-05 22:04:42.594: D/dalvikvm(5316): threadid=14: exiting
01-05 22:04:42.594: D/dalvikvm(5316): threadid=14: bye!
01-05 22:04:42.594: D/dalvikvm(5316): threadid=0: freeing
01-05 22:04:43.135: I/BluetoothSocket_MTK(5316): [JSR82] SdpHelper::onRfcommChannelFound: channel=-1
01-05 22:04:43.135: I/BluetoothSocket_MTK(5316): [JSR82] close
01-05 22:04:43.135: I/BluetoothSocket_MTK(5316): [JSR82] readLock got.
01-05 22:04:43.135: D/bluetooth2(5316): ...Create Socket...
01-05 22:04:43.138: D/dalvikvm(5316): threadid=14: interp stack at 0x4d1b5000
01-05 22:04:43.138: D/dalvikvm(5316): threadid=14 (Thread-372): calling run()
01-05 22:04:43.138: D/Buffer read(5316): ...[B@417c0ec0
01-05 22:04:43.138: D/dalvikvm(5316): threadid=14: exiting
01-05 22:04:43.138: D/dalvikvm(5316): threadid=14: bye!
01-05 22:04:43.138: D/dalvikvm(5316): threadid=0: freeing
01-05 22:04:43.149: I/BluetoothSocket_MTK(5316): [JSR82] close
01-05 22:04:43.149: I/BluetoothSocket_MTK(5316): [JSR82] readLock got.
01-05 22:04:46.149: I/BluetoothSocket_MTK(5316): [JSR82] Bluetooth Socket Constructor
01-05 22:04:46.150: I/BluetoothSocket_MTK(5316): [JSR82] type=1 fd=-1 auth=false encrypt=false port=-1
我看到有几个人在ICS上遇到与蓝牙连接相同的问题
我也尝试使用this修复它,但它也不起作用。
它说:Could not create Insecure RFComm Connection.
以下是摘录:
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private StringBuilder sb = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// MAC-address of Bluetooth module (you must edit this line)
private static String address = "20:13:06:24:05:60";
的OnCreate
btAdapter = BluetoothAdapter.getDefaultAdapter();
蓝牙连接
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if(Build.VERSION.SDK_INT >= 10){
try {
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
return (BluetoothSocket) m.invoke(device, MY_UUID);
} catch (Exception e) {
Log.e(TAG, "Could not create Insecure RFComm Connection",e);
}
}
return device.createRfcommSocketToServiceRecord(MY_UUID);
}
public void bluetoothDisconnect(){
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
public void bluetoothConnect(){
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
/*try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}*/
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
Log.d(TAG, "...Connecting...");
try {
btSocket.connect();
Log.d(TAG, "....Connection ok...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
Log.d(TAG, "...Create Socket...");
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
}
这个问题是否可以解决?特别是对于这个Android版本?
感谢