android蓝牙串口连接
在做android蓝牙串口连接的时候一般会使用 BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
然后是tmp。connect方法
可是 BluetoothSocket 。connect方法本身就会报很多错误
这是我自己修改的方法
private class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
ImprovedBluetoothDevice improvedBluetoothDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
improvedBluetoothDevice = new ImprovedBluetoothDevice(device);
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
String connectionType = "?";
//蓝牙设备有三十个端口号,是,从1到30,可以一个一个试,这个办法虽然笨,可是管用
for(int port = 1; port < 31; port++) {
Log.d(TAG, "Connecting with port: " + port);
try {
connectionType = "Secure";
Log.d(TAG, "Attempting createRfcommSocket");
BluetoothSocket s = improvedBluetoothDevice.createRfcommSocket(port);
s.connect();
mmSocket = s;
} catch (Exception ex) {
Log.e(TAG, ex.toString());
mmSocket = null;
try {
connectionType = "Insecure";
Log.d(TAG, "Attempting createInsecureRfcommSocket");
BluetoothSocket s = improvedBluetoothDevice.createInsecureRfcommSocket(port);
s.connect();
mmSocket = s;
} catch (Exception ex2) {
Log.e(TAG, ex2.toString());
mmSocket = null;
}
}
if (mmSocket != null) {
Log.d(TAG, "Connection succeeded with " + connectionType + " connection on port " + port);
break;
}
}
//如果还没有获取到mmSocket ,就使用以下方法
if (mmSocket == null) {
try {
mmSocket = improvedBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
mmSocket.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Reset the ConnectThread because we're done
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
if(mmSocket!=null)
{
mmSocket.close();
}
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
ImprovedBluetoothDevice 这个类
/* Copyright (C) 2011, Kenneth Skovhede
* http://www.hexad.dk, opensource@hexad.dk
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.example.bluetoothconnection.util;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import com.example.bluetoothconnection.R;
import com.example.bluetoothconnection.R.string;
import android.app.AlertD
补充:移动开发 , Android ,